Skip to content

Commit

Permalink
feat: share custom path module-federation#131
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangHongEn committed Oct 7, 2024
1 parent ef9bc5a commit a57000b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// vite.config.js
import { federation } from "file:///Users/zhanghongen/Desktop/zwork/open-code/vite8/lib/index.cjs";
import react from "file:///Users/zhanghongen/Desktop/zwork/open-code/vite8/node_modules/.pnpm/@[email protected][email protected]_@[email protected][email protected][email protected]_/node_modules/@vitejs/plugin-react/dist/index.mjs";
import { defineConfig } from "file:///Users/zhanghongen/Desktop/zwork/open-code/vite8/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]/node_modules/vite/dist/node/index.js";
import topLevelAwait from "file:///Users/zhanghongen/Desktop/zwork/open-code/vite8/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected]_@types+node@22._krbzckfzdnw5tslgq6xfzhp7ly/node_modules/vite-plugin-top-level-await/exports/import.mjs";
var vite_config_default = defineConfig({
server: {
open: true,
port: 5175
},
preview: {
port: 5175
},
// base: 'http://localhost:5175',
plugins: [
react(),
federation({
name: "viteViteHost",
remotes: {
mfapp01: "mfapp01@https://unpkg.com/[email protected]/dist/remoteEntry.js",
remote2: "mfapp02@https://unpkg.com/mf-app-02/dist/remoteEntry.js",
remote3: "remote1@https://unpkg.com/[email protected]/dist/mf-manifest.json",
"@namespace/viteViteRemote": "http://localhost:5176/mf-manifest.json"
},
filename: "remoteEntry-[hash].js",
manifest: true,
shared: {
vue: {},
"react/": {
requiredVersion: "18"
},
"react-dom": {},
"ag-grid-community": {},
"ag-grid-react": {},
"@emotion/react": {},
"styled-components": { singleton: true },
"@emotion/styled": {},
"@mui/material": {}
},
runtimePlugins: ["./src/mfPlugins"]
}),
// If you set build.target: "chrome89", you can remove this plugin
false
],
build: {
target: "chrome89"
}
});
export {
vite_config_default as default
};
//# sourceMappingURL=data:application/json;base64,ewog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BQSxRQUNuQjtBQUFBLFFBQ0EsYUFBYSxDQUFDO0FBQUEsUUFDZCxxQkFBcUIsQ0FBQztBQUFBLFFBQ3RCLGlCQUFpQixDQUFDO0FBQUEsUUFDbEIsa0JBQWtCLENBQUM7QUFBQSxRQUNuQixxQkFBcUIsRUFBRSxXQUFXLEtBQUs7QUFBQSxRQUN2QyxtQkFBbUIsQ0FBQztBQUFBLFFBQ3BCLGlCQUFpQixDQUFDO0FBQUEsTUFDcEI7QUFBQSxNQUNBLGdCQUFnQixDQUFDLGlCQUFpQjtBQUFBLElBQ3BDLENBQUM7QUFBQTtBQUFBLElBRUQ7QUFBQSxFQUNGO0FBQUEsRUFDQSxPQUFPO0FBQUEsSUFDTCxRQUFRO0FBQUEsRUFDVjtBQUNGLENBQUM7IiwKICAibmFtZXMiOiBbXQp9Cg==
3 changes: 3 additions & 0 deletions examples/vite-vite/vite-remote/src/customShare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function () {
console.log(12313123);
}
1 change: 1 addition & 0 deletions examples/vite-vite/vite-remote/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { App2 } from './App2';
import { EmotionDemo } from './EmotionDemo';
import { MuiDemo } from './MuiDemo';
import StyledDemo from './StyledDemo';
import "./customShare";

const root = ReactDOM.createRoot(document.getElementById('app'));
root.render(
Expand Down
1 change: 1 addition & 0 deletions examples/vite-vite/vite-remote/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default defineConfig({
filename: 'remoteEntry-[hash].js',
manifest: true,
shared: {
'./src/customShare': {},
vue: {},
'react/': {},
react: {
Expand Down
17 changes: 15 additions & 2 deletions src/utils/normalizeModuleFederationOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SharedConfig, ShareStrategy } from '@module-federation/runtime/types';
import { isAbsolute } from "pathe";

export type RemoteEntryType =
| 'var'
Expand Down Expand Up @@ -132,9 +133,13 @@ function normalizeShareItem(
): ShareItem {
let version: string | undefined;
try {
version = require(path.join(removePathFromNpmPackage(key), 'package.json')).version;
if (isAbsolute(key) || key.startsWith("./")) {
version = "1.0.0"
} else {
version = require(path.join(removePathFromNpmPackage(key), 'package.json')).version;
}
} catch (e) {
console.log(e);
console.log(e)
}
if (typeof shareItem === 'string') {
return {
Expand Down Expand Up @@ -313,6 +318,14 @@ export function getNormalizeModuleFederationOptions() {

export function getNormalizeShareItem(key: string) {
const options = getNormalizeModuleFederationOptions();
if (isAbsolute(key) || key.startsWith("./")) {
const shareItem = Object.keys(options.shared).filter(shareKey => {
if (shareKey.startsWith(key)) {
return
}
})?.[0]
if (shareItem) return shareItem
}
const shareItem =
options.shared[removePathFromNpmPackage(key)] ||
options.shared[removePathFromNpmPackage(key) + '/'];
Expand Down

0 comments on commit a57000b

Please sign in to comment.