Skip to content

Commit 4a054f2

Browse files
authored
feat: add unstable_patchInitializeCore (#88)
1 parent 9d719e9 commit 4a054f2

File tree

19 files changed

+152
-72
lines changed

19 files changed

+152
-72
lines changed

apps/example-host/metro.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ module.exports = withModuleFederation(
5252
{
5353
flags: {
5454
unstable_patchHMRClient: true,
55+
unstable_patchInitializeCore: true,
5556
unstable_patchRuntimeRequire: true,
5657
},
5758
}

apps/example-mini/metro.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ module.exports = withModuleFederation(
5151
{
5252
flags: {
5353
unstable_patchHMRClient: true,
54+
unstable_patchInitializeCore: true,
5455
unstable_patchRuntimeRequire: true,
5556
},
5657
}

apps/example-nested-mini/metro.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module.exports = withModuleFederation(
5555
{
5656
flags: {
5757
unstable_patchHMRClient: true,
58+
unstable_patchInitializeCore: true,
5859
unstable_patchRuntimeRequire: true,
5960
},
6061
}

apps/showcase-host/metro.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module.exports = withModuleFederation(
5757
{
5858
flags: {
5959
unstable_patchHMRClient: true,
60+
unstable_patchInitializeCore: true,
6061
unstable_patchRuntimeRequire: true,
6162
},
6263
}

apps/showcase-mini/metro.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module.exports = withModuleFederation(
6060
{
6161
flags: {
6262
unstable_patchHMRClient: true,
63+
unstable_patchInitializeCore: true,
6364
unstable_patchRuntimeRequire: true,
6465
},
6566
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// reuse `@babel/types` from `metro`
2+
const metroPath = require.resolve('metro');
3+
const babelTypesPath = require.resolve('@babel/types', { paths: [metroPath] });
4+
const t = require(babelTypesPath);
5+
6+
/**
7+
* Inject require('mf:init-host') right after 'use strict' directive
8+
* in React Native's Initialize Core
9+
*/
10+
function injectInitHostRequire(path, state) {
11+
// Only process once per file
12+
if (state.hasInjected) return;
13+
14+
let insertIndex = 0;
15+
16+
// Find the position after 'use strict' directive
17+
for (let i = 0; i < path.node.body.length; i++) {
18+
const node = path.node.body[i];
19+
if (
20+
t.isExpressionStatement(node) &&
21+
t.isStringLiteral(node.expression) &&
22+
node.expression.value === 'use strict'
23+
) {
24+
insertIndex = i + 1;
25+
break;
26+
}
27+
}
28+
29+
// Create the require('mf:init-host') statement
30+
const requireStatement = t.expressionStatement(
31+
t.callExpression(t.identifier('require'), [t.stringLiteral('mf:init-host')])
32+
);
33+
34+
// Insert the require statement
35+
path.node.body.splice(insertIndex, 0, requireStatement);
36+
state.hasInjected = true;
37+
}
38+
39+
function metroPatchInitializeCorePlugin() {
40+
return {
41+
name: 'module-federation-metro-patch-initialize-core',
42+
visitor: {
43+
Program: {
44+
enter(_, state) {
45+
state.hasInjected = false;
46+
state.shouldTransform = state.file.opts.filename.includes(
47+
'react-native/Libraries/Core/InitializeCore.js'
48+
);
49+
},
50+
exit(path, state) {
51+
if (!state.shouldTransform) return;
52+
injectInitHostRequire(path, state);
53+
},
54+
},
55+
},
56+
};
57+
}
58+
59+
module.exports = metroPatchInitializeCorePlugin;

packages/core/babel-plugin/patch-require.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function patchRequireRefresh(path) {
135135

136136
function metroPatchRequireBabelPlugin() {
137137
return {
138-
name: 'metro-patch-require',
138+
name: 'module-federation-metro-patch-require',
139139
visitor: {
140140
Program: {
141141
enter(_, state) {

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"default": "./dist/commands/index.js"
3636
},
3737
"./babel-plugin": "./babel-plugin/index.js",
38-
"./babel-plugin/patch-require": "./babel-plugin/patch-require.js",
38+
"./babel-plugin/*": "./babel-plugin/*.js",
3939
"./bootstrap": {
4040
"dev": "./src/modules/asyncStartup.tsx",
4141
"default": "./dist/modules/asyncStartup.tsx"

packages/core/src/babel/transformer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const babelTransformer = require('__BABEL_TRANSFORMER_PATH__');
2+
const babelPlugins = __BABEL_PLUGINS__;
23

34
function transform(config) {
45
return babelTransformer.transform({
56
...config,
6-
plugins: [...__BABEL_PLUGINS__, ...config.plugins],
7+
plugins: [...babelPlugins, ...config.plugins],
78
});
89
}
910

packages/core/src/plugin/babel-transformer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface CreateBabelTransformerOptions {
77
federationConfig: ModuleFederationConfigNormalized;
88
originalBabelTransformerPath: string;
99
tmpDirPath: string;
10+
enableInitializeCorePatching: boolean;
1011
enableRuntimeRequirePatching: boolean;
1112
}
1213

@@ -15,6 +16,7 @@ export function createBabelTransformer({
1516
federationConfig,
1617
originalBabelTransformerPath,
1718
tmpDirPath,
19+
enableInitializeCorePatching,
1820
enableRuntimeRequirePatching,
1921
}: CreateBabelTransformerOptions) {
2022
const outputPath = path.join(tmpDirPath, 'babel-transformer.js');
@@ -30,6 +32,9 @@ export function createBabelTransformer({
3032
shared: federationConfig.shared,
3133
},
3234
],
35+
enableInitializeCorePatching
36+
? '@module-federation/metro/babel-plugin/patch-initialize-core'
37+
: undefined,
3338
enableRuntimeRequirePatching
3439
? '@module-federation/metro/babel-plugin/patch-require'
3540
: undefined,

0 commit comments

Comments
 (0)