fix: importing extA lazy module from extB

This commit is contained in:
Sami Mazouz 2024-11-09 11:01:36 +01:00
parent 820894a7c9
commit dd45d75cd8
No known key found for this signature in database
2 changed files with 19 additions and 14 deletions

View File

@ -91,6 +91,10 @@ export default class ExportRegistry implements IExportRegistry, IChunkRegistry {
chunks = new Map<string, Chunk>();
chunkModules = new Map<string, Module>();
private _revisions: any = null;
private _webpack_runtimes: any = {
// @ts-ignore
core: __webpack_require__,
};
add(namespace: string, id: string, object: any): void {
this.moduleExports.set(namespace, this.moduleExports.get(namespace) || new Map());
@ -205,20 +209,9 @@ export default class ExportRegistry implements IExportRegistry, IChunkRegistry {
}
// @ts-ignore
const wr = __webpack_require__;
const wr = this._webpack_runtimes[namespace] ?? __webpack_require__;
return await wr.e(module.chunkId).then(() => {
// Needed to make sure the module is loaded.
// Taken care of by webpack.
wr.bind(wr, module.moduleId)();
const moduleExport = this.get(namespace, id);
// For consistent access to async modules.
moduleExport.default = moduleExport.default || moduleExport;
return moduleExport;
});
return await wr.e(module.chunkId).then(wr.bind(wr, module.moduleId));
}
public clear(): void {

View File

@ -2,12 +2,17 @@
* This plugin overrides the webpack chunk loader function `__webpack_require__.l` which is a webpack constant
* with `flarum.reg.loadChunk`, which resides in the flarum app.
*/
const path = require('path');
const extensionId = require('./extensionId.cjs');
class OverrideChunkLoaderFunction {
apply(compiler) {
const thisComposerJson = require(path.resolve(process.cwd(), '../composer.json'));
const namespace = extensionId(thisComposerJson.name);
// We don't want to literally override its source.
// We want to override the function that is called by webpack.
// By adding a new line to reassing the function to our own function.
// By adding a new line to reassign the function to our own function.
// The function is called by webpack so we can't just override it.
compiler.hooks.compilation.tap('OverrideChunkLoaderFunction', (compilation) => {
compilation.mainTemplate.hooks.requireEnsure.tap('OverrideChunkLoaderFunction', (source) => {
@ -16,6 +21,13 @@ class OverrideChunkLoaderFunction {
'\nconst originalLoadChunk = __webpack_require__.l;\n__webpack_require__.l = flarum.reg.loadChunk.bind(flarum.reg, originalLoadChunk);'
);
});
// log webpack runtime after the final code, the hook isn't requireEnsure
if (namespace !== 'core') {
compilation.mainTemplate.hooks.require.tap('OverrideChunkLoaderFunction', (source) => {
return 'flarum.reg._webpack_runtimes["' + namespace + '"] ||= __webpack_require__;' + source;
});
}
});
}
}