Skip to content

Commit

Permalink
Merge pull request #16943 from snitin315/fix/mf-sharescope
Browse files Browse the repository at this point in the history
test: add case for ModuleFederationPlugin usage with shareScope option
  • Loading branch information
alexander-akait committed Apr 8, 2023
2 parents 3c0af27 + 4e2d51e commit f22773f
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 0 deletions.
@@ -0,0 +1,10 @@
import React from "react";
import ComponentA from "containerA/ComponentA";
import ComponentB from "containerB/ComponentB";
import LocalComponentB from "./ComponentB";

export default () => {
return `App rendered with [${React()}] and [${ComponentA()}] and [${ComponentB()}]`;
};

expect(ComponentB).not.toBe(LocalComponentB);
@@ -0,0 +1,5 @@
import React from "react";

export default () => {
return `ComponentB rendered with [${React()}]`;
};
@@ -0,0 +1,7 @@
import React from "react";
import ComponentA from "containerA/ComponentA";
import ComponentB from "containerB/ComponentB";

export default () => {
return `ComponentC rendered with [${React()}] and [${ComponentA()}] and [${ComponentB()}]`;
};
@@ -0,0 +1,20 @@
it("should load the component from container", async () => {
await __webpack_init_sharing__("test-scope");

// 2 scopes for "0-container-full-mjs" & "mf-with-shareScope-mjs"
expect(Object.keys(__webpack_share_scopes__["test-scope"].react).length).toBe(2);

return import("./App").then(({ default: App }) => {
const rendered = App();
expect(rendered).toBe(
"App rendered with [This is react 2.1.0] and [ComponentA rendered with [This is react 2.1.0]] and [ComponentB rendered with [This is react 2.1.0]]"
);
return import("./upgrade-react").then(({ default: upgrade }) => {
upgrade();
const rendered = App();
expect(rendered).toBe(
"App rendered with [This is react 3.2.1] and [ComponentA rendered with [This is react 3.2.1]] and [ComponentB rendered with [This is react 3.2.1]]"
);
});
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,9 @@
{
"private": true,
"engines": {
"node": ">=10.13.0"
},
"dependencies": {
"react": "*"
}
}
@@ -0,0 +1,5 @@
module.exports = {
findBundle: function (i, options) {
return i === 0 ? "./main.js" : "./module/main.mjs";
}
};
@@ -0,0 +1,5 @@
import { setVersion } from "react";

export default function upgrade() {
setVersion("3.2.1");
}
@@ -0,0 +1,68 @@
// eslint-disable-next-line node/no-unpublished-require
const { ModuleFederationPlugin } = require("../../../../").container;

const common = {
entry: {
main: "./index.js"
},
optimization: {
runtimeChunk: "single"
}
};

/** @type {ConstructorParameters<typeof ModuleFederationPlugin>[0]} */
const commonMF = {
runtime: false,
exposes: {
"./ComponentB": "./ComponentB",
"./ComponentC": "./ComponentC"
},
shared: ["react"],
shareScope: "test-scope"
};

/** @type {import("../../../../types").Configuration[]} */
module.exports = [
{
...common,
output: {
filename: "[name].js",
uniqueName: "mf-with-shareScope"
},
plugins: [
new ModuleFederationPlugin({
name: "container",
library: { type: "commonjs-module" },
filename: "container.js",
remotes: {
containerA: "../0-container-full/container.js",
containerB: "./container.js"
},
...commonMF
})
]
},
{
...common,
experiments: {
outputModule: true
},
output: {
filename: "module/[name].mjs",
uniqueName: "mf-with-shareScope-mjs"
},
plugins: [
new ModuleFederationPlugin({
name: "container",
library: { type: "module" },
filename: "module/container.mjs",
remotes: {
containerA: "../../0-container-full/module/container.mjs",
containerB: "./container.mjs"
},
...commonMF
})
],
target: "node14"
}
];

0 comments on commit f22773f

Please sign in to comment.