|
8 | 8 | * @constructor |
9 | 9 | */ |
10 | 10 | function OmitJSforCSSPlugin(options) { |
11 | | - const defaults = { |
12 | | - preview: false, // OPTIONAL - {Boolean} - A preview of the files that are to be omitted (Will not actually omit) |
13 | | - cacheOnWatch: false, // OPTIONAL - {Boolean} - Whether it should cache the JS filenames that should be omitted on watch |
14 | | - verbose: false // OPTIONAL - {Boolean} - Whether it should display which files will be omitted |
15 | | - }; |
| 11 | + const defaults = { |
| 12 | + preview: false, // OPTIONAL - {Boolean} - A preview of the files that are to be omitted (Will not actually omit) |
| 13 | + verbose: false // OPTIONAL - {Boolean} - Whether it should display which files will be omitted |
| 14 | + }; |
16 | 15 |
|
17 | | - if ((typeof options !== 'undefined' && (options === null || typeof options !== 'object')) || Array.isArray(options)) { |
18 | | - throw new Error('OmitJSforCSSPlugin only takes an options "object" as an argument'); |
19 | | - } |
| 16 | + if ((typeof options !== 'undefined' && (options === null || typeof options !== 'object')) || Array.isArray(options)) { |
| 17 | + throw new Error('OmitJSforCSSPlugin only takes an options "object" as an argument'); |
| 18 | + } |
20 | 19 |
|
21 | | - this.options = Object.assign({}, defaults, options || {}); |
22 | | - this.cacheOmittedFilename = []; |
| 20 | + this.options = Object.assign({}, defaults, options || {}); |
23 | 21 | } |
24 | 22 |
|
25 | 23 | /** |
26 | 24 | * @function omitFiles |
27 | 25 | * @param {Object} omitted - The omitted file's details |
28 | 26 | * @param {Object} compilation |
29 | 27 | */ |
30 | | -OmitJSforCSSPlugin.prototype.omitFiles = function(omitted, compilation) { |
31 | | - if (this.options.preview) { |
32 | | - console.log(`PREVIEW File to be omitted for ${omitted.chunkName} : ${omitted.filename}`); |
33 | | - } else { |
34 | | - this.options.verbose && console.log(`File omitted for ${omitted.chunkName} : ${omitted.filename}`); |
35 | | - delete compilation.assets[omitted.filename]; |
36 | | - } |
| 28 | +OmitJSforCSSPlugin.prototype.omitFiles = function (omitted, compilation) { |
| 29 | + if (this.options.preview) { |
| 30 | + console.log(`PREVIEW File to be omitted for ${omitted.chunkName} : ${omitted.filename}`); |
| 31 | + } else { |
| 32 | + this.options.verbose && console.log(`File omitted for ${omitted.chunkName} : ${omitted.filename}`); |
| 33 | + delete compilation.assets[omitted.filename]; |
| 34 | + } |
37 | 35 | }; |
38 | 36 |
|
39 | 37 | /** |
40 | 38 | * @function findOmissibleFiles |
41 | 39 | * @param {Object} compilation |
42 | 40 | */ |
43 | | -OmitJSforCSSPlugin.prototype.findOmissibleFiles = function(compilation) { |
44 | | - // Every chunk / entry point |
45 | | - compilation.chunks.forEach(chunk => { |
46 | | - // Chunks origin files. ex. origin entry point, ![] entry |
47 | | - let resourceOrigin = {}; |
48 | | - let assetTypeCount = { internal: 0, css: 0 }; |
| 41 | +OmitJSforCSSPlugin.prototype.findOmissibleFiles = function (compilation) { |
| 42 | + // Every chunk / entry point |
| 43 | + compilation.chunks.forEach(chunk => { |
| 44 | + let assetTypeCount = { internal: 0, css: 0 }; |
49 | 45 |
|
50 | | - chunk.origins.forEach(origin => { |
51 | | - if (typeof origin.module.resource === 'string') { |
52 | | - resourceOrigin[origin.module.resource] = true; |
53 | | - } |
54 | | - }); |
| 46 | + // Each entry point will have its own dependencies, based on the files inner deps or the array deps in entry |
| 47 | + Array.from(chunk.modulesIterable, module => { |
| 48 | + if (!Array.isArray(module.dependencies)) return; |
55 | 49 |
|
56 | | - // Each entry point will have its own dependencies, based on the files inner deps or the array deps in entry |
57 | | - chunk.forEachModule(module => { |
58 | | - if (!Array.isArray(module.fileDependencies)) { |
59 | | - return; |
60 | | - } |
61 | | - module.fileDependencies.forEach(filepath => { |
62 | | - if (!resourceOrigin[filepath] && !/(\bnode_modules\b)/.test(filepath) && /\.(css|js)$/g.test(filepath)) { |
63 | | - /\.(css)$/i.test(filepath) ? assetTypeCount.css++ : assetTypeCount.internal++; |
64 | | - } |
65 | | - }); |
66 | | - }); |
| 50 | + module.dependencies.forEach(({ request }) => { |
| 51 | + if (!/ (\bnode_modules\b) /.test(request) && /\.(css|js)$/g.test(request)) { |
| 52 | + /\.(css)$/i.test(request) ? assetTypeCount.css++ : assetTypeCount.internal++; |
| 53 | + } |
| 54 | + }); |
| 55 | + }); |
67 | 56 |
|
68 | | - // Get the filenames that will be emitted, generated by the chunk, and omit JS if applicable |
69 | | - chunk.files.forEach(filename => { |
70 | | - // If all dependencies of this entry were CSS, then a JS version of this file will be created |
71 | | - // This js file will be empty due to extract-text-webpack-plugin |
72 | | - if (assetTypeCount.css > 0 && assetTypeCount.internal === 0 && (/\.(js)$/i.test(filename) || /\.(js).map$/i.test(filename))) { |
73 | | - let omitted = { |
74 | | - filename: filename, |
75 | | - chunkName: chunk.name |
76 | | - }; |
| 57 | + // Get the filenames that will be emitted, generated by the chunk, and omit JS if applicable |
| 58 | + chunk.files.forEach(filename => { |
| 59 | + // If all dependencies of this entry were CSS, then a JS version of this file will be created |
| 60 | + // This js file will be empty due to extract-text-webpack-plugin |
| 61 | + if (assetTypeCount.css > 0 && assetTypeCount.internal === 0 && (/\.(js)$/i.test(filename) || /\.(js).map$/i.test(filename))) { |
| 62 | + let omitted = { |
| 63 | + filename: filename, |
| 64 | + chunkName: chunk.name |
| 65 | + }; |
77 | 66 |
|
78 | | - this.cacheOmittedFilename.push(omitted); |
79 | | - this.omitFiles(omitted, compilation); |
80 | | - } |
81 | | - }); |
82 | | - }); |
| 67 | + this.omitFiles(omitted, compilation); |
| 68 | + } |
| 69 | + }); |
| 70 | + }); |
83 | 71 | }; |
84 | 72 |
|
85 | 73 | /** |
86 | 74 | * Hook into the webpack compiler |
87 | 75 | * @param {Object} compiler - The webpack compiler object |
88 | 76 | */ |
89 | | -OmitJSforCSSPlugin.prototype.apply = function(compiler) { |
90 | | - compiler.plugin('emit', (compilation, callback) => { |
91 | | - if (this.options.cacheOnWatch && this.cacheOmittedFilename.length) { |
92 | | - this.cacheOmittedFilename.forEach(omitted => { |
93 | | - this.omitFiles(omitted, compilation); |
94 | | - }); |
95 | | - } else { |
96 | | - this.findOmissibleFiles(compilation); |
97 | | - } |
98 | | - callback(); |
99 | | - }); |
| 77 | +OmitJSforCSSPlugin.prototype.apply = function (compiler) { |
| 78 | + compiler.hooks.emit.tap('OmitJSforCSSPlugin', (compilation) => { |
| 79 | + this.findOmissibleFiles(compilation); |
| 80 | + }); |
100 | 81 | }; |
101 | 82 |
|
102 | 83 | module.exports = OmitJSforCSSPlugin; |
0 commit comments