|
1 | 1 | const NAME = "webpack-fix-style-only-entries"; |
2 | 2 |
|
| 3 | +const defaultOptions = { |
| 4 | + extensions: ["less", "scss", "css", "sass", "styl"], |
| 5 | + silent: false, |
| 6 | + ignore: undefined, |
| 7 | +}; |
| 8 | + |
3 | 9 | class WebpackFixStyleOnlyEntriesPlugin { |
4 | 10 | constructor(options) { |
5 | 11 | this.apply = this.apply.bind(this); |
6 | 12 |
|
7 | | - options = options || {}; |
8 | | - let extensions = options.extensions || ["less", "scss", "css"]; |
9 | | - this.extensions = extensions.map(e => (e[0] === "." ? e : "." + e)); |
| 13 | + this.options = Object.assign({}, defaultOptions, options); |
10 | 14 | } |
11 | 15 |
|
12 | 16 | apply(compiler) { |
| 17 | + const extensionsWithoutDots = this.options.extensions.map(e => |
| 18 | + e[0] === "." ? e.substring(1) : e |
| 19 | + ); |
| 20 | + |
| 21 | + const patternOneOfExtensions = extensionsWithoutDots |
| 22 | + .map(ext => escapeRegExp(ext)) |
| 23 | + .join("|"); |
| 24 | + |
| 25 | + const reStylesResource = new RegExp( |
| 26 | + `[.](${patternOneOfExtensions})([?].*)?$` |
| 27 | + ); |
| 28 | + |
13 | 29 | compiler.hooks.compilation.tap(NAME, compilation => { |
| 30 | + const resourcesCache = []; |
14 | 31 | compilation.hooks.chunkAsset.tap(NAME, (chunk, file) => { |
15 | | - if (chunk.hasEntryModule()) { |
16 | | - let resources; |
17 | | - if (typeof chunk.entryModule.resource == "string") { |
18 | | - resources = [chunk.entryModule.resource]; |
19 | | - } else { |
20 | | - if ( |
21 | | - chunk.entryModule.dependencies && |
22 | | - chunk.entryModule.dependencies.length |
23 | | - ) { |
24 | | - const modules = chunk.entryModule.dependencies.map( |
25 | | - dep => dep.module |
26 | | - ); |
27 | | - resources = modules.map(m => m.resource); |
28 | | - } |
29 | | - } |
| 32 | + if (!file.endsWith(".js") && !file.endsWith(".mjs")) return; |
| 33 | + if (!chunk.hasEntryModule()) return; |
30 | 34 |
|
31 | | - if (resources) { |
32 | | - if ( |
33 | | - resources.every(resource => |
34 | | - this.extensions.find(ext => resource.endsWith(ext)) |
35 | | - ) |
36 | | - ) { |
37 | | - if (file.endsWith(".js")) { |
38 | | - console.log("removing js from style only module: " + file); |
39 | | - chunk.files = chunk.files.filter(f => f != file); |
40 | | - delete compilation.assets[file]; |
41 | | - } |
42 | | - } |
| 35 | + const rawResources = collectEntryResources(compilation, chunk.entryModule, resourcesCache); |
| 36 | + const resources = this.options.ignore |
| 37 | + ? rawResources.filter(r => !r.match(this.options.ignore)) |
| 38 | + : rawResources; |
| 39 | + |
| 40 | + const isStyleOnly = |
| 41 | + resources.length && |
| 42 | + resources.every(resource => reStylesResource.test(resource)); |
| 43 | + if (isStyleOnly) { |
| 44 | + if (!this.options.silent) { |
| 45 | + console.error( |
| 46 | + "webpack-fix-style-only-entries: removing js from style only module: " + |
| 47 | + file |
| 48 | + ); |
43 | 49 | } |
| 50 | + chunk.files = chunk.files.filter(f => f != file); |
| 51 | + delete compilation.assets[file]; |
44 | 52 | } |
45 | 53 | }); |
46 | 54 | }); |
47 | 55 | } |
48 | 56 | } |
49 | 57 |
|
| 58 | +function collectEntryResources(compilation, module, cache) { |
| 59 | + // module.index is unique per compilation |
| 60 | + // module.id can be null, not used here |
| 61 | + if (cache[module.index] !== undefined) { |
| 62 | + return cache[module.index]; |
| 63 | + } |
| 64 | + |
| 65 | + if (typeof module.resource == "string") { |
| 66 | + const resources = [module.resource]; |
| 67 | + cache[module.index] = resources; |
| 68 | + return resources; |
| 69 | + } |
| 70 | + |
| 71 | + const resources = []; |
| 72 | + if (module.dependencies) { |
| 73 | + const hasModuleGraphSupport = compilation.hasOwnProperty('moduleGraph'); |
| 74 | + module.dependencies.forEach(dep => { |
| 75 | + if(dep) { |
| 76 | + const module = hasModuleGraphSupport ? compilation.moduleGraph.getModule(dep) : dep.module; |
| 77 | + const originModule = hasModuleGraphSupport ? compilation.moduleGraph.getParentModule(dep) : dep.originModule; |
| 78 | + const nextModule = module || originModule; |
| 79 | + if (nextModule) { |
| 80 | + const depResources = collectEntryResources(compilation, nextModule, cache); |
| 81 | + for (let index = 0, length = depResources.length; index !== length; index++) { |
| 82 | + resources.push(depResources[index]); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + cache[module.index] = resources; |
| 90 | + return resources; |
| 91 | +} |
| 92 | + |
| 93 | +// https://github.com/lodash/lodash/blob/4.17.11/lodash.js#L14274 |
| 94 | +const reRegExpChar = /[\\^$.*+?()[\]{}|]/g; |
| 95 | +const reHasRegExpChar = RegExp(reRegExpChar.source); |
| 96 | +function escapeRegExp(string) { |
| 97 | + string = String(string); |
| 98 | + return string && reHasRegExpChar.test(string) |
| 99 | + ? string.replace(reRegExpChar, "\\$&") |
| 100 | + : string; |
| 101 | +} |
| 102 | + |
50 | 103 | module.exports = WebpackFixStyleOnlyEntriesPlugin; |
0 commit comments