Skip to content

Commit ce9997b

Browse files
committed
[FORK] Support Webpack 5 only.
- renaming of plugin - update all packages for Webpack 5 - fix deprecation messages in Webpack 5 - fix integration tests for Webpack 5 - some improvements
1 parent 2b62834 commit ce9997b

File tree

23 files changed

+1349
-3829
lines changed

23 files changed

+1349
-3829
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.idea
2-
_ignored
3-
node_modules
2+
node_modules
3+
test/outputs

CHANGELOG.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
1+
## 0.6.1 (Oct 20, 2020)
2+
The fork of original `https://github.com/fqborges/webpack-fix-style-only-entries` (ver. 0.6.0) for support only Webpack 5 and above.
3+
The Webpack 4 is no longer supported.
4+
5+
The changes from original version 0.6.0:
6+
7+
- Added the feature: considers the use hash after the .js and .mjs extension in file format like `.js?[hash]` or `.mjs?[hash]`.
8+
The idea and requirement belong to https://github.com/MatiasMorici from PR https://github.com/fqborges/webpack-fix-style-only-entries/pull/27/commits/57eeecbcb85926578770c3845465e4012b02a196
9+
- In Webpack 5 fixed deprecation messages:
10+
- DEP_WEBPACK_CHUNK_HAS_ENTRY_MODULE
11+
- DEP_WEBPACK_CHUNK_ENTRY_MODULE
12+
- DEP_WEBPACK_DEPRECATION_ARRAY_TO_SET
13+
- DEP_WEBPACK_MODULE_INDEX
14+
15+
- Update all packages for Webpack 5.
16+
- Fix integration test script for using with Webpack 5.
17+
- Fix a BUG of `terser-webpack-plugin` (generate the futile file `vendor.js.LICENSE.txt`) in the production test `vendor+multi-js-entry-css-entry`.
18+
- Fix module structure in README.md.
19+
120
## 0.6.0 (Oct 13, 2020)
2-
Being overly careful here, this version is not breaking for almost all the existing users. It could possibly break in some edge cases, since it changes how modules are collected (from global to one each compilation) or if you have a workaround for a working webpack multi configuration.
3-
* BREAKING (POSSIBLY): Use a dedicated cache for every compilation (Prevent arbitrary files deletion when using Webpack with multi configurations) (PR [#39](https://github.com/fqborges/webpack-fix-style-only-entries/pull/39))
21+
Being overly careful here, this version is not breaking for almost all the existing users. It could possibly break in some edge cases, since it changes how modules are collected (from global to one each compilation) or if you have a workaround for a working webpack multi configuration.
22+
- BREAKING (POSSIBLY): Use a dedicated cache for every compilation (Prevent arbitrary files deletion when using Webpack with multi configurations) (PR [#39](https://github.com/fqborges/webpack-fix-style-only-entries/pull/39))
423

524
## 0.5.2 (Oct 07, 2020)
6-
* Webpack 5 support using ModuleGraph API (PR [#38](https://github.com/fqborges/webpack-fix-style-only-entries/pull/38))
7-
* npm audit fix: ([ea9dd7](https://github.com/fqborges/webpack-fix-style-only-entries/commit/ea9dd7bd7ade5b16623064a4850de39545e1e18e))
25+
- Webpack 5 support using ModuleGraph API (PR [#38](https://github.com/fqborges/webpack-fix-style-only-entries/pull/38))
26+
- npm audit fix: ([ea9dd7](https://github.com/fqborges/webpack-fix-style-only-entries/commit/ea9dd7bd7ade5b16623064a4850de39545e1e18e))
827

928
## 0.5.1 (Jun 13, 2020)
10-
* Fix Maximum call stack size exceeded (PR [#34](https://github.com/fqborges/webpack-fix-style-only-entries/pull/34))
11-
* Added CHANGELOG ([3e3767](https://github.com/fqborges/webpack-fix-style-only-entries/commit/3e3767d8998a53d807b5d5b10643d05b800aa79a))
29+
- Fix Maximum call stack size exceeded (PR [#34](https://github.com/fqborges/webpack-fix-style-only-entries/pull/34))
30+
- Added CHANGELOG ([3e3767](https://github.com/fqborges/webpack-fix-style-only-entries/commit/3e3767d8998a53d807b5d5b10643d05b800aa79a))
1231

1332
## 0.5.0 (May 18, 2020)
14-
15-
* fix for [#23](https://github.com/fqborges/webpack-fix-style-only-entries/issues/23) ([37d350](https://github.com/fqborges/webpack-fix-style-only-entries/commit/37d350eec83f49c0b12729a93aa6cf2f96d92d0b))
16-
* fix for [#24](https://github.com/fqborges/webpack-fix-style-only-entries/issues/24) ([d92bec](https://github.com/fqborges/webpack-fix-style-only-entries/commit/d92bec4be5fe4b97a8651a9206fa2281ce1424be))
17-
* BREAKING: added styl and sass to default extensions ([247a5c](https://github.com/fqborges/webpack-fix-style-only-entries/commit/247a5c9f963e4d7598539056ab3f709b0558b4ec))
18-
* added LICENSE.txt ([220e20](https://github.com/fqborges/webpack-fix-style-only-entries/commit/220e20eeb9bc95652a942812a424aadd92ec7d1f))
33+
- fix for [#23](https://github.com/fqborges/webpack-fix-style-only-entries/issues/23) ([37d350](https://github.com/fqborges/webpack-fix-style-only-entries/commit/37d350eec83f49c0b12729a93aa6cf2f96d92d0b))
34+
- fix for [#24](https://github.com/fqborges/webpack-fix-style-only-entries/issues/24) ([d92bec](https://github.com/fqborges/webpack-fix-style-only-entries/commit/d92bec4be5fe4b97a8651a9206fa2281ce1424be))
35+
- BREAKING: added styl and sass to default extensions ([247a5c](https://github.com/fqborges/webpack-fix-style-only-entries/commit/247a5c9f963e4d7598539056ab3f709b0558b4ec))
36+
- added LICENSE.txt ([220e20](https://github.com/fqborges/webpack-fix-style-only-entries/commit/220e20eeb9bc95652a942812a424aadd92ec7d1f))
1937

2038
## 0.4.0 (Sep 8, 2019)
21-
* feat: add support for module js files (PR [#21](https://github.com/fqborges/webpack-fix-style-only-entries/pull/21))
22-
39+
- feat: add support for module js files (PR [#21](https://github.com/fqborges/webpack-fix-style-only-entries/pull/21))

LICENSE.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ISC License
22

3-
Copyright (c) 2020, fqborges
3+
Copyright (c) 20218-2020, fqborges
4+
Copyright (c) 2020, webdiscus
45

56
Permission to use, copy, modify, and/or distribute this software for any
67
purpose with or without fee is hereby granted, provided that the above

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@ module.exports = {
7070
Give an especial extension to your file (`.css.js` for example) and configure `new FixStyleOnlyEntriesPlugin({ extensions:['css.js'] })`. See: https://github.com/fqborges/webpack-fix-style-only-entries/issues/8.
7171

7272
### I use webpack-hot-middleware:
73-
Configure this plugin as `new FixStyleOnlyEntriesPlugin({ ignore: 'webpack-hot-middleware' })`. See: https://github.com/fqborges/webpack-fix-style-only-entries/issues/12 and https://github.com/fqborges/webpack-fix-style-only-entries/blob/master/test/cases/css-entry-with-ignored-hmr/webpack.config.js.
73+
Configure this plugin as `new FixStyleOnlyEntriesPlugin({ ignore: 'webpack-hot-middleware' })`. See: https://github.com/fqborges/webpack-fix-style-only-entries/issues/12 and https://github.com/fqborges/webpack-fix-style-only-entries/blob/master/test/cases/css-entry-with-ignored-hmr/webpack.config.js.

index.js

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,64 @@
1-
const NAME = "webpack-fix-style-only-entries";
1+
/**
2+
* Webpack 5 plugin to remove empty scripts generated by usage only style in entries.
3+
*/
4+
5+
const NAME = 'webpack-remove-empty-scripts';
26

37
const defaultOptions = {
4-
extensions: ["less", "scss", "css", "sass", "styl"],
8+
extensions: ['css', 'scss', 'sass', 'less', 'styl'],
9+
scriptExtensions: ['js', 'mjs'],
510
silent: false,
611
ignore: undefined,
712
};
813

9-
class WebpackFixStyleOnlyEntriesPlugin {
14+
class WebpackRemoveEmptyScriptsPlugin {
1015
constructor(options) {
1116
this.apply = this.apply.bind(this);
12-
1317
this.options = Object.assign({}, defaultOptions, options);
1418
}
1519

1620
apply(compiler) {
1721
const extensionsWithoutDots = this.options.extensions.map(e =>
18-
e[0] === "." ? e.substring(1) : e
22+
e[0] === '.' ? e.substring(1) : e
1923
);
2024

2125
const patternOneOfExtensions = extensionsWithoutDots
22-
.map(ext => escapeRegExp(ext))
23-
.join("|");
26+
.map(ext => escapeRegExp(ext))
27+
.join('|');
2428

2529
const reStylesResource = new RegExp(
26-
`[.](${patternOneOfExtensions})([?].*)?$`
30+
`[.](${patternOneOfExtensions})([?].*)?$`
2731
);
2832

2933
compiler.hooks.compilation.tap(NAME, compilation => {
3034
const resourcesCache = [];
35+
3136
compilation.hooks.chunkAsset.tap(NAME, (chunk, file) => {
32-
if (!file.endsWith(".js") && !file.endsWith(".mjs")) return;
33-
if (!chunk.hasEntryModule()) return;
37+
const isNotScript = defaultOptions.scriptExtensions.every((ext) => file.lastIndexOf('.' + ext) < 0);
38+
if (isNotScript) return;
39+
40+
// has entry modules
41+
if (compilation.chunkGraph.getNumberOfEntryModules(chunk) < 1) return;
42+
const entryModules = Array.from(compilation.chunkGraph.getChunkEntryModulesIterable(chunk));
43+
if (entryModules.length < 1) return;
44+
45+
const entryModule = entryModules[0];
46+
const entryResources = collectEntryResources(compilation, entryModule, resourcesCache);
3447

35-
const rawResources = collectEntryResources(compilation, chunk.entryModule, resourcesCache);
3648
const resources = this.options.ignore
37-
? rawResources.filter(r => !r.match(this.options.ignore))
38-
: rawResources;
49+
? entryResources.filter(res => !res.match(this.options.ignore))
50+
: entryResources;
3951

4052
const isStyleOnly =
41-
resources.length &&
42-
resources.every(resource => reStylesResource.test(resource));
53+
resources.length &&
54+
resources.every(resource => reStylesResource.test(resource));
55+
4356
if (isStyleOnly) {
4457
if (!this.options.silent) {
45-
console.error(
46-
"webpack-fix-style-only-entries: removing js from style only module: " +
47-
file
48-
);
58+
console.log('[remove-empty-scripts] remove empty js from style only entry: ' + file);
4959
}
50-
chunk.files = chunk.files.filter(f => f != file);
60+
61+
chunk.files.delete(file);
5162
delete compilation.assets[file];
5263
}
5364
});
@@ -56,48 +67,55 @@ class WebpackFixStyleOnlyEntriesPlugin {
5667
}
5768

5869
function collectEntryResources(compilation, module, cache) {
59-
// module.index is unique per compilation
70+
const index = compilation.moduleGraph.getPreOrderIndex(module),
71+
resources = [];
72+
73+
// index of module is unique per compilation
6074
// module.id can be null, not used here
61-
if (cache[module.index] !== undefined) {
62-
return cache[module.index];
75+
if (cache[index] !== undefined) {
76+
77+
return cache[index];
6378
}
6479

65-
if (typeof module.resource == "string") {
80+
if (typeof module.resource == 'string') {
6681
const resources = [module.resource];
67-
cache[module.index] = resources;
82+
cache[index] = resources;
83+
6884
return resources;
6985
}
7086

71-
const resources = [];
7287
if (module.dependencies) {
73-
const hasModuleGraphSupport = compilation.hasOwnProperty('moduleGraph');
7488
module.dependencies.forEach(dep => {
7589
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;
90+
const module = compilation.moduleGraph.getModule(dep),
91+
originModule = compilation.moduleGraph.getParentModule(dep),
92+
nextModule = module || originModule;
93+
7994
if (nextModule) {
8095
const depResources = collectEntryResources(compilation, nextModule, cache);
96+
8197
for (let index = 0, length = depResources.length; index !== length; index++) {
8298
resources.push(depResources[index]);
8399
}
84100
}
85101
}
86102
});
87103
}
104+
cache[index] = resources;
88105

89-
cache[module.index] = resources;
90106
return resources;
91107
}
92108

93109
// https://github.com/lodash/lodash/blob/4.17.11/lodash.js#L14274
94110
const reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
95111
const reHasRegExpChar = RegExp(reRegExpChar.source);
112+
96113
function escapeRegExp(string) {
97114
string = String(string);
115+
98116
return string && reHasRegExpChar.test(string)
99-
? string.replace(reRegExpChar, "\\$&")
100-
: string;
117+
? string.replace(reRegExpChar, '\\$&')
118+
: string;
101119
}
102120

103-
module.exports = WebpackFixStyleOnlyEntriesPlugin;
121+
module.exports = WebpackRemoveEmptyScriptsPlugin;

0 commit comments

Comments
 (0)