mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-23 15:36:23 +08:00
Adds sourcemap and minification to javascript added via the `scripts` array in `.angular-cli.json`. `script-loader` is no longer used, which should help with CSP since it used `eval`. Scripts will no longer appear in the console output for `ng build`, as they are now assets instead of webpack entry points. It's no longer possible to have the `output` property of both a `scripts` and a `styles` entry pointing to the same file. This wasn't officially supported or listed in the docs, but used to be possible. Fix #2796 Fix #7226 Fix #7290 Related to #6872
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
// Add assets from `ConcatPlugin` to index.html.
|
|
|
|
export class InsertConcatAssetsWebpackPlugin {
|
|
// Priority list of where to insert asset.
|
|
private insertAfter = [
|
|
/polyfills(\.[0-9a-f]{20})?\.bundle\.js/,
|
|
/inline(\.[0-9a-f]{20})?\.bundle\.js/,
|
|
];
|
|
|
|
constructor(private entryNames: string[]) { }
|
|
|
|
apply(compiler: any): void {
|
|
compiler.plugin('compilation', (compilation: any) => {
|
|
compilation.plugin('html-webpack-plugin-before-html-generation',
|
|
(htmlPluginData: any, callback: any) => {
|
|
|
|
const fileNames = this.entryNames.map((entryName) => {
|
|
const fileName = htmlPluginData.assets.webpackConcat
|
|
&& htmlPluginData.assets.webpackConcat[entryName];
|
|
|
|
if (!fileName) {
|
|
// Something went wrong and the asset was not correctly added.
|
|
throw new Error(`Cannot find file for ${entryName} script.`);
|
|
}
|
|
|
|
return fileName;
|
|
});
|
|
|
|
let insertAt = 0;
|
|
|
|
// TODO: try to figure out if there are duplicate bundle names when adding and throw
|
|
for (let el of this.insertAfter) {
|
|
const jsIdx = htmlPluginData.assets.js.findIndex((js: string) => js.match(el));
|
|
if (jsIdx !== -1) {
|
|
insertAt = jsIdx + 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
htmlPluginData.assets.js.splice(insertAt, 0, ...fileNames);
|
|
callback(null, htmlPluginData);
|
|
});
|
|
});
|
|
}
|
|
}
|