Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 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
8 changes: 8 additions & 0 deletions packages/metro-core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
"options": {
"lintFilePatterns": ["packages/metro-core/**/*.{ts,tsx,js,jsx}"]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/packages/metro-core"],
"options": {
"jestConfig": "packages/metro-core/jest.config.ts",
"passWithNoTests": true
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs aligning with main, uses vitest now

}
}
}
2 changes: 1 addition & 1 deletion packages/metro-core/src/babel/transformer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-undef */
const babelTransformer = require('__BABEL_TRANSFORMER_PATH__');
const babelTransformer = require(__BABEL_TRANSFORMER_PATH__);
const babelPlugins = __BABEL_PLUGINS__;
/* eslint-enable no-undef */

Expand Down
39 changes: 39 additions & 0 deletions packages/metro-core/src/plugin/__tests__/babel-transformer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { createBabelTransformer } from '../babel-transformer';
import type { ModuleFederationConfigNormalized } from '../../types';

function createConfig(): ModuleFederationConfigNormalized {
return {
name: 'test-app',
filename: 'remote.js',
remotes: {},
exposes: {},
shared: {},
shareStrategy: 'loaded-first',
plugins: [],
};
}

describe('createBabelTransformer', () => {
it('escapes Windows paths for require()', () => {
const tmpDirPath = fs.mkdtempSync(path.join(os.tmpdir(), 'mf-metro-'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memfs perhaps?

const windowsPath =
'C:\\Users\\someone\\project\\node_modules\\metro-babel-transformer\\src\\index.js';

const outputPath = createBabelTransformer({
blacklistedPaths: [],
federationConfig: createConfig(),
originalBabelTransformerPath: windowsPath,
tmpDirPath,
enableInitializeCorePatching: false,
enableRuntimeRequirePatching: false,
});

const output = fs.readFileSync(outputPath, 'utf-8');
expect(output).toContain(`require(${JSON.stringify(windowsPath)})`);

fs.rmSync(tmpDirPath, { recursive: true, force: true });
});
});
5 changes: 4 additions & 1 deletion packages/metro-core/src/plugin/babel-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ export function createBabelTransformer({
].filter(Boolean);

const babelTransformer = transformerTemplate
.replaceAll('__BABEL_TRANSFORMER_PATH__', originalBabelTransformerPath)
.replaceAll(
'__BABEL_TRANSFORMER_PATH__',
JSON.stringify(originalBabelTransformerPath),
)
.replaceAll('__BABEL_PLUGINS__', JSON.stringify(plugins));

fs.writeFileSync(outputPath, babelTransformer, 'utf-8');
Expand Down
Loading