forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsuppress-entry-chunks-webpack-plugin.ts
49 lines (47 loc) · 1.96 KB
/
suppress-entry-chunks-webpack-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Remove .js files from entry points consisting entirely of .css|scss|sass|less|styl.
// To be used together with ExtractTextPlugin.
export class SuppressExtractedTextChunksWebpackPlugin {
constructor() { }
apply(compiler: any): void {
compiler.plugin('compilation', function (compilation: any) {
// find which chunks have css only entry points
const cssOnlyChunks: string[] = [];
const entryPoints = compilation.options.entry;
// determine which entry points are composed entirely of css files
for (let entryPoint of Object.keys(entryPoints)) {
if (entryPoints[entryPoint].every((el: string) =>
el.match(/\.(css|scss|sass|less|styl)$/))) {
cssOnlyChunks.push(entryPoint);
}
}
// Remove the js file for supressed chunks
compilation.plugin('after-seal', (callback: any) => {
compilation.chunks
.filter((chunk: any) => cssOnlyChunks.indexOf(chunk.name) !== -1)
.forEach((chunk: any) => {
let newFiles: string[] = [];
chunk.files.forEach((file: string) => {
if (file.match(/\.js$/)) {
// remove js files
delete compilation.assets[file];
} else {
newFiles.push(file);
}
});
chunk.files = newFiles;
});
callback();
});
// Remove scripts tags with a css file as source, because HtmlWebpackPlugin will use
// a css file as a script for chunks without js files.
compilation.plugin('html-webpack-plugin-alter-asset-tags',
(htmlPluginData: any, callback: any) => {
const filterFn = (tag: any) =>
!(tag.tagName === 'script' && tag.attributes.src.match(/\.css$/));
htmlPluginData.head = htmlPluginData.head.filter(filterFn);
htmlPluginData.body = htmlPluginData.body.filter(filterFn);
callback(null, htmlPluginData);
});
});
}
}