Skip to content

Commit 33aab58

Browse files
committed
feat: share custom path module-federation#131
1 parent ef9bc5a commit 33aab58

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function () {
2+
console.log(12313123);
3+
}

examples/vite-vite/vite-remote/src/main.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { App2 } from './App2';
77
import { EmotionDemo } from './EmotionDemo';
88
import { MuiDemo } from './MuiDemo';
99
import StyledDemo from './StyledDemo';
10+
import "./customShare";
1011

1112
const root = ReactDOM.createRoot(document.getElementById('app'));
1213
root.render(

examples/vite-vite/vite-remote/vite.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default defineConfig({
3030
filename: 'remoteEntry-[hash].js',
3131
manifest: true,
3232
shared: {
33+
'./src/customShare': {},
3334
vue: {},
3435
'react/': {},
3536
react: {

src/utils/normalizeModuleFederationOptions.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SharedConfig, ShareStrategy } from '@module-federation/runtime/types';
2+
import { isAbsolute } from "pathe";
23

34
export type RemoteEntryType =
45
| 'var'
@@ -132,9 +133,13 @@ function normalizeShareItem(
132133
): ShareItem {
133134
let version: string | undefined;
134135
try {
135-
version = require(path.join(removePathFromNpmPackage(key), 'package.json')).version;
136+
if (isAbsolute(key) || key.startsWith("./")) {
137+
version = "1.0.0"
138+
} else {
139+
version = require(path.join(removePathFromNpmPackage(key), 'package.json')).version;
140+
}
136141
} catch (e) {
137-
console.log(e);
142+
console.log(e)
138143
}
139144
if (typeof shareItem === 'string') {
140145
return {
@@ -313,6 +318,14 @@ export function getNormalizeModuleFederationOptions() {
313318

314319
export function getNormalizeShareItem(key: string) {
315320
const options = getNormalizeModuleFederationOptions();
321+
if (isAbsolute(key) || key.startsWith("./")) {
322+
const shareItem = Object.keys(options.shared).filter(shareKey => {
323+
if (shareKey.startsWith(key)) {
324+
return
325+
}
326+
})?.[0]
327+
if (shareItem) return shareItem
328+
}
316329
const shareItem =
317330
options.shared[removePathFromNpmPackage(key)] ||
318331
options.shared[removePathFromNpmPackage(key) + '/'];

0 commit comments

Comments
 (0)