diff --git a/framework/core/js/src/common/ExportRegistry.ts b/framework/core/js/src/common/ExportRegistry.ts index 42c73e3f77..6317061d92 100644 --- a/framework/core/js/src/common/ExportRegistry.ts +++ b/framework/core/js/src/common/ExportRegistry.ts @@ -91,6 +91,10 @@ export default class ExportRegistry implements IExportRegistry, IChunkRegistry { chunks = new Map(); chunkModules = new Map(); 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__; - - 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); + const wr = this._webpack_runtimes[namespace] ?? __webpack_require__; - // 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 { diff --git a/js-packages/webpack-config/src/OverrideChunkLoaderFunction.cjs b/js-packages/webpack-config/src/OverrideChunkLoaderFunction.cjs index 1680d31631..5de86094f5 100644 --- a/js-packages/webpack-config/src/OverrideChunkLoaderFunction.cjs +++ b/js-packages/webpack-config/src/OverrideChunkLoaderFunction.cjs @@ -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; + }); + } }); } }