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 33aab58
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
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 33aab58

Please sign in to comment.