Skip to content

Commit bc6a6ad

Browse files
committed
cleaned up a few functions
1 parent 15d6a5e commit bc6a6ad

1 file changed

Lines changed: 9 additions & 23 deletions

File tree

‎lib/optimize/CommonsChunkPlugin.js‎

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,19 @@ The available options are:
4848
const selectedChunks = this.selectedChunks;
4949
const asyncOption = this.async;
5050
const minSize = this.minSize;
51-
const ident = this.ident;
51+
const optimized = new WeakSet();
5252
compiler.plugin("this-compilation", (compilation) => {
5353
compilation.plugin(["optimize-chunks", "optimize-extracted-chunks"], (chunks) => {
5454
// only optimize once
55-
if(compilation[ident]) return;
56-
compilation[ident] = true;
55+
if(optimized.has(compilation)) return;
56+
optimized.add(compilation)
5757

5858
let commonChunks;
5959
if(!chunkNames && (selectedChunks === false || asyncOption)) {
6060
commonChunks = chunks;
6161
} else if(Array.isArray(chunkNames) || typeof chunkNames === "string") {
62-
commonChunks = [].concat(chunkNames).map((chunkName) => {
63-
let chunk = chunks.filter((chunk) => {
64-
return chunk.name === chunkName;
65-
})[0];
66-
if(!chunk) {
67-
chunk = compilation.addChunk(chunkName);
68-
}
69-
return chunk;
70-
}, this);
62+
commonChunks = [].concat(chunkNames).map(chunkName =>
63+
chunks.find(chunk => chunk.name === chunkName) || compilation.addChunk(chunkName));
7164
} else {
7265
throw new Error("Invalid chunkNames argument");
7366
}
@@ -79,20 +72,15 @@ The available options are:
7972
return selectedChunks.indexOf(chunk.name) >= 0;
8073
});
8174
} else if(selectedChunks === false || asyncOption) {
82-
usedChunks = (commonChunk.chunks || []).filter((chunk) => {
75+
usedChunks = (commonChunk.chunks || []).filter((chunk) =>
8376
// we can only move modules from this chunk if the "commonChunk" is the only parent
84-
return asyncOption || chunk.parents.length === 1;
85-
});
77+
asyncOption || chunk.parents.length === 1);
8678
} else {
8779
if(commonChunk.parents.length > 0) {
8880
compilation.errors.push(new Error("CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (" + commonChunk.name + ")"));
8981
return;
9082
}
91-
usedChunks = chunks.filter((chunk) => {
92-
const found = commonChunks.indexOf(chunk);
93-
if(found >= idx) return false;
94-
return chunk.hasRuntime();
95-
});
83+
usedChunks = chunks.filter(chunk => commonChunks.indexOf(chunk) < idx && chunk.hasRuntime())
9684
}
9785
let asyncChunk;
9886
if(asyncOption) {
@@ -133,9 +121,7 @@ The available options are:
133121
});
134122
}
135123
if(minSize) {
136-
const size = reallyUsedModules.reduce((a, b) => {
137-
return a + b.size();
138-
}, 0);
124+
const size = reallyUsedModules.reduce((a, b) => a + b.size(), 0);
139125
if(size < minSize)
140126
return;
141127
}

0 commit comments

Comments
 (0)