Skip to content

Commit

Permalink
Move AsyncDependenciesBlock.chunkGroup into ChunkGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Aug 22, 2018
1 parent 3aa2280 commit 670502f
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 79 deletions.
29 changes: 2 additions & 27 deletions lib/AsyncDependenciesBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class AsyncDependenciesBlock extends DependenciesBlock {
groupOptions = { name: undefined };
}
this.groupOptions = groupOptions;
/** @type {ChunkGroup=} */
this.chunkGroup = undefined;
this.loc = loc;
this.request = request;
/** @type {DependenciesBlock} */
Expand Down Expand Up @@ -61,33 +59,10 @@ class AsyncDependenciesBlock extends DependenciesBlock {
*/
updateHash(hash, compilation) {
hash.update(JSON.stringify(this.groupOptions));
hash.update(
(this.chunkGroup &&
this.chunkGroup.chunks
.map(chunk => {
return chunk.id !== null ? chunk.id : "";
})
.join(",")) ||
""
);
const chunkGroup = compilation.chunkGraph.getBlockChunkGroup(this);
hash.update(chunkGroup ? chunkGroup.id : "");
super.updateHash(hash, compilation);
}

/**
* @returns {void}
*/
disconnect() {
this.chunkGroup = undefined;
super.disconnect();
}

/**
* @returns {void}
*/
unseal() {
this.chunkGroup = undefined;
super.unseal();
}
}

Object.defineProperty(AsyncDependenciesBlock.prototype, "module", {
Expand Down
33 changes: 33 additions & 0 deletions lib/ChunkGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const util = require("util");
const SortableSet = require("./util/SortableSet");
const { compareModulesById } = require("./util/comparators");

/** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
/** @typedef {import("./Chunk")} Chunk */
/** @typedef {import("./ChunkGroup")} ChunkGroup */
/** @typedef {import("./Module")} Module */
Expand Down Expand Up @@ -99,6 +100,8 @@ class ChunkGraph {
this._modules = new WeakMap();
/** @private @type {WeakMap<Chunk, ChunkGraphChunk>} */
this._chunks = new WeakMap();
/** @private @type {WeakMap<AsyncDependenciesBlock, ChunkGroup>} */
this._blockChunkGroups = new WeakMap();
/** @private @type {ModuleGraph} */
this._moduleGraph = moduleGraph;
}
Expand Down Expand Up @@ -678,6 +681,36 @@ class ChunkGraph {
return cgc.entryModules;
}

/**
* @param {AsyncDependenciesBlock} depBlock the async block
* @returns {ChunkGroup} the chunk group
*/
getBlockChunkGroup(depBlock) {
return this._blockChunkGroups.get(depBlock);
}

/**
* @param {AsyncDependenciesBlock} depBlock the async block
* @param {ChunkGroup} chunkGroup the chunk group
* @returns {void}
*/
connectBlockAndChunkGroup(depBlock, chunkGroup) {
this._blockChunkGroups.set(depBlock, chunkGroup);
chunkGroup.addBlock(depBlock);
}

/**
* @param {ChunkGroup} chunkGroup the chunk group
* @returns {void}
*/
disconnectChunkGroup(chunkGroup) {
for (const block of chunkGroup.blocksIterable) {
this._blockChunkGroups.delete(block);
}
// TODO refactor by moving blocks list into ChunkGraph
chunkGroup._blocks.clear();
}

/**
* @param {Module} module the module
* @returns {string} hash
Expand Down
5 changes: 0 additions & 5 deletions lib/ChunkGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,6 @@ class ChunkGroup {
chunkGroup._parents.delete(this);
}

// cleanup blocks
for (const block of this._blocks) {
block.chunkGroup = null;
}

// remove chunks
for (const chunk of this.chunks) {
chunk.removeGroup(this);
Expand Down
19 changes: 11 additions & 8 deletions lib/Compilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ const EntryModuleNotFoundError = require("./EntryModuleNotFoundError");
const Entrypoint = require("./Entrypoint");
const {
connectChunkGroupAndChunk,
connectChunkGroupParentAndChild,
connectDependenciesBlockAndChunkGroup
connectChunkGroupParentAndChild
} = require("./GraphHelpers");
const HotUpdateChunkTemplate = require("./HotUpdateChunkTemplate");
const MainTemplate = require("./MainTemplate");
Expand Down Expand Up @@ -1396,7 +1395,9 @@ class Compilation {

const moduleGraph = this.moduleGraph;

/** @type {Map<ChunkGroup, {block: AsyncDependenciesBlock, chunkGroup: ChunkGroup}[]>} */
/** @typedef {{block: AsyncDependenciesBlock, chunkGroup: ChunkGroup}} ChunkGroupDep */

/** @type {Map<ChunkGroup, ChunkGroupDep[]>} */
const chunkDependencies = new Map();
/** @type {Set<ChunkGroup>} */
const allCreatedChunkGroups = new Set();
Expand Down Expand Up @@ -1726,7 +1727,7 @@ class Compilation {

// For each edge in the basic chunk graph
/**
* @param {TODO} dep the dependency used for filtering
* @param {ChunkGroupDep} dep the dependency used for filtering
* @returns {boolean} used to filter "edges" (aka Dependencies) that were pointing
* to modules that are already available. Also filters circular dependencies in the chunks graph
*/
Expand Down Expand Up @@ -1789,7 +1790,7 @@ class Compilation {
const depBlock = dep.block;

// 6. Connect block with chunk
connectDependenciesBlockAndChunkGroup(depBlock, depChunkGroup);
chunkGraph.connectBlockAndChunkGroup(depBlock, depChunkGroup);

// 7. Connect chunk with parent
connectChunkGroupParentAndChild(chunkGroup, depChunkGroup);
Expand All @@ -1814,6 +1815,7 @@ class Compilation {
if (idx >= 0) this.chunks.splice(idx, 1);
chunkGraph.disconnectChunk(chunk);
}
chunkGraph.disconnectChunkGroup(chunkGroup);
chunkGroup.remove();
}
}
Expand Down Expand Up @@ -1883,13 +1885,14 @@ class Compilation {
const blocks = block.blocks;
for (let indexBlock = 0; indexBlock < blocks.length; indexBlock++) {
const asyncBlock = blocks[indexBlock];
const chunkGroup = this.chunkGraph.getBlockChunkGroup(asyncBlock);
// Grab all chunks from the first Block's AsyncDepBlock
const chunks = asyncBlock.chunkGroup.chunks;
const chunks = chunkGroup.chunks;
// For each chunk in chunkGroup
for (let indexChunk = 0; indexChunk < chunks.length; indexChunk++) {
const iteratedChunk = chunks[indexChunk];
asyncBlock.chunkGroup.removeChunk(iteratedChunk);
asyncBlock.chunkGroup.removeParent(iteratedChunk);
chunkGroup.removeChunk(iteratedChunk);
chunkGroup.removeParent(iteratedChunk);
// Recurse
this.removeChunkFromDependencies(block, iteratedChunk);
}
Expand Down
35 changes: 26 additions & 9 deletions lib/ContextModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,14 @@ webpackAsyncContext.id = ${JSON.stringify(id)};
module.exports = webpackAsyncContext;`;
}

getLazyOnceSource(block, dependencies, id, { runtimeTemplate, moduleGraph }) {
getLazyOnceSource(
block,
dependencies,
id,
{ runtimeTemplate, moduleGraph, chunkGraph }
) {
const promise = runtimeTemplate.blockPromise({
chunkGraph,
block,
message: "lazy-once context"
});
Expand Down Expand Up @@ -600,7 +606,7 @@ webpackAsyncContext.id = ${JSON.stringify(id)};
module.exports = webpackAsyncContext;`;
}

getLazySource(blocks, id, moduleGraph) {
getLazySource(blocks, id, moduleGraph, chunkGraph) {
let hasMultipleOrNoChunks = false;
const fakeMap = this.getFakeMap(
blocks.map(b => b.dependencies[0]),
Expand All @@ -622,8 +628,8 @@ module.exports = webpackAsyncContext;`;
return a.userRequest < b.userRequest ? -1 : 1;
})
.reduce((map, item) => {
const chunks =
(item.block.chunkGroup && item.block.chunkGroup.chunks) || [];
const chunkGroup = chunkGraph.getBlockChunkGroup(item.block);
const chunks = (chunkGroup && chunkGroup.chunks) || [];
if (chunks.length !== 1) {
hasMultipleOrNoChunks = true;
}
Expand Down Expand Up @@ -697,10 +703,20 @@ module.exports = webpackEmptyAsyncContext;
webpackEmptyAsyncContext.id = ${JSON.stringify(id)};`;
}

getSourceString(asyncMode, { runtimeTemplate, moduleGraph }) {
/**
* @param {string} asyncMode module mode
* @param {SourceContext} sourceContext context info
* @returns {string} the source code
*/
getSourceString(asyncMode, { runtimeTemplate, moduleGraph, chunkGraph }) {
if (asyncMode === "lazy") {
if (this.blocks && this.blocks.length > 0) {
return this.getLazySource(this.blocks, this.id, moduleGraph);
return this.getLazySource(
this.blocks,
this.id,
moduleGraph,
chunkGraph
);
}
return this.getSourceForEmptyAsyncContext(this.id);
}
Expand All @@ -715,7 +731,8 @@ webpackEmptyAsyncContext.id = ${JSON.stringify(id)};`;
if (block) {
return this.getLazyOnceSource(block, block.dependencies, this.id, {
runtimeTemplate,
moduleGraph
moduleGraph,
chunkGraph
});
}
return this.getSourceForEmptyAsyncContext(this.id);
Expand Down Expand Up @@ -748,9 +765,9 @@ webpackEmptyAsyncContext.id = ${JSON.stringify(id)};`;
* @param {SourceContext} sourceContext source context
* @returns {Source} generated source
*/
source({ runtimeTemplate, moduleGraph }) {
source(sourceContext) {
return this.getSource(
this.getSourceString(this.options.mode, { runtimeTemplate, moduleGraph })
this.getSourceString(this.options.mode, sourceContext)
);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/DependencyTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"use strict";

/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("./ChunkGraph")} ChunkGraph */
/** @typedef {import("./Dependency")} Dependency */
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
/** @typedef {import("./InitFragment")} InitFragment */
Expand All @@ -18,6 +19,7 @@
* @property {RuntimeTemplate} runtimeTemplate the runtime template
* @property {DependencyTemplates} dependencyTemplates the dependency templates
* @property {ModuleGraph} moduleGraph the module graph
* @property {ChunkGraph} chunkGraph the chunk graph
* @property {Module} module current module
*/

Expand Down
2 changes: 2 additions & 0 deletions lib/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"use strict";

/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./ChunkGraph")} ChunkGraph */
/** @typedef {import("./DependencyTemplate")} DependencyTemplate */
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
/** @typedef {import("./ModuleGraph")} ModuleGraph */
Expand All @@ -17,6 +18,7 @@
* @property {DependencyTemplates} dependencyTemplates mapping from dependencies to templates
* @property {RuntimeTemplate} runtimeTemplate the runtime template
* @property {ModuleGraph} moduleGraph the module graph
* @property {ChunkGraph} chunkGraph the chunk graph
* @property {string} type which kind of code should be generated
*/

Expand Down
12 changes: 0 additions & 12 deletions lib/GraphHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,5 @@ const connectChunkGroupParentAndChild = (parent, child) => {
}
};

/**
* @param {AsyncDependenciesBlock} depBlock DepBlock being tied to ChunkGroup
* @param {ChunkGroup} chunkGroup ChunkGroup being tied to DepBlock
* @returns {void}
*/
const connectDependenciesBlockAndChunkGroup = (depBlock, chunkGroup) => {
if (chunkGroup.addBlock(depBlock)) {
depBlock.chunkGroup = chunkGroup;
}
};

exports.connectChunkGroupAndChunk = connectChunkGroupAndChunk;
exports.connectChunkGroupParentAndChild = connectChunkGroupParentAndChild;
exports.connectDependenciesBlockAndChunkGroup = connectDependenciesBlockAndChunkGroup;
1 change: 1 addition & 0 deletions lib/JavascriptGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class JavascriptGenerator extends Generator {
runtimeTemplate: generateContext.runtimeTemplate,
dependencyTemplates: generateContext.dependencyTemplates,
moduleGraph: generateContext.moduleGraph,
chunkGraph: generateContext.chunkGraph,
module
};

Expand Down
1 change: 1 addition & 0 deletions lib/NormalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ class NormalModule extends Module {
dependencyTemplates,
runtimeTemplate,
moduleGraph,
chunkGraph,
type
});

Expand Down
26 changes: 22 additions & 4 deletions lib/RuntimeTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const Template = require("./Template");

/** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
/** @typedef {import("./ChunkGraph")} ChunkGraph */
/** @typedef {import("./Module")} Module */
/** @typedef {import("./ModuleGraph")} ModuleGraph */
/** @typedef {import("./RequestShortener")} RequestShortener */
Expand Down Expand Up @@ -259,6 +260,7 @@ module.exports = class RuntimeTemplate {

/**
* @param {Object} options options object
* @param {ChunkGraph} options.chunkGraph the chunk graph
* @param {AsyncDependenciesBlock=} options.block the current dependencies block
* @param {Module} options.module the module
* @param {string} options.request the request that should be printed as comment
Expand All @@ -267,7 +269,15 @@ module.exports = class RuntimeTemplate {
* @param {boolean=} options.weak if the dependency is weak (will create a nice error message)
* @returns {string} the promise expression
*/
moduleNamespacePromise({ block, module, request, message, strict, weak }) {
moduleNamespacePromise({
chunkGraph,
block,
module,
request,
message,
strict,
weak
}) {
if (!module) {
return this.missingModulePromise({
request
Expand All @@ -288,6 +298,7 @@ module.exports = class RuntimeTemplate {
);
}
const promise = this.blockPromise({
chunkGraph,
block,
message
});
Expand Down Expand Up @@ -488,14 +499,21 @@ module.exports = class RuntimeTemplate {
}
}

blockPromise({ block, message }) {
if (!block || !block.chunkGroup || block.chunkGroup.chunks.length === 0) {
blockPromise({ block, message, chunkGraph }) {
if (!block) {
const comment = this.comment({
message
});
return `Promise.resolve(${comment.trim()})`;
}
const chunkGroup = chunkGraph.getBlockChunkGroup(block);
if (!chunkGroup || chunkGroup.chunks.length === 0) {
const comment = this.comment({
message
});
return `Promise.resolve(${comment.trim()})`;
}
const chunks = block.chunkGroup.chunks.filter(
const chunks = chunkGroup.chunks.filter(
chunk => !chunk.hasRuntime() && chunk.id !== null
);
const comment = this.comment({
Expand Down
Loading

0 comments on commit 670502f

Please sign in to comment.