-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete-no-js-entries-plugin.js
71 lines (51 loc) · 1.8 KB
/
delete-no-js-entries-plugin.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
class DeleteNoJsEntriesPlugin {
constructor(options) {
// list entries with no js files
this.entriesWithNoJs = [];
}
// define the `apply` method
apply(compiler) {
// specify the event hook to attach to
compiler.hooks.emit.tapAsync('DeleteNoJsEntriesPlugin', (compilation, callback) => {
// prepare list of no js entry
compilation.chunks.forEach((chunk) => {
if (chunk.entryModule) {
const entryNoJs = chunk.entryModule.dependencies.every((dep, index) => {
return /\.js$/.test(dep.request) === false;
});
if (entryNoJs) {
this.entriesWithNoJs.push(chunk.name);
}
}
});
// follow code get from symfony/webpack-encore package,
// see: https://github.com/symfony/webpack-encore/blob/master/lib/webpack/delete-unused-entries-js-plugin.js
// loop over output chunks
compilation.chunks.forEach((chunk) => {
// if current chunk is from no js entry
if (this.entriesWithNoJs.includes(chunk.name)) {
let fileDeleteCount = 0;
// loop over the output files and find one *.js
chunk.files.forEach((filename) => {
if (/\.js(\.map)?(\?[^.]*)?$/.test(filename)) {
fileDeleteCount++;
// remove the output file
delete compilation.assets[filename];
// also remove file from manifest
chunk.files.splice(chunk.files.indexOf(filename), 1);
}
});
// sanity check: make sure 1 or 2 files were deleted
// if there's some edge case where more .js files
// or 0 .js files might be deleted, I'd rather error
if (fileDeleteCount === 0 || fileDeleteCount > 2) {
throw new Error(`Problem deleting JS entry for ${chunk.name}: ${fileDeleteCount} files were deleted`);
}
}
});
callback();
});
}
}
module.exports = DeleteNoJsEntriesPlugin;