From a57000b7ea2d1dd7da95d62b13e10c1808e1005c Mon Sep 17 00:00:00 2001 From: zhangHongEn Date: Mon, 7 Oct 2024 23:58:48 +0800 Subject: [PATCH] feat: share custom path #131 --- ....timestamp-1728315610870-9397701f4e9c3.mjs | 52 +++++++++++++++++++ .../vite-vite/vite-remote/src/customShare.js | 3 ++ examples/vite-vite/vite-remote/src/main.jsx | 1 + examples/vite-vite/vite-remote/vite.config.js | 1 + src/utils/normalizeModuleFederationOptions.ts | 17 +++++- 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 examples/vite-vite/vite-host/vite.config.js.timestamp-1728315610870-9397701f4e9c3.mjs create mode 100644 examples/vite-vite/vite-remote/src/customShare.js diff --git a/examples/vite-vite/vite-host/vite.config.js.timestamp-1728315610870-9397701f4e9c3.mjs b/examples/vite-vite/vite-host/vite.config.js.timestamp-1728315610870-9397701f4e9c3.mjs new file mode 100644 index 0000000..f388ae1 --- /dev/null +++ b/examples/vite-vite/vite-host/vite.config.js.timestamp-1728315610870-9397701f4e9c3.mjs @@ -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/@vitejs+plugin-react@4.3.1_vite@5.4.2_@types+node@22.5.0_sass-embedded@1.77.8_terser@5.31.6_/node_modules/@vitejs/plugin-react/dist/index.mjs"; +import { defineConfig } from "file:///Users/zhanghongen/Desktop/zwork/open-code/vite8/node_modules/.pnpm/vite@5.4.2_@types+node@22.5.0_sass-embedded@1.77.8_terser@5.31.6/node_modules/vite/dist/node/index.js"; +import topLevelAwait from "file:///Users/zhanghongen/Desktop/zwork/open-code/vite8/node_modules/.pnpm/vite-plugin-top-level-await@1.4.4_@swc+helpers@0.5.3_rollup@4.21.0_vite@5.4.2_@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/mf-app-01@1.0.11/dist/remoteEntry.js", + remote2: "mfapp02@https://unpkg.com/mf-app-02/dist/remoteEntry.js", + remote3: "remote1@https://unpkg.com/react-manifest-example_remote1@1.0.6/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,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsidml0ZS5jb25maWcuanMiXSwKICAic291cmNlc0NvbnRlbnQiOiBbImNvbnN0IF9fdml0ZV9pbmplY3RlZF9vcmlnaW5hbF9kaXJuYW1lID0gXCIvVXNlcnMvemhhbmdob25nZW4vRGVza3RvcC96d29yay9vcGVuLWNvZGUvdml0ZTgvZXhhbXBsZXMvdml0ZS12aXRlL3ZpdGUtaG9zdFwiO2NvbnN0IF9fdml0ZV9pbmplY3RlZF9vcmlnaW5hbF9maWxlbmFtZSA9IFwiL1VzZXJzL3poYW5naG9uZ2VuL0Rlc2t0b3Avendvcmsvb3Blbi1jb2RlL3ZpdGU4L2V4YW1wbGVzL3ZpdGUtdml0ZS92aXRlLWhvc3Qvdml0ZS5jb25maWcuanNcIjtjb25zdCBfX3ZpdGVfaW5qZWN0ZWRfb3JpZ2luYWxfaW1wb3J0X21ldGFfdXJsID0gXCJmaWxlOi8vL1VzZXJzL3po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01 --- /dev/null +++ b/examples/vite-vite/vite-remote/src/customShare.js @@ -0,0 +1,3 @@ +export default function () { + console.log(12313123); +} diff --git a/examples/vite-vite/vite-remote/src/main.jsx b/examples/vite-vite/vite-remote/src/main.jsx index d1b4a91..7c2ad7c 100644 --- a/examples/vite-vite/vite-remote/src/main.jsx +++ b/examples/vite-vite/vite-remote/src/main.jsx @@ -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( diff --git a/examples/vite-vite/vite-remote/vite.config.js b/examples/vite-vite/vite-remote/vite.config.js index fb5e6b7..00343ac 100644 --- a/examples/vite-vite/vite-remote/vite.config.js +++ b/examples/vite-vite/vite-remote/vite.config.js @@ -30,6 +30,7 @@ export default defineConfig({ filename: 'remoteEntry-[hash].js', manifest: true, shared: { + './src/customShare': {}, vue: {}, 'react/': {}, react: { diff --git a/src/utils/normalizeModuleFederationOptions.ts b/src/utils/normalizeModuleFederationOptions.ts index 6fc071a..e6954ea 100644 --- a/src/utils/normalizeModuleFederationOptions.ts +++ b/src/utils/normalizeModuleFederationOptions.ts @@ -1,4 +1,5 @@ import { SharedConfig, ShareStrategy } from '@module-federation/runtime/types'; +import { isAbsolute } from "pathe"; export type RemoteEntryType = | 'var' @@ -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 { @@ -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) + '/'];