Skip to content

Commit

Permalink
chore: Move script compilation from @rollup/stream to plain rollup. (#…
Browse files Browse the repository at this point in the history
…1618)

* Added rollup configuration file and plugins file.

* Added npm scripts for running rollup.

* Updated the scripts' gulp tasks to run the new rollup commands.

* Renamed the compile:dev command to match the gulp task's name.

* Updated the gitignore.

* Uninstalled rollup/stream as it's no longer needed.

* Simplified our lives a little by leaving the bundle's name the same in prod and in local dev.

* Added a changelog entry.

* Added a TODO to the Karma file.

* Added some internal logging in the scripts' compilation step.

* Silenced the unknown option warning for mode.
  • Loading branch information
shirblc authored Apr 18, 2024
1 parent 0bb431d commit ef149e0
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 93 deletions.
12 changes: 5 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ coverage
.DS_Store

# local development files
localdev/app
localdev/css
localdev/app.bundle.js
localdev/app.bundle.js.map
localdev/index.html
localdev/assets/*
localdev/manifest.webmanifest
localdev/*
# TODO: These should really be moved to the src folder
!localdev/favicon.ico
!localdev/sitemap.xml
!localdev/sw.js

# Testing files
src/tests.specs.ts
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The project is open source, so feel free to use parts of the code. However, the
2. cd into the project directory.
3. Run `git config core.hooksPath .githooks` to install the pre-commit hook, which runs prettier.
4. Run `npm install` to install dependencies.
5. Run `gulp localdev` to compile the whole project for local development.
5. Run `npm run localdev` to compile the whole project for local development.
6. Run `gulp serve` to start the local server.
7. Open localhost:3000.

Expand Down
13 changes: 13 additions & 0 deletions changelog/pr1618.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"pr_number": 1618,
"changes": [
{
"change": "Chores",
"description": "Replaced @rollup/stream with rollup in the process of bundling the site's scripts. Since @rollup/stream is the recommended plugin for making rollup work with gulp, we've been using it in script compilation. However, @rollup/stream doesn't support chunking, and the process of using it with gulp produces considerably larger JavaScript bundles than rollup itself. Moving to rollup allows us to generate smaller packages, and, in the future, it will allow us to start splitting the code. The change includes:\n\t- Added a new rollup configuration file.\n\t- Converted the old processor CommonJS file (with the rollup plugins we use in compilation and in tests) to an ES module with the plugins we use.\n\t- The gulp tasks for bundling the scripts now run rollup via a child process (Node.js's `exec`) instead of running @rollup/stream."
},
{
"change": "Chores",
"description": "Combined the rollup plugin for setting the environment to production with the rollup plugin for replacing the environment variables. Since they both update the environment variables and the production environment setter now consists of one line of code, there was no need to keep them separated."
}
]
}
85 changes: 16 additions & 69 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,13 @@ const gulp = require("gulp");
const postcss = require("gulp-postcss");
const rename = require("gulp-rename");
const replace = require("gulp-replace");
const source = require("vinyl-source-stream");
const buffer = require("vinyl-buffer");
const less = require('gulp-less');
// rollup deps
const rollupStream = require("@rollup/stream");
const commonjs = require("@rollup/plugin-commonjs");
const nodeResolve = require("@rollup/plugin-node-resolve").nodeResolve;
const typescript = require("@rollup/plugin-typescript");
const terser = require('@rollup/plugin-terser');
// everything else
const autoprefixer = require("autoprefixer");
const browserSync = require("browser-sync").create();
const Server = require('karma').Server;
const parseConfig = require('karma').config.parseConfig;
const glob = require("glob");
// internal plugins
const {
setProductionEnv,
updateComponentTemplateUrl,
updateEnvironmentVariables
} = require("./processor");

let bs;

Expand Down Expand Up @@ -98,30 +84,19 @@ function styles()
}

//deals with transforming the scripts while in development mode
function scripts()
async function scripts()
{
const options = {
input: 'src/main.ts',
output: { sourcemap: 'inline' },
plugins: [
updateEnvironmentVariables("development"),
updateComponentTemplateUrl(),
typescript({ exclude: ['**/*.spec.ts', 'e2e/**/*'] }),
nodeResolve({
extensions: ['.js', '.ts']
}),
commonjs({
extensions: ['.js', '.ts'],
transformMixedEsModules: true
})
]
};

return rollupStream(options)
.pipe(source("src/main.ts"))
.pipe(buffer())
.pipe(rename("app.bundle.js"))
.pipe(gulp.dest("./localdev"));
// A bit hacky, but worth it considering the massive size improvement
// rollup provides vs @rollup/stream
await exec("npm run rollup:dev", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
}
if (stderr) {
console.log(`stderr: ${stderr}`);
}
console.log(`stdout: ${stdout}`);
});
}

// copy webmanifest
Expand Down Expand Up @@ -208,10 +183,6 @@ function copyIndexDist()
{
return gulp
.src("index.html")
.pipe(replace(/src="app.bundle.js"/, () => {
let newString = `src="app.bundle.min.js"`
return newString;
}))
.pipe(gulp.dest("./dist"));
}

Expand All @@ -234,35 +205,11 @@ function stylesDist()
}

//deals with transforming and bundling the scripts while in production mode
function scriptsDist()
async function scriptsDist()
{
const options = {
input: 'src/main.ts',
output: {
file: "app.bundle.min.js",
sourcemap: 'hidden',
plugins: [terser()]
},
plugins: [
updateEnvironmentVariables("live"),
setProductionEnv(),
updateComponentTemplateUrl(),
typescript({ exclude: ['**/*.spec.ts', 'e2e/**/*'] }),
nodeResolve({
extensions: ['.js', '.ts']
}),
commonjs({
extensions: ['.js', '.ts'],
transformMixedEsModules: true
})
]
};

return rollupStream(options)
.pipe(source("src/main.ts"))
.pipe(buffer())
.pipe(rename("app.bundle.min.js"))
.pipe(gulp.dest("./dist"));
// A bit hacky, but worth it considering the massive size improvement
// rollup provides vs @rollup/stream
await exec("npm run rollup:live");
}

//copy the service worker to the distribution folder; update the cache version on each build
Expand Down
4 changes: 4 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const path = require("path");
// TODO: Figure out how to avoid defining the same module TWICE
// However, seems like Karma doesn't like ESM and since it's deprecated
// it's unlikely to get an update to fix that:
// https://github.com/karma-runner/karma/issues/3677
const { inlineComponentTemplate, inlineSVGs, updateEnvironmentVariables } = require("./processor");
const coverage = require("rollup-plugin-istanbul");
const commonjs = require("@rollup/plugin-commonjs");
Expand Down
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
},
"scripts": {
"format": "prettier . --write",
"compile:dev": "gulp localDev",
"localDev": "gulp localDev",
"test": "gulp test",
"cypress": "cypress run",
"e2e": "gulp e2e",
"build": "gulp dist",
"start": "node server.js"
"start": "node server.js",
"rollup:dev": "rollup --config --mode development",
"rollup:live": "rollup --config"
},
"overrides": {
"karma-jasmine": {
Expand All @@ -26,7 +28,6 @@
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.6",
"@rollup/stream": "^3.0.1",
"@types/auth0-js": "^9.21.5",
"@types/jasmine": "^5.1.4",
"@types/node": "^20.12.7",
Expand Down
144 changes: 144 additions & 0 deletions plugins.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import MagicString from "magic-string";
import fs from "fs";
import dotenv from "dotenv";

/**
* Rollup plugin that replaces the Angular templateUrl in test files with
* the inlined template. Originally written as a Browserify transform:
* https://github.com/shirblc/angular-gulp/pull/1
*
* Inspited by @rollup/plugin-replce
* https://github.com/rollup/plugins/blob/master/packages/replace/src/index.js
*/
export function inlineComponentTemplate() {
return {
name: "plugin-inline-template",
transform(code) {
const magicString = new MagicString(code);
const commonComponents = fs.readdirSync("./src/app/common/components");
const adminComponents = fs.readdirSync("./src/app/admin/components");
const userComponents = fs.readdirSync("./src/app/user/components");

magicString.replace(/(templateUrl:)(.*)(\.component\.html")/, (match) => {
const componentName = match.split(".")[1].substring(1);
if (componentName == "my") return match;
let componentTemplateURL;

if (componentName == "app") {
componentTemplateURL = __dirname + `/src/app/${componentName}.component.html`;
} else if (commonComponents.includes(componentName)) {
componentTemplateURL =
__dirname +
`/src/app/common/components/${componentName}/${componentName}.component.html`;
} else if (adminComponents.includes(componentName)) {
componentTemplateURL =
__dirname +
`/src/app/admin/components/${componentName}/${componentName}.component.html`;
} else if (userComponents.includes(componentName)) {
componentTemplateURL =
__dirname + `/src/app/user/components/${componentName}/${componentName}.component.html`;
} else {
componentTemplateURL =
__dirname + `/src/app/components/${componentName}/${componentName}.component.html`;
}

componentTemplate = fs.readFileSync(componentTemplateURL);

return `template: \`${componentTemplate}\``;
});

return {
code: magicString.toString(),
map: magicString.generateMap(),
};
},
};
}

/**
* Rollup plugin for inlining the SVGs.
* Originally written as a Browserify transform:
* https://github.com/sendahug/send-hug-frontend/commit/d0cb971da5801ebfecb05184d09386e743b7405b
*/
export function inlineSVGs() {
return {
name: "inliner",
transform(code) {
const magicString = new MagicString(code);

// inline the SVGs
magicString.replace(/(<img src="..\/assets.)(.*)(.">)/g, (match) => {
const altIndex = match.indexOf("alt");
const url = match.substring(13, altIndex - 2);
const svg = fs.readFileSync(__dirname + `/src/${url}`);

return `${svg}`;
});

return {
code: magicString.toString(),
map: magicString.generateMap(),
};
},
};
}

/**
* Updates the environment variables in the app based on
* the environment variables
*/
export function updateEnvironmentVariables(currentMode = "development") {
return {
name: "rollup-plugin-update-environment-variables",
transform(code) {
const magicString = new MagicString(code);

// Sets the angular environment to production.
// Originally written as a Browserify transform:
// https://github.com/sendahug/send-hug-frontend/blob/c783442236d07d4aa9d7439b3bc74e450bf4b5ec/gulpfile.js#L232
// And later updated to a rollup plugin:
// https://github.com/sendahug/send-hug-frontend/blob/0bb431d0a4f4cdba441d25d4dc9fa836f198ba93/processor.js#L91
if (currentMode != "development") {
magicString.replace(/environments\/environment/, "environments/environment.prod");
}

dotenv.config({
path: `./.env.${currentMode}`,
});

magicString.replaceAll(/process\.env\.(.+),/g, (match) => {
const envVar = match.split(".")[2].split(",")[0];
const value = process.env[envVar];
return `'${value}',`;
});

return {
code: magicString.toString(),
map: magicString.generateMap(),
};
},
};
}

/**
* Updates each component's template URL to the production
* structure.
*/
export function updateComponentTemplateUrl() {
return {
name: "plugin-template-updater",
transform(code) {
const magicString = new MagicString(code);

magicString.replace(/(templateUrl:)(.*)(\.component\.html")/, (match) => {
const componentName = match.split(".")[1].substring(1);
return `templateUrl: "./app/${componentName}.component.html"`;
});

return {
code: magicString.toString(),
map: magicString.generateMap(),
};
},
};
}
Loading

0 comments on commit ef149e0

Please sign in to comment.