Skip to content

Commit

Permalink
fix: importing extA lazy module from extB
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Nov 9, 2024
1 parent 820894a commit dd45d75
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
19 changes: 6 additions & 13 deletions framework/core/js/src/common/ExportRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 13 additions & 1 deletion js-packages/webpack-config/src/OverrideChunkLoaderFunction.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
});
}
});
}
}
Expand Down

0 comments on commit dd45d75

Please sign in to comment.