Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Nov 24, 2023
1 parent a2ec83b commit 38c37be
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct ModuleFederationRuntimeModule {
impl Default for ModuleFederationRuntimeModule {
fn default() -> Self {
Self {
id: Identifier::from("webpack/runtime/mf_scope"),
id: Identifier::from("webpack/runtime/module_federation_runtime"),
chunk: None,
}
}
Expand Down
24 changes: 19 additions & 5 deletions packages/rspack/src/container/ContainerReferencePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { Compiler } from "../Compiler";
import { ExternalsPlugin } from "../builtin-plugin/ExternalsPlugin";
import { ExternalsType } from "../config";
import { parseOptions } from "./options";
import { RemoteRuntimeSingletonPlugin } from "./RemoteRuntimeSingletonPlugin";
import { ShareRuntimeSingletonPlugin } from "../sharing/ShareRuntimeSingletonPlugin";
import { ModuleFederationRuntimePlugin } from "./ModuleFederationRuntimePlugin";
import { isNil } from "../util";

export type ContainerReferencePluginOptions = {
remoteType: ExternalsType;
Expand All @@ -30,8 +30,12 @@ export class ContainerReferencePlugin extends RspackBuiltinPlugin {
name = BuiltinPluginName.ContainerReferencePlugin;
_options: RawContainerReferencePluginOptions;
_remotes;
_mfRuntimePlugin: [boolean, ModuleFederationRuntimePlugin];

constructor(options: ContainerReferencePluginOptions) {
constructor(
options: ContainerReferencePluginOptions,
mfRuntimePlugin?: ModuleFederationRuntimePlugin
) {
super();
this._remotes = parseOptions(
options.remotes,
Expand All @@ -50,6 +54,10 @@ export class ContainerReferencePlugin extends RspackBuiltinPlugin {
remoteType: options.remoteType,
remotes: this._remotes.map(([key, r]) => ({ key, ...r }))
};
this._mfRuntimePlugin = [
!isNil(mfRuntimePlugin),
mfRuntimePlugin ?? new ModuleFederationRuntimePlugin()
];
}

raw(compiler: Compiler): BuiltinPlugin {
Expand All @@ -66,8 +74,14 @@ export class ContainerReferencePlugin extends RspackBuiltinPlugin {
}
}
new ExternalsPlugin(remoteType, remoteExternals).apply(compiler);
new ShareRuntimeSingletonPlugin().apply(compiler);
new RemoteRuntimeSingletonPlugin().apply(compiler);
const [injected, mfRuntimePlugin] = this._mfRuntimePlugin;
mfRuntimePlugin.addPlugin(
require.resolve("../sharing/initializeSharing.js")
);
mfRuntimePlugin.addPlugin(require.resolve("./remotesLoading.js"));
if (!injected) {
mfRuntimePlugin.apply(compiler);
}

return {
name: this.name as any,
Expand Down
16 changes: 11 additions & 5 deletions packages/rspack/src/container/ModuleFederationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { isValidate } from "../util/validate";
import { ContainerPlugin, Exposes } from "./ContainerPlugin";
import { ContainerReferencePlugin, Remotes } from "./ContainerReferencePlugin";
import { ModuleFederationRuntimePlugin } from "./ModuleFederationRuntimePlugin";

export interface ModuleFederationPluginOptions {
exposes?: Exposes;
Expand Down Expand Up @@ -38,6 +39,8 @@ export class ModuleFederationPlugin {
) {
compiler.options.output.enabledLibraryTypes!.push(library.type);
}
const mfRuntimePlugin = new ModuleFederationRuntimePlugin();
mfRuntimePlugin.apply(compiler);
compiler.hooks.afterPlugins.tap("ModuleFederationPlugin", () => {
if (
options.exposes &&
Expand All @@ -60,11 +63,14 @@ export class ModuleFederationPlugin {
? options.remotes.length > 0
: Object.keys(options.remotes).length > 0)
) {
new ContainerReferencePlugin({
remoteType,
shareScope: options.shareScope,
remotes: options.remotes
}).apply(compiler);
new ContainerReferencePlugin(
{
remoteType,
shareScope: options.shareScope,
remotes: options.remotes
},
mfRuntimePlugin
).apply(compiler);
}
// if (options.shared) {
// new SharePlugin({
Expand Down
21 changes: 18 additions & 3 deletions packages/rspack/src/container/ModuleFederationRuntimePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import { Compiler } from "../Compiler";
import { BuiltinPluginName, create } from "../builtin-plugin/base";
import { EntryPlugin } from "../builtin-plugin/EntryPlugin";

const ModuleFederationRuntimePlugin2 = create(
BuiltinPluginName.ModuleFederationRuntimePlugin,
() => undefined
);

export class ModuleFederationRuntimePlugin {
plugins: string[] = [];

apply(compiler: Compiler) {
// TODO: a hack to make sure this runtime is added after ContainerReferencePlugin
// remove afterPlugin once we make rust side runtime_requirements_in_tree "tapable"
compiler.hooks.afterPlugins.tap("ModuleFederationRuntimePlugin", () => {
new ModuleFederationRuntimePlugin2().apply(compiler);
});
compiler.hooks.afterPlugins.tap(
{ name: ModuleFederationRuntimePlugin.name, stage: 10 },
() => {
// TODO: move to rust side so don't depend on dataUrl
const entry = this.plugins.map(p => `import "${p}";`).join("\n");
new EntryPlugin(compiler.context, `data:text/javascript,${entry}`, {
name: undefined
}).apply(compiler);
new ModuleFederationRuntimePlugin2().apply(compiler);
}
);
}

addPlugin(dep: string) {
this.plugins.push(dep);
}
}
17 changes: 0 additions & 17 deletions packages/rspack/src/container/RemoteRuntimeSingletonPlugin.ts

This file was deleted.

137 changes: 70 additions & 67 deletions packages/rspack/src/container/remotesLoading.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,75 @@
// @ts-nocheck

__webpack_require__.MF.remotes = function (data) {
var chunkId = data.chunkId,
promises = data.promises,
chunkMapping = data.chunkMapping,
idToExternalAndNameMapping = data.idToExternalAndNameMapping;
if (__webpack_require__.o(chunkMapping, chunkId)) {
chunkMapping[chunkId].forEach(function (id) {
var getScope = __webpack_require__.R;
if (!getScope) getScope = [];
var data = idToExternalAndNameMapping[id];
if (getScope.indexOf(data) >= 0) return;
getScope.push(data);
if (data.p) return promises.push(data.p);
var onError = function (error) {
if (!error) error = new Error("Container missing");
if (typeof error.message === "string")
error.message += '\nwhile loading "' + data[1] + '" from ' + data[2];
__webpack_require__.m[id] = function () {
throw error;
if (__webpack_require__.MF) {
__webpack_require__.MF.remotes = function (data) {
var chunkId = data.chunkId,
promises = data.promises,
chunkMapping = data.chunkMapping,
idToExternalAndNameMapping = data.idToExternalAndNameMapping;
if (__webpack_require__.o(chunkMapping, chunkId)) {
chunkMapping[chunkId].forEach(function (id) {
var getScope = __webpack_require__.R;
if (!getScope) getScope = [];
var data = idToExternalAndNameMapping[id];
if (getScope.indexOf(data) >= 0) return;
getScope.push(data);
if (data.p) return promises.push(data.p);
var onError = function (error) {
if (!error) error = new Error("Container missing");
if (typeof error.message === "string")
error.message +=
'\nwhile loading "' + data[1] + '" from ' + data[2];
__webpack_require__.m[id] = function () {
throw error;
};
data.p = 0;
};
data.p = 0;
};
var handleFunction = function (fn, arg1, arg2, d, next, first) {
try {
var promise = fn(arg1, arg2);
if (promise && promise.then) {
var p = promise.then(function (result) {
return next(result, d);
}, onError);
if (first) promises.push((data.p = p));
else return p;
} else {
return next(promise, d, first);
var handleFunction = function (fn, arg1, arg2, d, next, first) {
try {
var promise = fn(arg1, arg2);
if (promise && promise.then) {
var p = promise.then(function (result) {
return next(result, d);
}, onError);
if (first) promises.push((data.p = p));
else return p;
} else {
return next(promise, d, first);
}
} catch (error) {
onError(error);
}
} catch (error) {
onError(error);
}
};
var onExternal = function (external, _, first) {
return external
? handleFunction(
__webpack_require__.I,
data[0],
0,
external,
onInitialized,
first
)
: onError();
};
var onInitialized = function (_, external, first) {
return handleFunction(
external.get,
data[1],
getScope,
0,
onFactory,
first
);
};
var onFactory = function (factory) {
data.p = 1;
__webpack_require__.m[id] = function (module) {
module.exports = factory();
};
};
handleFunction(__webpack_require__, data[2], 0, 0, onExternal, 1);
});
}
};
var onExternal = function (external, _, first) {
return external
? handleFunction(
__webpack_require__.I,
data[0],
0,
external,
onInitialized,
first
)
: onError();
};
var onInitialized = function (_, external, first) {
return handleFunction(
external.get,
data[1],
getScope,
0,
onFactory,
first
);
};
var onFactory = function (factory) {
data.p = 1;
__webpack_require__.m[id] = function (module) {
module.exports = factory();
};
};
handleFunction(__webpack_require__, data[2], 0, 0, onExternal, 1);
});
}
};
}
2 changes: 0 additions & 2 deletions packages/rspack/src/rspackOptionsApply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ export class RspackOptionsApply {
);
compiler.hooks.entryOption.call(options.context, options.entry);

new ModuleFederationRuntimePlugin().apply(compiler);

const { minimize, minimizer } = options.optimization;
if (minimize && minimizer) {
for (const item of minimizer) {
Expand Down
19 changes: 0 additions & 19 deletions packages/rspack/src/sharing/ShareRuntimeSingletonPlugin.ts

This file was deleted.

Loading

0 comments on commit 38c37be

Please sign in to comment.