fix: code splitting on dev mode

This commit is contained in:
Sami Mazouz 2024-09-11 11:30:10 +01:00
parent 270ba09d08
commit b9037df05c
No known key found for this signature in database

View File

@ -1,6 +1,7 @@
const path = require('path');
const extensionId = require('./extensionId.cjs');
const { Compilation } = require('webpack');
const ConcatenatedModule = require('webpack/lib/optimize/ConcatenatedModule');
class RegisterAsyncChunksPlugin {
static registry = {};
@ -81,14 +82,40 @@ class RegisterAsyncChunksPlugin {
return match;
}
let concatenatedModule = chunkModules(relevantChunk)[0];
const moduleId = compilation.chunkGraph.getModuleId(concatenatedModule);
const relevantChunkModules = chunkModules(relevantChunk);
const mainModule = relevantChunkModules.filter((m) => {
return m.resource?.split('.')[0] === importPathResolved || m.rootModule?.resource?.split('.')[0] === importPathResolved;
})[0];
const otherRelevantChunkModules = relevantChunkModules.filter((m) => m !== mainModule);
if (mainModule instanceof ConcatenatedModule && mainModule.modules) {
otherRelevantChunkModules.push(...mainModule.modules);
}
if (!mainModule) {
return match;
}
const moduleId = compilation.chunkGraph.getModuleId(mainModule);
const registrableModulesUrlPaths = new Map();
registrableModulesUrlPaths.set(urlPath, [relevantChunk.id, moduleId, namespace, urlPath]);
if (concatenatedModule?.rootModule) {
const modules = [];
otherRelevantChunkModules.forEach((module) => {
if (module instanceof ConcatenatedModule && module.modules) {
modules.push(...module.modules);
} else {
modules.push(module);
}
});
if (relevantChunk.name.endsWith('PostStream')) {
console.log(otherRelevantChunkModules);
}
// This is a chunk with many modules, we need to register all of them.
concatenatedModule.modules?.forEach((module) => {
modules?.forEach((module) => {
if (!module.resource.includes(`${path.sep}src${path.sep}`)) {
return;
}
@ -101,7 +128,6 @@ class RegisterAsyncChunksPlugin {
registrableModulesUrlPaths.set(urlPath, [relevantChunk.id, moduleId, namespace, urlPath]);
}
});
}
registrableModulesUrlPaths.forEach(([chunkId, moduleId, namespace, urlPath]) => {
chunkModuleMemory[sourceChunkId] = chunkModuleMemory[sourceChunkId] || [];