Unverified Commit ad7d27ec authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Turbopack hmr: preserve group factory consistency for compressed modules (#89976)

## Summary
- update `installCompressedModuleFactories` to reuse an existing factory
in a compressed ID group when filling missing IDs
- keep `newModuleId` notifications aligned with actual IDs installed
(`newModuleId(id)`)
- add an execution regression test for the mixed-group case where one ID
already has a factory and another is missing
- regenerate affected runtime snapshots
(runtime/debug-ids/workers/preset_env)

Aims to fix cases like this being seen recently:

```sh
Error: Module 8896 was instantiated because it was required from module 18500, but the module factory is not available.
```

```

## Test Plan
- `cargo test -p turbopack-tests --test execution test_tests__execution__turbopack__runtime__factory_group_existing_factory__input__index_js -- --nocapture`
- `cargo test -p turbopack-tests --test snapshot test_tests__snapshot__runtime__default_build_runtime__input__index_js -- --nocapture`
- `cargo test -p turbopack-tests --test snapshot test_tests__snapshot__runtime__default_dev_runtime__input__index_js -- --nocapture`
- `cargo test -p turbopack-tests --test snapshot test_tests__snapshot__debug_ids -- --nocapture`
- `cargo test -p turbopack-tests --test snapshot test_tests__snapshot__swc_transforms__preset_env__input__index_js -- --nocapture`
- `cargo test -p turbopack-tests --test snapshot test_tests__snapshot__workers__ -- --nocapture`
parent 6df0b7b9
...@@ -551,7 +551,6 @@ function installCompressedModuleFactories( ...@@ -551,7 +551,6 @@ function installCompressedModuleFactories(
) { ) {
let i = offset let i = offset
while (i < chunkModules.length) { while (i < chunkModules.length) {
let moduleId = chunkModules[i] as ModuleId
let end = i + 1 let end = i + 1
// Find our factory function // Find our factory function
while ( while (
...@@ -565,19 +564,33 @@ function installCompressedModuleFactories( ...@@ -565,19 +564,33 @@ function installCompressedModuleFactories(
} }
// Install the factory for each module ID that doesn't already have one. // Install the factory for each module ID that doesn't already have one.
// This handles both the normal case and the case where some IDs in a group // When some IDs in this group already have a factory, reuse that existing
// may have been registered separately (e.g., from another chunk or HMR update). // group factory for the missing IDs to keep all IDs in the group consistent.
// Otherwise, install the factory from this chunk.
const moduleFactoryFn = chunkModules[end] as Function const moduleFactoryFn = chunkModules[end] as Function
let existingGroupFactory: Function | undefined = undefined
for (let j = i; j < end; j++) {
const id = chunkModules[j] as ModuleId
const existingFactory = moduleFactories.get(id)
if (existingFactory) {
existingGroupFactory = existingFactory
break
}
}
const factoryToInstall = existingGroupFactory ?? moduleFactoryFn
let didInstallFactory = false let didInstallFactory = false
for (let j = i; j < end; j++) { for (let j = i; j < end; j++) {
const id = chunkModules[j] as ModuleId const id = chunkModules[j] as ModuleId
if (!moduleFactories.has(id)) { if (!moduleFactories.has(id)) {
if (!didInstallFactory) { if (!didInstallFactory) {
applyModuleFactoryName(moduleFactoryFn) if (factoryToInstall === moduleFactoryFn) {
newModuleId?.(moduleId) applyModuleFactoryName(moduleFactoryFn)
}
didInstallFactory = true didInstallFactory = true
} }
moduleFactories.set(id, moduleFactoryFn) moduleFactories.set(id, factoryToInstall)
newModuleId?.(id)
} }
} }
i = end + 1 // end is pointing at the last factory advance to the next id or the end of the array. i = end + 1 // end is pointing at the last factory advance to the next id or the end of the array.
......
const fs = require('fs')
const os = require('os')
const path = require('path')
function writeChunk(filePath, chunkModules) {
fs.writeFileSync(filePath, `module.exports = ${chunkModules};\n`)
}
it('reuses the existing group factory for missing IDs', () => {
const repoRoot = path.resolve(process.cwd(), '../../../../../../../..')
const runtimePath = path.join(
repoRoot,
'turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_build_runtime/output/[turbopack]_runtime.js'
)
const createRuntime = eval('require')(runtimePath)
const suffix = `${Date.now()}-${Math.random().toString(16).slice(2)}`
const moduleA = `module-a-${suffix}`
const moduleB = `module-b-${suffix}`
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tp-factory-group-'))
try {
const firstChunk = path.join(tempDir, 'first.js')
const mixedChunk = path.join(tempDir, 'mixed.js')
writeChunk(
firstChunk,
`[
${JSON.stringify(moduleA)},
function (__turbopack_context__, module) {
module.exports = "first-factory"
}
]`
)
writeChunk(
mixedChunk,
`[
${JSON.stringify(moduleA)},
${JSON.stringify(moduleB)},
function (__turbopack_context__, module) {
module.exports = "second-factory"
}
]`
)
const runtime = createRuntime('test-source')
runtime.c(firstChunk)
runtime.c(mixedChunk)
expect(runtime.m(moduleA).exports).toBe('first-factory')
expect(runtime.m(moduleB).exports).toBe('first-factory')
} finally {
fs.rmSync(tempDir, { recursive: true, force: true })
}
})
;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="3804020b-9b04-847a-6d0e-e56ae49220ac")}catch(e){}}(); ;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="db4038c7-36b5-4d88-bb31-cce5ee0ea824")}catch(e){}}();
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([ (globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/ba425_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0151fefb.js", "output/ba425_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0151fefb.js",
{"otherChunks":["output/aaf3a_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0b8736b3.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/input/index.js [test] (ecmascript)"]} {"otherChunks":["output/aaf3a_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0b8736b3.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/input/index.js [test] (ecmascript)"]}
...@@ -382,7 +382,6 @@ function createPromise() { ...@@ -382,7 +382,6 @@ function createPromise() {
function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) { function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) {
let i = offset; let i = offset;
while(i < chunkModules.length){ while(i < chunkModules.length){
let moduleId = chunkModules[i];
let end = i + 1; let end = i + 1;
// Find our factory function // Find our factory function
while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){ while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){
...@@ -392,19 +391,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories, ...@@ -392,19 +391,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories,
throw new Error('malformed chunk format, expected a factory function'); throw new Error('malformed chunk format, expected a factory function');
} }
// Install the factory for each module ID that doesn't already have one. // Install the factory for each module ID that doesn't already have one.
// This handles both the normal case and the case where some IDs in a group // When some IDs in this group already have a factory, reuse that existing
// may have been registered separately (e.g., from another chunk or HMR update). // group factory for the missing IDs to keep all IDs in the group consistent.
// Otherwise, install the factory from this chunk.
const moduleFactoryFn = chunkModules[end]; const moduleFactoryFn = chunkModules[end];
let existingGroupFactory = undefined;
for(let j = i; j < end; j++){
const id = chunkModules[j];
const existingFactory = moduleFactories.get(id);
if (existingFactory) {
existingGroupFactory = existingFactory;
break;
}
}
const factoryToInstall = existingGroupFactory ?? moduleFactoryFn;
let didInstallFactory = false; let didInstallFactory = false;
for(let j = i; j < end; j++){ for(let j = i; j < end; j++){
const id = chunkModules[j]; const id = chunkModules[j];
if (!moduleFactories.has(id)) { if (!moduleFactories.has(id)) {
if (!didInstallFactory) { if (!didInstallFactory) {
applyModuleFactoryName(moduleFactoryFn); if (factoryToInstall === moduleFactoryFn) {
newModuleId?.(moduleId); applyModuleFactoryName(moduleFactoryFn);
}
didInstallFactory = true; didInstallFactory = true;
} }
moduleFactories.set(id, moduleFactoryFn); moduleFactories.set(id, factoryToInstall);
newModuleId?.(id);
} }
} }
i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array. i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array.
...@@ -2081,5 +2093,5 @@ chunkListsToRegister.forEach(registerChunkList); ...@@ -2081,5 +2093,5 @@ chunkListsToRegister.forEach(registerChunkList);
})(); })();
//# debugId=3804020b-9b04-847a-6d0e-e56ae49220ac //# debugId=db4038c7-36b5-4d88-bb31-cce5ee0ea824
//# sourceMappingURL=aaf3a_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0151fefb.js.map //# sourceMappingURL=aaf3a_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0151fefb.js.map
\ No newline at end of file
...@@ -371,7 +371,6 @@ function createPromise() { ...@@ -371,7 +371,6 @@ function createPromise() {
function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) { function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) {
let i = offset; let i = offset;
while(i < chunkModules.length){ while(i < chunkModules.length){
let moduleId = chunkModules[i];
let end = i + 1; let end = i + 1;
// Find our factory function // Find our factory function
while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){ while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){
...@@ -381,19 +380,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories, ...@@ -381,19 +380,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories,
throw new Error('malformed chunk format, expected a factory function'); throw new Error('malformed chunk format, expected a factory function');
} }
// Install the factory for each module ID that doesn't already have one. // Install the factory for each module ID that doesn't already have one.
// This handles both the normal case and the case where some IDs in a group // When some IDs in this group already have a factory, reuse that existing
// may have been registered separately (e.g., from another chunk or HMR update). // group factory for the missing IDs to keep all IDs in the group consistent.
// Otherwise, install the factory from this chunk.
const moduleFactoryFn = chunkModules[end]; const moduleFactoryFn = chunkModules[end];
let existingGroupFactory = undefined;
for(let j = i; j < end; j++){
const id = chunkModules[j];
const existingFactory = moduleFactories.get(id);
if (existingFactory) {
existingGroupFactory = existingFactory;
break;
}
}
const factoryToInstall = existingGroupFactory ?? moduleFactoryFn;
let didInstallFactory = false; let didInstallFactory = false;
for(let j = i; j < end; j++){ for(let j = i; j < end; j++){
const id = chunkModules[j]; const id = chunkModules[j];
if (!moduleFactories.has(id)) { if (!moduleFactories.has(id)) {
if (!didInstallFactory) { if (!didInstallFactory) {
applyModuleFactoryName(moduleFactoryFn); if (factoryToInstall === moduleFactoryFn) {
newModuleId?.(moduleId); applyModuleFactoryName(moduleFactoryFn);
}
didInstallFactory = true; didInstallFactory = true;
} }
moduleFactories.set(id, moduleFactoryFn); moduleFactories.set(id, factoryToInstall);
newModuleId?.(id);
} }
} }
i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array. i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array.
......
...@@ -371,7 +371,6 @@ function createPromise() { ...@@ -371,7 +371,6 @@ function createPromise() {
function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) { function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) {
let i = offset; let i = offset;
while(i < chunkModules.length){ while(i < chunkModules.length){
let moduleId = chunkModules[i];
let end = i + 1; let end = i + 1;
// Find our factory function // Find our factory function
while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){ while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){
...@@ -381,19 +380,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories, ...@@ -381,19 +380,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories,
throw new Error('malformed chunk format, expected a factory function'); throw new Error('malformed chunk format, expected a factory function');
} }
// Install the factory for each module ID that doesn't already have one. // Install the factory for each module ID that doesn't already have one.
// This handles both the normal case and the case where some IDs in a group // When some IDs in this group already have a factory, reuse that existing
// may have been registered separately (e.g., from another chunk or HMR update). // group factory for the missing IDs to keep all IDs in the group consistent.
// Otherwise, install the factory from this chunk.
const moduleFactoryFn = chunkModules[end]; const moduleFactoryFn = chunkModules[end];
let existingGroupFactory = undefined;
for(let j = i; j < end; j++){
const id = chunkModules[j];
const existingFactory = moduleFactories.get(id);
if (existingFactory) {
existingGroupFactory = existingFactory;
break;
}
}
const factoryToInstall = existingGroupFactory ?? moduleFactoryFn;
let didInstallFactory = false; let didInstallFactory = false;
for(let j = i; j < end; j++){ for(let j = i; j < end; j++){
const id = chunkModules[j]; const id = chunkModules[j];
if (!moduleFactories.has(id)) { if (!moduleFactories.has(id)) {
if (!didInstallFactory) { if (!didInstallFactory) {
applyModuleFactoryName(moduleFactoryFn); if (factoryToInstall === moduleFactoryFn) {
newModuleId?.(moduleId); applyModuleFactoryName(moduleFactoryFn);
}
didInstallFactory = true; didInstallFactory = true;
} }
moduleFactories.set(id, moduleFactoryFn); moduleFactories.set(id, factoryToInstall);
newModuleId?.(id);
} }
} }
i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array. i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array.
......
...@@ -381,7 +381,6 @@ function createPromise() { ...@@ -381,7 +381,6 @@ function createPromise() {
function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) { function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) {
let i = offset; let i = offset;
while(i < chunkModules.length){ while(i < chunkModules.length){
let moduleId = chunkModules[i];
let end = i + 1; let end = i + 1;
// Find our factory function // Find our factory function
while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){ while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){
...@@ -391,19 +390,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories, ...@@ -391,19 +390,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories,
throw new Error('malformed chunk format, expected a factory function'); throw new Error('malformed chunk format, expected a factory function');
} }
// Install the factory for each module ID that doesn't already have one. // Install the factory for each module ID that doesn't already have one.
// This handles both the normal case and the case where some IDs in a group // When some IDs in this group already have a factory, reuse that existing
// may have been registered separately (e.g., from another chunk or HMR update). // group factory for the missing IDs to keep all IDs in the group consistent.
// Otherwise, install the factory from this chunk.
const moduleFactoryFn = chunkModules[end]; const moduleFactoryFn = chunkModules[end];
let existingGroupFactory = undefined;
for(let j = i; j < end; j++){
const id = chunkModules[j];
const existingFactory = moduleFactories.get(id);
if (existingFactory) {
existingGroupFactory = existingFactory;
break;
}
}
const factoryToInstall = existingGroupFactory ?? moduleFactoryFn;
let didInstallFactory = false; let didInstallFactory = false;
for(let j = i; j < end; j++){ for(let j = i; j < end; j++){
const id = chunkModules[j]; const id = chunkModules[j];
if (!moduleFactories.has(id)) { if (!moduleFactories.has(id)) {
if (!didInstallFactory) { if (!didInstallFactory) {
applyModuleFactoryName(moduleFactoryFn); if (factoryToInstall === moduleFactoryFn) {
newModuleId?.(moduleId); applyModuleFactoryName(moduleFactoryFn);
}
didInstallFactory = true; didInstallFactory = true;
} }
moduleFactories.set(id, moduleFactoryFn); moduleFactories.set(id, factoryToInstall);
newModuleId?.(id);
} }
} }
i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array. i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array.
......
...@@ -618,7 +618,6 @@ function createPromise() { ...@@ -618,7 +618,6 @@ function createPromise() {
function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) { function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) {
var i = offset; var i = offset;
while(i < chunkModules.length){ while(i < chunkModules.length){
var moduleId = chunkModules[i];
var end = i + 1; var end = i + 1;
// Find our factory function // Find our factory function
while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){ while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){
...@@ -628,19 +627,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories, ...@@ -628,19 +627,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories,
throw new Error('malformed chunk format, expected a factory function'); throw new Error('malformed chunk format, expected a factory function');
} }
// Install the factory for each module ID that doesn't already have one. // Install the factory for each module ID that doesn't already have one.
// This handles both the normal case and the case where some IDs in a group // When some IDs in this group already have a factory, reuse that existing
// may have been registered separately (e.g., from another chunk or HMR update). // group factory for the missing IDs to keep all IDs in the group consistent.
// Otherwise, install the factory from this chunk.
var moduleFactoryFn = chunkModules[end]; var moduleFactoryFn = chunkModules[end];
var didInstallFactory = false; var existingGroupFactory = undefined;
for(var j = i; j < end; j++){ for(var j = i; j < end; j++){
var id = chunkModules[j]; var id = chunkModules[j];
if (!moduleFactories.has(id)) { var existingFactory = moduleFactories.get(id);
if (existingFactory) {
existingGroupFactory = existingFactory;
break;
}
}
var factoryToInstall = existingGroupFactory !== null && existingGroupFactory !== void 0 ? existingGroupFactory : moduleFactoryFn;
var didInstallFactory = false;
for(var j1 = i; j1 < end; j1++){
var id1 = chunkModules[j1];
if (!moduleFactories.has(id1)) {
if (!didInstallFactory) { if (!didInstallFactory) {
applyModuleFactoryName(moduleFactoryFn); if (factoryToInstall === moduleFactoryFn) {
newModuleId === null || newModuleId === void 0 ? void 0 : newModuleId(moduleId); applyModuleFactoryName(moduleFactoryFn);
}
didInstallFactory = true; didInstallFactory = true;
} }
moduleFactories.set(id, moduleFactoryFn); moduleFactories.set(id1, factoryToInstall);
newModuleId === null || newModuleId === void 0 ? void 0 : newModuleId(id1);
} }
} }
i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array. i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array.
......
...@@ -381,7 +381,6 @@ function createPromise() { ...@@ -381,7 +381,6 @@ function createPromise() {
function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) { function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) {
let i = offset; let i = offset;
while(i < chunkModules.length){ while(i < chunkModules.length){
let moduleId = chunkModules[i];
let end = i + 1; let end = i + 1;
// Find our factory function // Find our factory function
while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){ while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){
...@@ -391,19 +390,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories, ...@@ -391,19 +390,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories,
throw new Error('malformed chunk format, expected a factory function'); throw new Error('malformed chunk format, expected a factory function');
} }
// Install the factory for each module ID that doesn't already have one. // Install the factory for each module ID that doesn't already have one.
// This handles both the normal case and the case where some IDs in a group // When some IDs in this group already have a factory, reuse that existing
// may have been registered separately (e.g., from another chunk or HMR update). // group factory for the missing IDs to keep all IDs in the group consistent.
// Otherwise, install the factory from this chunk.
const moduleFactoryFn = chunkModules[end]; const moduleFactoryFn = chunkModules[end];
let existingGroupFactory = undefined;
for(let j = i; j < end; j++){
const id = chunkModules[j];
const existingFactory = moduleFactories.get(id);
if (existingFactory) {
existingGroupFactory = existingFactory;
break;
}
}
const factoryToInstall = existingGroupFactory ?? moduleFactoryFn;
let didInstallFactory = false; let didInstallFactory = false;
for(let j = i; j < end; j++){ for(let j = i; j < end; j++){
const id = chunkModules[j]; const id = chunkModules[j];
if (!moduleFactories.has(id)) { if (!moduleFactories.has(id)) {
if (!didInstallFactory) { if (!didInstallFactory) {
applyModuleFactoryName(moduleFactoryFn); if (factoryToInstall === moduleFactoryFn) {
newModuleId?.(moduleId); applyModuleFactoryName(moduleFactoryFn);
}
didInstallFactory = true; didInstallFactory = true;
} }
moduleFactories.set(id, moduleFactoryFn); moduleFactories.set(id, factoryToInstall);
newModuleId?.(id);
} }
} }
i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array. i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array.
......
...@@ -381,7 +381,6 @@ function createPromise() { ...@@ -381,7 +381,6 @@ function createPromise() {
function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) { function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) {
let i = offset; let i = offset;
while(i < chunkModules.length){ while(i < chunkModules.length){
let moduleId = chunkModules[i];
let end = i + 1; let end = i + 1;
// Find our factory function // Find our factory function
while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){ while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){
...@@ -391,19 +390,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories, ...@@ -391,19 +390,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories,
throw new Error('malformed chunk format, expected a factory function'); throw new Error('malformed chunk format, expected a factory function');
} }
// Install the factory for each module ID that doesn't already have one. // Install the factory for each module ID that doesn't already have one.
// This handles both the normal case and the case where some IDs in a group // When some IDs in this group already have a factory, reuse that existing
// may have been registered separately (e.g., from another chunk or HMR update). // group factory for the missing IDs to keep all IDs in the group consistent.
// Otherwise, install the factory from this chunk.
const moduleFactoryFn = chunkModules[end]; const moduleFactoryFn = chunkModules[end];
let existingGroupFactory = undefined;
for(let j = i; j < end; j++){
const id = chunkModules[j];
const existingFactory = moduleFactories.get(id);
if (existingFactory) {
existingGroupFactory = existingFactory;
break;
}
}
const factoryToInstall = existingGroupFactory ?? moduleFactoryFn;
let didInstallFactory = false; let didInstallFactory = false;
for(let j = i; j < end; j++){ for(let j = i; j < end; j++){
const id = chunkModules[j]; const id = chunkModules[j];
if (!moduleFactories.has(id)) { if (!moduleFactories.has(id)) {
if (!didInstallFactory) { if (!didInstallFactory) {
applyModuleFactoryName(moduleFactoryFn); if (factoryToInstall === moduleFactoryFn) {
newModuleId?.(moduleId); applyModuleFactoryName(moduleFactoryFn);
}
didInstallFactory = true; didInstallFactory = true;
} }
moduleFactories.set(id, moduleFactoryFn); moduleFactories.set(id, factoryToInstall);
newModuleId?.(id);
} }
} }
i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array. i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array.
......
...@@ -381,7 +381,6 @@ function createPromise() { ...@@ -381,7 +381,6 @@ function createPromise() {
function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) { function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) {
let i = offset; let i = offset;
while(i < chunkModules.length){ while(i < chunkModules.length){
let moduleId = chunkModules[i];
let end = i + 1; let end = i + 1;
// Find our factory function // Find our factory function
while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){ while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){
...@@ -391,19 +390,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories, ...@@ -391,19 +390,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories,
throw new Error('malformed chunk format, expected a factory function'); throw new Error('malformed chunk format, expected a factory function');
} }
// Install the factory for each module ID that doesn't already have one. // Install the factory for each module ID that doesn't already have one.
// This handles both the normal case and the case where some IDs in a group // When some IDs in this group already have a factory, reuse that existing
// may have been registered separately (e.g., from another chunk or HMR update). // group factory for the missing IDs to keep all IDs in the group consistent.
// Otherwise, install the factory from this chunk.
const moduleFactoryFn = chunkModules[end]; const moduleFactoryFn = chunkModules[end];
let existingGroupFactory = undefined;
for(let j = i; j < end; j++){
const id = chunkModules[j];
const existingFactory = moduleFactories.get(id);
if (existingFactory) {
existingGroupFactory = existingFactory;
break;
}
}
const factoryToInstall = existingGroupFactory ?? moduleFactoryFn;
let didInstallFactory = false; let didInstallFactory = false;
for(let j = i; j < end; j++){ for(let j = i; j < end; j++){
const id = chunkModules[j]; const id = chunkModules[j];
if (!moduleFactories.has(id)) { if (!moduleFactories.has(id)) {
if (!didInstallFactory) { if (!didInstallFactory) {
applyModuleFactoryName(moduleFactoryFn); if (factoryToInstall === moduleFactoryFn) {
newModuleId?.(moduleId); applyModuleFactoryName(moduleFactoryFn);
}
didInstallFactory = true; didInstallFactory = true;
} }
moduleFactories.set(id, moduleFactoryFn); moduleFactories.set(id, factoryToInstall);
newModuleId?.(id);
} }
} }
i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array. i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array.
......
...@@ -381,7 +381,6 @@ function createPromise() { ...@@ -381,7 +381,6 @@ function createPromise() {
function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) { function installCompressedModuleFactories(chunkModules, offset, moduleFactories, newModuleId) {
let i = offset; let i = offset;
while(i < chunkModules.length){ while(i < chunkModules.length){
let moduleId = chunkModules[i];
let end = i + 1; let end = i + 1;
// Find our factory function // Find our factory function
while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){ while(end < chunkModules.length && typeof chunkModules[end] !== 'function'){
...@@ -391,19 +390,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories, ...@@ -391,19 +390,32 @@ function installCompressedModuleFactories(chunkModules, offset, moduleFactories,
throw new Error('malformed chunk format, expected a factory function'); throw new Error('malformed chunk format, expected a factory function');
} }
// Install the factory for each module ID that doesn't already have one. // Install the factory for each module ID that doesn't already have one.
// This handles both the normal case and the case where some IDs in a group // When some IDs in this group already have a factory, reuse that existing
// may have been registered separately (e.g., from another chunk or HMR update). // group factory for the missing IDs to keep all IDs in the group consistent.
// Otherwise, install the factory from this chunk.
const moduleFactoryFn = chunkModules[end]; const moduleFactoryFn = chunkModules[end];
let existingGroupFactory = undefined;
for(let j = i; j < end; j++){
const id = chunkModules[j];
const existingFactory = moduleFactories.get(id);
if (existingFactory) {
existingGroupFactory = existingFactory;
break;
}
}
const factoryToInstall = existingGroupFactory ?? moduleFactoryFn;
let didInstallFactory = false; let didInstallFactory = false;
for(let j = i; j < end; j++){ for(let j = i; j < end; j++){
const id = chunkModules[j]; const id = chunkModules[j];
if (!moduleFactories.has(id)) { if (!moduleFactories.has(id)) {
if (!didInstallFactory) { if (!didInstallFactory) {
applyModuleFactoryName(moduleFactoryFn); if (factoryToInstall === moduleFactoryFn) {
newModuleId?.(moduleId); applyModuleFactoryName(moduleFactoryFn);
}
didInstallFactory = true; didInstallFactory = true;
} }
moduleFactories.set(id, moduleFactoryFn); moduleFactories.set(id, factoryToInstall);
newModuleId?.(id);
} }
} }
i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array. i = end + 1; // end is pointing at the last factory advance to the next id or the end of the array.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment