Skip to content

Commit

Permalink
use esm style export when type is module
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Onneby authored and poenneby committed Feb 7, 2025
1 parent a224e93 commit 7c8f166
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/plugins/pluginProxyRemotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function (options: NormalizedModuleFederationOptions): Plugin {
find: new RegExp(`^(${remote.name}(\/.*|$))`),
replacement: '$1',
customResolver(source: string) {
const remoteModule = getRemoteVirtualModule(source, _command);
const remoteModule = getRemoteVirtualModule(source, _command, remote.type === 'module');
addUsedRemote(remote.name, source);
return remoteModule.getPath();
},
Expand Down
12 changes: 6 additions & 6 deletions src/virtualModules/virtualRemotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const cacheRemoteMap: {
[remote: string]: VirtualModule;
} = {};
export const LOAD_REMOTE_TAG = '__loadRemote__';
export function getRemoteVirtualModule(remote: string, command: string) {
export function getRemoteVirtualModule(remote: string, command: string, esm = false) {
if (!cacheRemoteMap[remote]) {
cacheRemoteMap[remote] = new VirtualModule(remote, LOAD_REMOTE_TAG, '.js');
cacheRemoteMap[remote].writeSync(generateRemotes(remote, command));
cacheRemoteMap[remote].writeSync(generateRemotes(remote, command, esm));
}
const virtual = cacheRemoteMap[remote];
return virtual;
Expand All @@ -23,14 +23,14 @@ export function addUsedRemote(remoteKey: string, remoteModule: string) {
export function getUsedRemotesMap() {
return usedRemotesMap;
}
export function generateRemotes(id: string, command: string) {
export function generateRemotes(id: string, command: string, esm = false) {
return `
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
import {createRequire} from 'node:module'
const require = createRequire(import.meta.url)
const {loadRemote} = require("@module-federation/runtime")
const {initPromise} = require("${virtualRuntimeInitStatus.getImportId()}")
const res = initPromise.then(_ => loadRemote(${JSON.stringify(id)}))
const exportModule = ${command !== 'build' ? '/*mf top-level-await placeholder replacement mf*/' : 'await '}initPromise.then(_ => res)
export default exportModule;
${esm ? `export default exportModule` : `module.exports = exportModule`}
`;
}

0 comments on commit 7c8f166

Please sign in to comment.