-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathsuppress-entry-chunks-webpack-plugin.ts
55 lines (49 loc) · 1.63 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
50
51
52
53
54
55
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
/**
* Remove .js files from entry points consisting entirely of stylesheets.
* To be used together with mini-css-extract-plugin.
*/
export class SuppressExtractedTextChunksWebpackPlugin {
apply(compiler: import('webpack').Compiler): void {
compiler.hooks.compilation.tap('SuppressExtractedTextChunks', (compilation) => {
compilation.hooks.chunkAsset.tap('SuppressExtractedTextChunks', (chunk, filename) => {
// Remove only JavaScript assets
if (!filename.endsWith('.js')) {
return;
}
// Only chunks with a css asset should have JavaScript assets removed
let hasCssFile = false;
for (const file of chunk.files) {
if (file.endsWith('.css')) {
hasCssFile = true;
break;
}
}
if (!hasCssFile) {
return;
}
// Only chunks with all CSS entry dependencies should have JavaScript assets removed
let cssOnly = false;
const entryModules = compilation.chunkGraph.getChunkEntryModulesIterable(chunk);
for (const module of entryModules) {
cssOnly = module.dependencies.every(
(dependency: {}) => dependency.constructor.name === 'CssDependency',
);
if (!cssOnly) {
break;
}
}
if (cssOnly) {
chunk.files.delete(filename);
compilation.deleteAsset(filename);
}
});
});
}
}