Skip to content

Commit

Permalink
fix: dynamic import
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangHongEn committed Aug 6, 2024
1 parent 0435fc0 commit fa9ae42
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
4 changes: 2 additions & 2 deletions examples/vite/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Mfapp01App from 'mfapp01/App';
import R from 'react';
import RD from 'react-dom';
import Mfapp01App from 'mfapp01/App';
import Remote2App from 'remote2/App';
import Button from 'remote3/button';
import { ref } from 'vue';

console.log('share vue', ref);
console.log('share React', R, RD);
console.log('share React', R, RD, import('remote3/button'), import('react'));

export default function () {
return (
Expand Down
54 changes: 38 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ function generateRemoteEntry(options: NormalizedModuleFederationOptions): string
async function init(shared = {}) {
const localShared = {
${Object.keys(options.shared)
.map((key) => {
const shareItem = options.shared[key];
return `
.map((key) => {
const shareItem = options.shared[key];
return `
${JSON.stringify(key)}: {
name: ${JSON.stringify(shareItem.name)},
version: ${JSON.stringify(shareItem.version)},
Expand All @@ -82,24 +82,24 @@ function generateRemoteEntry(options: NormalizedModuleFederationOptions): string
}
}
`;
})
.join(',')}
})
.join(',')}
}
const initRes = runtimeInit({
name: ${JSON.stringify(options.name)},
remotes: [${Object.keys(options.remotes)
.map((key) => {
const remote = options.remotes[key];
return `
.map((key) => {
const remote = options.remotes[key];
return `
{
entryGlobalName: ${JSON.stringify(remote.entryGlobalName)},
name: ${JSON.stringify(remote.name)},
type: ${JSON.stringify(remote.type)},
entry: ${JSON.stringify(remote.entry)},
}
`;
})
.join(',')}
})
.join(',')}
],
shared: localShared,
plugins: [${pluginImportNames.map((item) => `${item[0]}()`).join(', ')}]
Expand Down Expand Up @@ -133,10 +133,22 @@ function wrapShare(
strictVersion: ${JSON.stringify(shareConfig.strictVersion)},
requiredVersion: ${JSON.stringify(shareConfig.requiredVersion)}
}}})
export default res()
const exportModule = res()
${(command === "build" &&
`
export default 'default' in (exportModule || {}) ? exportModule.default : exportModule
export const __mf__dynamicExports = exportModule
`
) || ""}
${(command !== "build" &&
`
export default exportModule
`
) || ""}
`,
map: null,
syntheticNamedExports: 'default',
// TODO: vite dev mode invalid, use optimizeDeps.needsInterop
syntheticNamedExports: '__mf__dynamicExports',
};
}

Expand All @@ -145,12 +157,22 @@ function wrapRemote(id: string): { code: string; map: null; syntheticNamedExport
return {
code: `
import {loadRemote} from "@module-federation/runtime"
export ${
command !== 'build' ? 'default' : 'const dynamicExport = '
} await loadRemote(${JSON.stringify(id)})
const exportModule = await loadRemote(${JSON.stringify(id)})
${(command === "build" &&
`
export default 'default' in (exportModule || {}) ? exportModule.default : undefined
export const __mf__dynamicExports = exportModule
`
) || ""}
${(command !== "build" &&
`
export default exportModule
`
) || ""}
`,
map: null,
syntheticNamedExports: 'dynamicExport',
// TODO: vite dev mode invalid, use optimizeDeps.needsInterop
syntheticNamedExports: '__mf__dynamicExports',
};
}

Expand Down
8 changes: 2 additions & 6 deletions src/utils/vitePluginOverrideModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,10 @@ export function overrideModule(
if (node.type === 'ExportNamedDeclaration' || node.type === 'ExportAllDeclaration') {
replaceIfMatch(node.source);
}

if (
node.type === 'CallExpression' &&
node.callee.type === 'Import' &&
node.arguments.length &&
node.arguments[0].type === 'Literal'
node.type === 'ImportExpression'
) {
replaceIfMatch(node.arguments[0]);
replaceIfMatch(node.source);
}

if (
Expand Down

0 comments on commit fa9ae42

Please sign in to comment.