Skip to content

Commit

Permalink
split Chunk.isInitial into isOnlyInitial and canBeInitial
Browse files Browse the repository at this point in the history
remove includeInitial argument of getChunk(Module)Maps
  • Loading branch information
sokra committed Jan 22, 2018
1 parent 6bd1240 commit ea60937
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 130 deletions.
2 changes: 1 addition & 1 deletion lib/BannerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class BannerPlugin {
compiler.hooks.compilation.tap("BannerPlugin", (compilation) => {
compilation.hooks.optimizeChunkAssets.tap("BannerPlugin", (chunks) => {
chunks.forEach((chunk) => {
if(options.entryOnly && !chunk.isInitial()) return;
if(options.entryOnly && !chunk.canBeInitial()) return;
chunk.files
.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options))
.forEach((file) => {
Expand Down
67 changes: 38 additions & 29 deletions lib/Chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ class Chunk {
}

get initial() {
throw new Error("Chunk.initial was removed. Use isInitial()");
throw new Error("Chunk.initial was removed. Use canBeInitial/isOnlyInitial()");
}

set initial(data) {
throw new Error("Chunk.initial was removed. Use isInitial()");
throw new Error("Chunk.initial was removed. Use canBeInitial/isOnlyInitial()");
}

hasRuntime() {
Expand All @@ -84,14 +84,23 @@ class Chunk {
return false;
}

isInitial() {
canBeInitial() {
for(const chunkGroup of this._groups) {
// We only need to check the first one
return chunkGroup.isInitial();
if(chunkGroup.isInitial())
return true;
}
return false;
}

isOnlyInitial() {
if(this._groups.size <= 0) return false;
for(const chunkGroup of this._groups) {
if(!chunkGroup.isInitial())
return false;
}
return true;
}

hasEntryModule() {
return !!this.entryModule;
}
Expand Down Expand Up @@ -263,10 +272,10 @@ class Chunk {
}
return true;
};
if(this.isInitial() !== otherChunk.isInitial()) {
if(this.isInitial()) {
if(this.hasRuntime() !== otherChunk.hasRuntime()) {
if(this.hasRuntime()) {
return isAvailable(this, otherChunk);
} else if(otherChunk.isInitial()) {
} else if(otherChunk.hasRuntime()) {
return isAvailable(otherChunk, this);
} else {
return false;
Expand All @@ -279,7 +288,7 @@ class Chunk {

addMultiplierAndOverhead(size, options) {
const overhead = typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
const multiplicator = this.isInitial() ? (options.entryChunkMultiplicator || 10) : 1;
const multiplicator = this.canBeInitial() ? (options.entryChunkMultiplicator || 10) : 1;

return size * multiplicator + overhead;
}
Expand Down Expand Up @@ -317,22 +326,33 @@ class Chunk {
this.sortModules();
}

getChunkMaps(includeInitial, realHash) {
const chunkHashMap = Object.create(null);
const chunkNameMap = Object.create(null);

getAllAsyncChunks() {
const initialChunks = new Set();
const queue = new Set(this.groupsIterable);
const chunks = new Set();

for(const chunkGroup of queue) {
if(includeInitial || !chunkGroup.isInitial())
for(const chunk of chunkGroup.chunks)
for(const chunk of chunkGroup.chunks)
initialChunks.add(chunk);
}

for(const chunkGroup of queue) {
for(const chunk of chunkGroup.chunks) {
if(!initialChunks.has(chunk))
chunks.add(chunk);
}
for(const child of chunkGroup.childrenIterable)
queue.add(child);
}

for(const chunk of chunks) {
return chunks;
}

getChunkMaps(realHash) {
const chunkHashMap = Object.create(null);
const chunkNameMap = Object.create(null);

for(const chunk of this.getAllAsyncChunks()) {
chunkHashMap[chunk.id] = realHash ? chunk.hash : chunk.renderedHash;
if(chunk.name)
chunkNameMap[chunk.id] = chunk.name;
Expand All @@ -344,22 +364,11 @@ class Chunk {
};
}

getChunkModuleMaps(includeInitial, filterFn) {
getChunkModuleMaps(filterFn) {
const chunkModuleIdMap = Object.create(null);
const chunkModuleHashMap = Object.create(null);

const queue = new Set(this.groupsIterable);
const chunks = new Set();

for(const chunkGroup of queue) {
if(includeInitial || !chunkGroup.isInitial())
for(const chunk of chunkGroup.chunks)
chunks.add(chunk);
for(const child of chunkGroup.childrenIterable)
queue.add(child);
}

for(const chunk of chunks) {
for(const chunk of this.getAllAsyncChunks()) {
let array;
for(const module of chunk.modulesIterable) {
if(filterFn(module)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/FlagInitialModulesAsUsedPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FlagInitialModulesAsUsedPlugin {
compiler.hooks.compilation.tap("FlagInitialModulesAsUsedPlugin", (compilation) => {
compilation.hooks.afterOptimizeChunks.tap("FlagInitialModulesAsUsedPlugin", (chunks) => {
chunks.forEach((chunk) => {
if(!chunk.isInitial()) {
if(!chunk.isOnlyInitial()) {
return;
}
for(const module of chunk.modulesIterable) {
Expand Down
2 changes: 1 addition & 1 deletion lib/LibManifestPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class LibManifestPlugin {
apply(compiler) {
compiler.hooks.emit.tapAsync("LibManifestPlugin", (compilation, callback) => {
asyncLib.forEach(compilation.chunks, (chunk, callback) => {
if(!chunk.isInitial()) {
if(!chunk.isOnlyInitial()) {
callback();
return;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class Stats {
message: e
};
if(e.chunk) {
text += `chunk ${e.chunk.name || e.chunk.id}${e.chunk.hasRuntime() ? " [entry]" : e.chunk.isInitial() ? " [initial]" : ""}\n`;
text += `chunk ${e.chunk.name || e.chunk.id}${e.chunk.hasRuntime() ? " [entry]" : e.chunk.canBeInitial() ? " [initial]" : ""}\n`;
}
if(e.file) {
text += `${e.file}\n`;
Expand Down Expand Up @@ -431,7 +431,7 @@ class Stats {
const obj = {
id: chunk.id,
rendered: chunk.rendered,
initial: chunk.isInitial(),
initial: chunk.canBeInitial(),
entry: chunk.hasRuntime(),
recorded: chunk.recorded,
reason: chunk.chunkReason,
Expand Down
4 changes: 2 additions & 2 deletions lib/TemplatedPathPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ class TemplatedPathPlugin {
const outputOptions = mainTemplate.outputOptions;
const chunkFilename = outputOptions.chunkFilename || outputOptions.filename;
if(REGEXP_CHUNKHASH_FOR_TEST.test(chunkFilename))
hash.update(JSON.stringify(chunk.getChunkMaps(false, true).hash));
hash.update(JSON.stringify(chunk.getChunkMaps(true).hash));
if(REGEXP_NAME_FOR_TEST.test(chunkFilename))
hash.update(JSON.stringify(chunk.getChunkMaps(false, true).name));
hash.update(JSON.stringify(chunk.getChunkMaps(true).name));
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/node/ReadFileCompileWasmMainTemplatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ReadFileCompileWasmMainTemplatePlugin {
});
mainTemplate.hooks.requireEnsure.tap("ReadFileCompileWasmMainTemplatePlugin", (source, chunk, hash) => {
const webassemblyModuleFilename = mainTemplate.outputOptions.webassemblyModuleFilename;
const chunkModuleMaps = chunk.getChunkModuleMaps(false, m => m.type.startsWith("webassembly"));
const chunkModuleMaps = chunk.getChunkModuleMaps(m => m.type.startsWith("webassembly"));
if(Object.keys(chunkModuleMaps.id).length === 0) return source;
const wasmModuleSrcPath = mainTemplate.getAssetPath(JSON.stringify(webassemblyModuleFilename), {
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
Expand Down
4 changes: 2 additions & 2 deletions lib/optimize/AggressiveMergingPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class AggressiveMergingPlugin {
compilation.hooks.optimizeChunksAdvanced.tap("AggressiveMergingPlugin", (chunks) => {
let combinations = [];
chunks.forEach((a, idx) => {
if(a.isInitial()) return;
if(a.canBeInitial()) return;
for(let i = 0; i < idx; i++) {
const b = chunks[i];
if(b.isInitial()) continue;
if(b.canBeInitial()) continue;
combinations.push({
a,
b,
Expand Down
2 changes: 1 addition & 1 deletion lib/optimize/MergeDuplicateChunksPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MergeDuplicateChunksPlugin {
// when we found duplicates
if(possibleDuplicates !== undefined && possibleDuplicates.size > 0) {
for(const otherChunk of possibleDuplicates) {
if(otherChunk.isInitial() !== chunk.isInitial()) continue;
if(otherChunk.hasRuntime() !== chunk.hasRuntime()) continue;
// merge them
if(chunk.integrate(otherChunk, "duplicate"))
chunks.splice(chunks.indexOf(otherChunk), 1);
Expand Down
2 changes: 1 addition & 1 deletion lib/optimize/OccurrenceOrderPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class OccurrenceOrderPlugin {
let initial = 0;
let entry = 0;
m.forEachChunk(c => {
if(c.isInitial()) initial++;
if(c.canBeInitial()) initial++;
if(c.entryModule === m) entry++;
});
initialChunkChunkMap.set(m, initial);
Expand Down
8 changes: 4 additions & 4 deletions lib/optimize/SplitChunksPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ module.exports = class SplitChunksPlugin {
reuseExistingChunk: cacheGroupSource.reuseExistingChunk
};
// Select chunks by configuration
const selectedChunks = cacheGroup.chunks === "initial" ? chunks.filter(chunk => chunk.isInitial()) :
cacheGroup.chunks === "async" ? chunks.filter(chunk => !chunk.isInitial()) :
const selectedChunks = cacheGroup.chunks === "initial" ? chunks.filter(chunk => chunk.canBeInitial()) :
cacheGroup.chunks === "async" ? chunks.filter(chunk => !chunk.canBeInitial()) :
chunks;
// Get indices of chunks in which this module occurs
const chunkIndices = selectedChunks.map(chunk => indexMap.get(chunk));
Expand Down Expand Up @@ -304,8 +304,8 @@ module.exports = class SplitChunksPlugin {
// skip if we address ourself
if(chunk.name === chunkName || chunk === newChunk) continue;
// respect max requests when not enforced
const maxRequests = chunk.isInitial() ?
item.cacheGroup.maxInitialRequests :
const maxRequests = chunk.isOnlyInitial() ? item.cacheGroup.maxInitialRequests :
chunk.canBeInitial() ? Math.min(item.cacheGroup.maxInitialRequests, item.cacheGroup.maxAsyncRequests) :
item.cacheGroup.maxAsyncRequests;
if(isFinite(maxRequests) && getRequests(chunk) >= maxRequests) continue;
if(newChunk === undefined) {
Expand Down
2 changes: 1 addition & 1 deletion lib/performance/SizeLimitsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ module.exports = class SizeLimitsPlugin {
}

if(warnings.length > 0) {
const hasAsyncChunks = compilation.chunks.filter(chunk => !chunk.isInitial()).length > 0;
const hasAsyncChunks = compilation.chunks.filter(chunk => !chunk.canBeInitial()).length > 0;

if(!hasAsyncChunks) {
warnings.push(new NoAsyncChunksWarning());
Expand Down
2 changes: 1 addition & 1 deletion lib/wasm/WasmModuleTemplatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class WasmModuleTemplatePlugin {
chunk
}) => {
if(module.type && module.type.startsWith("webassembly")) {
if(chunk.isInitial())
if(chunk.canBeInitial())
throw new Error("Sync WebAsssmbly compilation is not yet implemented");
const generateExports = () => {
if(Array.isArray(module.buildMeta.providedExports) && Array.isArray(module.usedExports)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/web/FetchCompileWasmMainTemplatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FetchCompileWasmMainTemplatePlugin {
});
mainTemplate.hooks.requireEnsure.tap("FetchCompileWasmMainTemplatePlugin", (source, chunk, hash) => {
const webassemblyModuleFilename = mainTemplate.outputOptions.webassemblyModuleFilename;
const chunkModuleMaps = chunk.getChunkModuleMaps(false, m => m.type.startsWith("webassembly"));
const chunkModuleMaps = chunk.getChunkModuleMaps(m => m.type.startsWith("webassembly"));
if(Object.keys(chunkModuleMaps.id).length === 0) return source;
const wasmModuleSrcPath = mainTemplate.getAssetPath(JSON.stringify(webassemblyModuleFilename), {
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
Expand Down
6 changes: 3 additions & 3 deletions test/Chunk.unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("Chunk", () => {

it("returns a string with modules information", () => should(ChunkInstance.toString()).be.exactly("Chunk[]"));

it("should not be the initial instance", () => should(ChunkInstance.isInitial()).be.false());
it("should not be the initial instance", () => should(ChunkInstance.canBeInitial()).be.false());

describe("entry", () => {
it("returns an error if get entry", () =>
Expand All @@ -32,12 +32,12 @@ describe("Chunk", () => {
it("returns an error if get initial", () =>
should(() => {
ChunkInstance.initial;
}).throw("Chunk.initial was removed. Use isInitial()"));
}).throw("Chunk.initial was removed. Use canBeInitial/isOnlyInitial()"));

it("returns an error if set an initial", () =>
should(() => {
ChunkInstance.initial = 10;
}).throw("Chunk.initial was removed. Use isInitial()"));
}).throw("Chunk.initial was removed. Use canBeInitial/isOnlyInitial()"));
});

describe("hasRuntime", () => {
Expand Down
Loading

0 comments on commit ea60937

Please sign in to comment.