fix: correct preOrderIndex and postOrderIndex

This commit is contained in:
jserfeng 2024-01-26 16:21:05 +08:00
parent 4a2662381e
commit fc33aaf9a8
10 changed files with 140 additions and 0 deletions

View File

@ -266,12 +266,18 @@ const visitModules = (
/** @type {Map<DependenciesBlock, ChunkGroupInfo>} */
const blockChunkGroups = new Map();
/** @type {Map<ChunkGroupInfo, DependenciesBlock>} */
const blockByChunkGroups = new Map();
/** @type {Map<string, ChunkGroupInfo>} */
const namedChunkGroups = new Map();
/** @type {Map<string, ChunkGroupInfo>} */
const namedAsyncEntrypoints = new Map();
/** @type {Set<ChunkGroupInfo>} */
const outdatedOrderIndexChunkGroups = new Set();
const ADD_AND_ENTER_ENTRY_MODULE = 0;
const ADD_AND_ENTER_MODULE = 1;
const ENTER_MODULE = 2;
@ -524,6 +530,7 @@ const visitModules = (
blockConnections.set(b, []);
}
blockChunkGroups.set(b, /** @type {ChunkGroupInfo} */ (cgi));
blockByChunkGroups.set(cgi, b);
} else if (entryOptions) {
entrypoint = /** @type {Entrypoint} */ (cgi.chunkGroup);
} else {
@ -1208,6 +1215,7 @@ const visitModules = (
chunkGroupsForCombining.add(cgi);
}
}
outdatedOrderIndexChunkGroups.add(info);
}
outdatedChunkGroupInfo.clear();
};
@ -1254,6 +1262,51 @@ const visitModules = (
}
}
for (const info of outdatedOrderIndexChunkGroups) {
const { chunkGroup, runtime } = info;
const block = blockByChunkGroups.get(info);
if (!block) {
continue;
}
let preOrderIndex = 0;
let postOrderIndex = 0;
const process = (current, visited = new Set()) => {
if (visited.has(current)) {
return;
}
visited.add(current);
const blockModules = getBlockModules(current, runtime);
if (blockModules === undefined) {
return;
}
for (let i = 0; i < blockModules.length; i += 2) {
const refModule = /** @type {Module} */ (blockModules[i]);
const activeState = /** @type {ConnectionState} */ (
blockModules[i + 1]
);
if (activeState === false) {
continue;
}
if (refModule) {
chunkGroup.setModulePreOrderIndex(refModule, preOrderIndex++);
process(refModule, visited);
chunkGroup.setModulePostOrderIndex(refModule, postOrderIndex++);
}
}
};
process(block);
}
outdatedOrderIndexChunkGroups.clear();
logger.log(
`${statProcessedQueueItems} queue items processed (${statProcessedBlocks} blocks)`
);

View File

@ -0,0 +1,3 @@
import './m.css'
export default import(/* webpackChunkName: 'shared' */ './shared')

View File

@ -0,0 +1 @@
export default import(/* webpackChunkName: 'shared' */ './shared')

View File

@ -0,0 +1 @@
export default import(/* webpackChunkName: 'B-2' */ './B-2')

View File

@ -0,0 +1,3 @@
.m {
color: red;
}

View File

@ -0,0 +1,4 @@
it('should compile', async () => {
await (await import(/* webpackChunkName: 'A' */ './A')).default
await (await import(/* webpackChunkName: 'B' */ './B')).default
})

View File

@ -0,0 +1,3 @@
.n {
color: red;
}

View File

@ -0,0 +1,2 @@
import './m.css'
import './n.css'

View File

@ -0,0 +1,5 @@
module.exports = {
findBundle: function (i, options) {
return ["main.js", "A.js", "shared.js", "B.js", "B-2.js"];
}
};

View File

@ -0,0 +1,65 @@
/** @typedef {import("../../../../types").Compilation} Compilation */
/** @typedef {import("../../../../types").Module} Module */
/** @type {import("../../../../types").Configuration} */
module.exports = {
entry: {
main: "./main.js"
},
output: {
filename: "[name].js"
},
optimization: {
splitChunks: false,
chunkIds: "named"
},
plugins: [
function () {
/**
* @param {Compilation} compilation compilation
* @returns {void}
*/
const handler = compilation => {
compilation.hooks.afterSeal.tap("testcase", () => {
const data = {};
for (const [name, group] of compilation.namedChunkGroups) {
/** @type {Map<Module, number>} */
const modules = new Map();
for (const chunk of group.chunks) {
for (const module of compilation.chunkGraph.getChunkModulesIterable(
chunk
)) {
const preOrder = group.getModulePreOrderIndex(module);
if (typeof preOrder === "number") {
modules.set(module, preOrder);
}
}
}
const sortedModules = Array.from(modules).sort((a, b) => {
return a[1] - b[1];
});
const text = sortedModules
.map(
([m, index]) =>
`${index}: ${m.readableIdentifier(
compilation.requestShortener
)}`
)
.join(", ");
data[name + "Index"] = text;
}
expect(data).toEqual({
AIndex: "0: ./A.js, 1: css ./m.css",
"B-2Index": "0: ./B-2.js",
BIndex: "0: ./B.js",
mainIndex: "0: ./main.js",
sharedIndex: "0: ./shared.js, 1: css ./m.css, 2: css ./n.css"
});
});
};
this.hooks.compilation.tap("testcase", handler);
}
],
experiments: {
css: true
}
};