Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/metro-windows-path-comparisons.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/metro': patch
---

fix(metro): normalize remaining Windows path comparisons in the resolver and babel plugin. The init-host/remote-entry origin-module checks, the patched HMRClient check, and the babel plugin's `blacklistedPaths`/`state.filename` check now compare POSIX-normalized paths so they match correctly on Windows, where Metro and `path.resolve` can disagree on path separators.
42 changes: 42 additions & 0 deletions packages/metro-core/__tests__/plugin/resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,46 @@ describe('createResolveRequest', () => {
expect(fallbackResolver).not.toHaveBeenCalled();
},
);

it('matches the init-host origin module path regardless of separator (Windows)', () => {
// Metro and `path.resolve` can disagree on path separators on Windows.
// The generated init-host path uses native (backslash) separators while
// the origin module path reported by Metro may use POSIX separators.
const projectDir = 'C:\\project';
const tmpDir = 'C:\\project\\node_modules\\.mf-metro';
const initHost = 'C:\\project\\node_modules\\.mf-metro\\init-host.js';
const config = createConfig('lodash', 'lodash');
const fallbackResolver = vi.fn<CustomResolver>(() => ({
type: 'sourceFile',
filePath: '/fallback.js',
}));
// Metro reports the same file using POSIX separators
const context = createResolverContext(
'C:/project/node_modules/.mf-metro/init-host.js',
fallbackResolver,
);
const vmManager: Pick<VirtualModuleManager, 'registerVirtualModule'> = {
registerVirtualModule: vi.fn(),
};

const resolveRequest = createResolveRequest({
isRemote: false,
hacks: { patchHMRClient: false, patchInitializeCore: false },
options: config,
paths: {
...createPaths(projectDir, tmpDir),
initHost,
},
vmManager,
});

const resolved = resolveRequest(context, 'lodash', 'ios');

// init-host imports its shared deps directly, so the request should fall
// through to the host resolver instead of being rewritten to a virtual
// shared module.
expect(resolved).toEqual({ type: 'sourceFile', filePath: '/fallback.js' });
expect(fallbackResolver).toHaveBeenCalledTimes(1);
expect(vmManager.registerVirtualModule).not.toHaveBeenCalled();
});
});
13 changes: 12 additions & 1 deletion packages/metro-core/babel-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,23 @@ function getRejectedPromise(errorMessage) {
);
}

// normalize paths to POSIX separators so comparisons against generated
// paths work on Windows, where Metro and `path.resolve` can disagree
// on path separators
function toPosixPath(value) {
return typeof value === 'string' ? value.replace(/\\/g, '/') : value;
}

function moduleFederationMetroBabelPlugin() {
return {
name: 'module-federation-metro-babel-plugin',
visitor: {
CallExpression(path, state) {
if (state.opts.blacklistedPaths.includes(state.filename)) {
const filename = toPosixPath(state.filename);
const blacklistedPaths = (state.opts.blacklistedPaths || []).map(
toPosixPath,
);
if (blacklistedPaths.includes(filename)) {
return;
}

Expand Down
11 changes: 8 additions & 3 deletions packages/metro-core/src/plugin/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export function createResolveRequest({
});

return function resolveRequest(context, moduleName, platform) {
// normalize the origin module path so comparisons against generated
// paths work on Windows, where Metro and `path.resolve` can disagree
// on path separators
const originModulePath = toPosixPath(context.originModulePath);

// virtual entrypoint for host
if (moduleName.match(hostEntryPathRegex)) {
const hostEntryGenerator = () =>
Expand Down Expand Up @@ -119,7 +124,7 @@ export function createResolveRequest({
}

// shared modules handling in init-host.js
if ([paths.initHost].includes(context.originModulePath)) {
if (toPosixPath(paths.initHost) === originModulePath) {
// init-host contains definition of shared modules so we need to prevent
// circular import of shared module, by allowing import shared dependencies directly
return customResolver
Expand All @@ -128,7 +133,7 @@ export function createResolveRequest({
}

// shared modules handling in remote-entry.js
if ([paths.remoteEntry].includes(context.originModulePath)) {
if (toPosixPath(paths.remoteEntry) === originModulePath) {
const sharedModule = options.shared[moduleName];
// import: false means that the module is marked as external
if (sharedModule && sharedModule.import === false) {
Expand Down Expand Up @@ -192,7 +197,7 @@ export function createResolveRequest({
if (
hacks.patchHMRClient &&
moduleName.endsWith('HMRClient') &&
context.originModulePath !== resolveModule('HMRClient.ts')
originModulePath !== toPosixPath(resolveModule('HMRClient.ts'))
) {
const res = customResolver
? customResolver(context, moduleName, platform)
Expand Down
Loading