1
0
mirror of https://github.com/flarum/core.git synced 2025-08-08 09:26:34 +02:00

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
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>(); chunks = new Map<string, Chunk>();
chunkModules = new Map<string, Module>(); chunkModules = new Map<string, Module>();
private _revisions: any = null; private _revisions: any = null;
private _webpack_runtimes: any = {
// @ts-ignore
core: __webpack_require__,
};
add(namespace: string, id: string, object: any): void { add(namespace: string, id: string, object: any): void {
this.moduleExports.set(namespace, this.moduleExports.get(namespace) || new Map()); this.moduleExports.set(namespace, this.moduleExports.get(namespace) || new Map());
@@ -205,20 +209,9 @@ export default class ExportRegistry implements IExportRegistry, IChunkRegistry {
} }
// @ts-ignore // @ts-ignore
const wr = __webpack_require__; const wr = this._webpack_runtimes[namespace] ?? __webpack_require__;
return await wr.e(module.chunkId).then(() => { return await wr.e(module.chunkId).then(wr.bind(wr, module.moduleId));
// 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;
});
} }
public clear(): void { 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 * 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. * with `flarum.reg.loadChunk`, which resides in the flarum app.
*/ */
const path = require('path');
const extensionId = require('./extensionId.cjs');
class OverrideChunkLoaderFunction { class OverrideChunkLoaderFunction {
apply(compiler) { 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 don't want to literally override its source.
// We want to override the function that is called by webpack. // 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. // The function is called by webpack so we can't just override it.
compiler.hooks.compilation.tap('OverrideChunkLoaderFunction', (compilation) => { compiler.hooks.compilation.tap('OverrideChunkLoaderFunction', (compilation) => {
compilation.mainTemplate.hooks.requireEnsure.tap('OverrideChunkLoaderFunction', (source) => { 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);' '\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;
});
}
}); });
} }
} }