-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Move script compilation from @rollup/stream to plain rollup. (#…
…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
Showing
9 changed files
with
232 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}; | ||
}, | ||
}; | ||
} |
Oops, something went wrong.