Skip to content

Commit 35e77b1

Browse files
committed
[BUGFIX] the issue infinite recursion by collect of resources from dependency modules by usage in react app some big components with many thousands dependencies
1 parent a928bfa commit 35e77b1

File tree

12 files changed

+3717
-13
lines changed

12 files changed

+3717
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
## 0.7.1 (Jan 14, 2021)
2+
### Bugfixes
3+
- The issue infinite recursion by collect of resources from dependency modules by usage in react app some big components with many thousands dependencies.
4+
15
## 0.7.0 (Dec 21, 2020)
26
### Breaking change
37
- 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-
8+
59
### Bugfixes
610
- The issue `Maximum call stack size exceeded` was general fixed in all cases, for example, by usage the webpack setting `optimization.concatenateModules: true` and:
711
- import react

index.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ const defaultOptions = {
1111
ignore: [],
1212
};
1313

14-
// Save last dependency to compare it with dependency of next module
14+
// Save unique id in dependency object as marker of 'analysed module'
1515
// to avoid the infinite recursion by collect of resources.
16-
let lastDependency = '';
16+
let dependencyId = 1;
1717

1818
class WebpackRemoveEmptyScriptsPlugin {
1919
constructor(options) {
@@ -91,8 +91,9 @@ class WebpackRemoveEmptyScriptsPlugin {
9191
}
9292

9393
function collectEntryResources(compilation, module, cache) {
94-
const moduleGraph = compilation.moduleGraph;
95-
const index = moduleGraph.getPreOrderIndex(module),
94+
const moduleGraph = compilation.moduleGraph,
95+
index = moduleGraph.getPreOrderIndex(module),
96+
propNameDependencyId = '__dependencyWebpackRemoveEmptyScriptsUniqueId',
9697
resources = [];
9798

9899
// the index can be null
@@ -115,12 +116,19 @@ function collectEntryResources(compilation, module, cache) {
115116

116117
if (module.dependencies) {
117118
module.dependencies.forEach(dependency => {
119+
118120
let module = moduleGraph.getModule(dependency),
119121
originModule = moduleGraph.getParentModule(dependency),
120122
nextModule = module || originModule,
121-
useNextModule = JSON.stringify(dependency) !== lastDependency;
123+
useNextModule = false;
124+
125+
if (!dependency.hasOwnProperty(propNameDependencyId)) {
126+
dependency[propNameDependencyId] = dependencyId++;
127+
useNextModule = true;
128+
}
122129

123-
lastDependency = JSON.stringify(dependency);
130+
// debug info
131+
//console.log('::: module ::: ', useNextModule ? '' : '-----', dependency[propNameDependencyId]);
124132

125133
if (nextModule && useNextModule) {
126134
const dependencyResources = collectEntryResources(compilation, nextModule, cache);

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-remove-empty-scripts",
3-
"version": "0.7.0",
3+
"version": "0.7.1",
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": [
@@ -34,13 +34,13 @@
3434
"css-loader": "^5.0.1",
3535
"fs": "^0.0.1-security",
3636
"jest": "^26.6.3",
37-
"mini-css-extract-plugin": "^1.3.3",
37+
"mini-css-extract-plugin": "^1.3.4",
3838
"moment": "^2.29.1",
3939
"rimraf": "^3.0.2",
40-
"terser-webpack-plugin": "^5.0.3",
41-
"webpack": "^5.11.0",
40+
"terser-webpack-plugin": "^5.1.1",
41+
"webpack": "^5.14.0",
4242
"webpack-hot-middleware": "^2.25.0",
43-
"webpack-merge": "^5.7.2"
43+
"webpack-merge": "^5.7.4"
4444
},
4545
"bugs": {
4646
"url": "https://github.com/webdiscus/webpack-remove-empty-scripts/issues"

test/issue-webpack-concatenateModules/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ where not exist a `name` or a `require`:
1414

1515
If occur like it structure, then not analyse for a resource, skip it.
1616

17-
### Manual test (issure is fixed)
17+
### Manual test (issue is fixed)
1818

1919
Initial, run `npm install` to install dependencies.
2020

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": [
3+
"@babel/preset-env",
4+
"@babel/preset-react"
5+
],
6+
"plugins": [
7+
]
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Fix the issue by concatenateModules = true in react application
2+
3+
Some big react components, such as `@material-ui/data-grid` from `material-ui.com` has more thousands dependencies (over 4000).
4+
In any combination of imports from same components come out the issue: _infinite recursion by collect of resources from dependency modules_.
5+
6+
### Manual test (issue is fixed)
7+
8+
Initial, run `npm install` to install dependencies.
9+
10+
Build in production mode with webpack config `optimization.concatenateModules: true`
11+
12+
run `./webpack-build.sh`
13+
14+
15+
Build in development mode with webpack config `optimization.concatenateModules: true`
16+
17+
run `./webpack-build-dev.sh`

0 commit comments

Comments
 (0)