Skip to content

Commit a928bfa

Browse files
committed
[TASK] improvement for next minor version
1 parent 0e9aa5a commit a928bfa

File tree

25 files changed

+138
-75
lines changed

25 files changed

+138
-75
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 0.7.0 (Dec 21, 2020)
2+
### Breaking change
3+
- The `silent` option is deprecated and will be removed on Juni 30, 2021. Use option `verbose: true` to show in console each removed empty file. Defaults, `verbose: false`.
4+
5+
### Bugfixes
6+
- The issue `Maximum call stack size exceeded` was general fixed in all cases, for example, by usage the webpack setting `optimization.concatenateModules: true` and:
7+
- import react
8+
- import redux
9+
- webpack setting `externals.jquery: 'jQuery'` or other external libs
10+
- The issue if first in webpack entries are a styles and then a scripts.
11+
112
## 0.6.4 (Dec 19, 2020)
213
- Bugfix the error: `Maximum call stack size exceeded` with webpack setting `optimization.concatenateModules: true`and usage in script imports from `react` and `redux`.
314
- Add test case for single style without a scripts in webpack config.

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ module.exports = {
5656

5757
| Name | Type | Default | Description |
5858
|------------|------------------|-----------------------------------------|-------------|
59-
| silent | boolean | false | supress logs to console |
60-
| extensions | Array[string] | ['css', 'scss', 'sass', 'less', 'styl'] | file extensions for styles |
61-
| ignore | string or RegExp or Array[string] or Array[RegExp] | ['/node_modules/'] | match resource path to be ignored, defaults the resources from `node_modules` are ignored|
59+
| verbose | boolean | false | show logs to console |
60+
| extensions | Array[string] | ['css', 'scss', 'sass', 'less', 'styl'] | file extensions for styles |
61+
| ignore | string or RegExp or Array[string] or Array[RegExp] | | match resource path to be ignored |
6262

6363
### Example config:
6464
```JavaScript
65-
// supress logs to console, use it for production
66-
new RemoveEmptyScriptsPlugin({ silent: true })
65+
// show logs to console, use it for development
66+
new RemoveEmptyScriptsPlugin({ verbose: true })
6767
```
6868

6969
```JavaScript

index.js

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,30 @@
55
const NAME = 'webpack-remove-empty-scripts';
66

77
const defaultOptions = {
8+
verbose: false,
89
extensions: ['css', 'scss', 'sass', 'less', 'styl'],
910
scriptExtensions: ['js', 'mjs'],
10-
silent: false,
11-
ignore: [
12-
'/node_modules/'
13-
],
11+
ignore: [],
1412
};
1513

14+
// Save last dependency to compare it with dependency of next module
15+
// to avoid the infinite recursion by collect of resources.
16+
let lastDependency = '';
17+
1618
class WebpackRemoveEmptyScriptsPlugin {
1719
constructor(options) {
1820
this.apply = this.apply.bind(this);
1921
this.options = Object.assign({}, defaultOptions, options);
20-
this.options.ignore = defaultOptions.ignore;
2122

22-
// default ignore resource plus customer ignores
23-
if (options && options.hasOwnProperty('ignore')) {
24-
let optionIgnore = Array.isArray(options.ignore)
25-
? options.ignore
26-
: [options.ignore];
23+
// Deprecation of option `silent`.
24+
if (options && options.hasOwnProperty('silent')) {
25+
this.options.verbose = !options.silent;
26+
console.warn('[DEPRECATION] the `silent` option is deprecated and will be removed on Juni 30, 2021. Use option `verbose: true` to show in console each removed empty file. Defaults, `verbose: false`.')
27+
}
2728

28-
this.options.ignore = this.options.ignore.concat(optionIgnore);
29+
// if by assigned option the `ignore` was not array, then set as array
30+
if (!Array.isArray(this.options.ignore)) {
31+
this.options.ignore = [this.options.ignore];
2932
}
3033
}
3134

@@ -51,13 +54,20 @@ class WebpackRemoveEmptyScriptsPlugin {
5154
const isNotScript = defaultOptions.scriptExtensions.every((ext) => file.lastIndexOf('.' + ext) < 0);
5255
if (isNotScript) return;
5356

54-
// has entry modules
55-
if (compilation.chunkGraph.getNumberOfEntryModules(chunk) < 1) return;
56-
const entryModules = Array.from(compilation.chunkGraph.getChunkEntryModulesIterable(chunk));
57-
if (entryModules.length < 1) return;
57+
const chunkGraph = compilation.chunkGraph;
58+
let entryResources = [];
59+
60+
for (const module of chunkGraph.getChunkEntryModulesIterable(chunk)) {
61+
if (!compilation.modules.has(module)) {
62+
throw new Error(
63+
"checkConstraints: entry module in chunk but not in compilation " +
64+
` ${chunk.debugId} ${module.debugId}`
65+
);
66+
}
5867

59-
const entryModule = entryModules[0];
60-
const entryResources = collectEntryResources(compilation, entryModule, resourcesCache);
68+
const moduleResources = collectEntryResources(compilation, module, resourcesCache);
69+
entryResources = entryResources.concat(moduleResources);
70+
}
6171

6272
const resources = customIgnore.length > 0
6373
? entryResources.filter(res => customIgnore.every(ignore => !res.match(ignore)))
@@ -68,8 +78,8 @@ class WebpackRemoveEmptyScriptsPlugin {
6878
resources.every(resource => reStylesResource.test(resource));
6979

7080
if (isStyleOnly) {
71-
if (!this.options.silent) {
72-
console.log('[remove-empty-scripts] remove empty js from style only entry: ' + file);
81+
if (this.options.verbose) {
82+
console.log('[remove-empty-scripts] remove empty js file: ' + file);
7383
}
7484

7585
chunk.files.delete(file);
@@ -81,7 +91,8 @@ class WebpackRemoveEmptyScriptsPlugin {
8191
}
8292

8393
function collectEntryResources(compilation, module, cache) {
84-
const index = compilation.moduleGraph.getPreOrderIndex(module),
94+
const moduleGraph = compilation.moduleGraph;
95+
const index = moduleGraph.getPreOrderIndex(module),
8596
resources = [];
8697

8798
// the index can be null
@@ -103,20 +114,21 @@ function collectEntryResources(compilation, module, cache) {
103114
}
104115

105116
if (module.dependencies) {
106-
module.dependencies.forEach(dep => {
107-
if (dep) {
108-
const module = compilation.moduleGraph.getModule(dep),
109-
originModule = compilation.moduleGraph.getParentModule(dep),
110-
nextModule = module || originModule;
111-
112-
if (nextModule && (!dep.hasOwnProperty('decorator') || dep['decorator'].indexOf('__webpack_require__') < 0)) {
113-
const depResources = collectEntryResources(compilation, nextModule, cache);
114-
115-
for (let i = 0, length = depResources.length; i !== length; i++) {
116-
let depFile = depResources[i];
117-
if (resources.indexOf(depFile) < 0) {
118-
resources.push(depFile);
119-
}
117+
module.dependencies.forEach(dependency => {
118+
let module = moduleGraph.getModule(dependency),
119+
originModule = moduleGraph.getParentModule(dependency),
120+
nextModule = module || originModule,
121+
useNextModule = JSON.stringify(dependency) !== lastDependency;
122+
123+
lastDependency = JSON.stringify(dependency);
124+
125+
if (nextModule && useNextModule) {
126+
const dependencyResources = collectEntryResources(compilation, nextModule, cache);
127+
128+
for (let i = 0, length = dependencyResources.length; i !== length; i++) {
129+
const file = dependencyResources[i];
130+
if (resources.indexOf(file) < 0) {
131+
resources.push(file);
120132
}
121133
}
122134
}
@@ -130,7 +142,6 @@ function collectEntryResources(compilation, module, cache) {
130142
return resources;
131143
}
132144

133-
// https://github.com/lodash/lodash/blob/4.17.11/lodash.js#L14274
134145
const reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
135146
const reHasRegExpChar = RegExp(reRegExpChar.source);
136147

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "webpack-remove-empty-scripts",
3-
"version": "0.6.4",
3+
"version": "0.7.0",
44
"description": "Webpack 5 plugin to remove empty scripts generated by usage only style in entries. This is the fork of original plugin https://github.com/fqborges/webpack-fix-style-only-entries (ver. 0.6.0).",
55
"main": "index.js",
66
"files": [
77
"index.js",
8-
"LICENSE.txt"
8+
"LICENSE"
99
],
1010
"scripts": {
1111
"test": "jest --detectOpenHandles"
@@ -50,4 +50,4 @@
5050
"test": "test"
5151
},
5252
"dependencies": {}
53-
}
53+
}

test/cases/css-entry-only/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
],
1414
},
1515
plugins: [
16-
new WebpackRemoveEmptyScripts({ silent: true }),
16+
new WebpackRemoveEmptyScripts(),
1717
new MiniCssExtractPlugin({
1818
filename: "[name].css",
1919
}),

test/cases/css-entry-with-ignored-hmr/webpack.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ module.exports = {
2020
],
2121
},
2222
plugins: [
23-
//new WebpackRemoveEmptyScripts({ silent: true, ignore: "webpack-hot-middleware" }),
24-
new WebpackRemoveEmptyScripts({ silent: true, ignore: "webpack-hot-middleware-qq" }),
23+
new WebpackRemoveEmptyScripts({ ignore: "webpack-hot-middleware" }),
2524
new MiniCssExtractPlugin({
2625
filename: "[name].css",
2726
}),

test/cases/css-entry-with-query/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
],
1414
},
1515
plugins: [
16-
new WebpackRemoveEmptyScripts({ silent: true }),
16+
new WebpackRemoveEmptyScripts(),
1717
new MiniCssExtractPlugin({
1818
filename: "[name].css",
1919
}),

test/cases/multi-configuration/webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const baseConfig = {
1212
],
1313
},
1414
plugins: [
15-
new WebpackRemoveEmptyScripts({ silent: true }),
15+
new WebpackRemoveEmptyScripts(),
1616
new MiniCssExtractPlugin({
1717
filename: "[name].css",
1818
}),
@@ -28,4 +28,4 @@ module.exports = [
2828
entry: { styleB: "./style.css", scriptB: "./script.js" },
2929
...baseConfig,
3030
},
31-
];
31+
];

test/cases/multi-js-entry-css-entry-extensions-do-not-match/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
],
1414
},
1515
plugins: [
16-
new WebpackRemoveEmptyScripts({ silent: true, extensions: ["foo", "bar"] }),
16+
new WebpackRemoveEmptyScripts({ extensions: ["foo", "bar"] }),
1717
new MiniCssExtractPlugin({
1818
filename: "[name].css",
1919
}),

test/cases/multi-js-entry-css-entry-mjs-output-css-output/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = {
1616
],
1717
},
1818
plugins: [
19-
new WebpackRemoveEmptyScripts({ silent: true }),
19+
new WebpackRemoveEmptyScripts(),
2020
new MiniCssExtractPlugin({
2121
filename: "[name].css",
2222
}),

0 commit comments

Comments
 (0)