Skip to content

Commit 90f0f16

Browse files
author
jingjing2222
committed
feat: visitor order
1 parent 7216951 commit 90f0f16

File tree

1 file changed

+67
-53
lines changed

1 file changed

+67
-53
lines changed

packages/plugin-radon/src/babel.js

Lines changed: 67 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,61 @@ const { injectGraniteGlobals } = require('./lib/granite_router/global_injector')
66
const { processPageFile } = require('./lib/granite_router/navigation_injector');
77
const { parseRouterGenFile } = require('./lib/granite_router/router_parser');
88

9-
module.exports = function(api, options = {}) {
9+
module.exports = function (api, options = {}) {
1010
api.assertVersion(7);
11-
11+
1212
const { parse, types: t } = api;
13-
13+
1414
const appRoot = getPackageRoot();
15-
15+
1616
const scanGraniteRoutes = () => {
1717
try {
1818
const routerGenPath = path.join(appRoot, 'src', 'router.gen.ts');
19-
19+
2020
if (fs.existsSync(routerGenPath)) {
2121
return parseRouterGenFile(parse, routerGenPath);
2222
}
23-
2423
} catch (error) {
2524
console.error('🔥 RADON BABEL PLUGIN: Route scanning failed:', error);
26-
return [{
27-
path: "/",
28-
filePath: "./pages/index.tsx",
29-
type: "route"
30-
}];
25+
return [
26+
{
27+
path: '/',
28+
filePath: './pages/index.tsx',
29+
type: 'route',
30+
},
31+
];
3132
}
3233
};
33-
34-
3534

3635
const requireFromAppDir = (module) => {
3736
const resolvedPath = require.resolve(module, { paths: [appRoot] });
3837
return require(resolvedPath);
3938
};
4039

4140
// --- Helper Functions ---
42-
41+
4342
// Function to generate paths for bundled renderer files
4443
const createRendererPath = (rendererFileName, version) => {
4544
try {
4645
const pluginPackageJsonPath = require.resolve('@granite-js/plugin-radon/package.json', { paths: [appRoot] });
4746
const pluginRoot = path.dirname(pluginPackageJsonPath);
48-
47+
4948
let versionFolder;
50-
if (version.startsWith("0.72")) {
51-
versionFolder = "react-native-72";
52-
}
53-
49+
if (version.startsWith('0.72')) {
50+
versionFolder = 'react-native-72';
51+
}
52+
5453
if (versionFolder) {
55-
const rendererPath = path.join(pluginRoot, 'dist', 'lib', 'rn-renderer', rendererFileName.replace('.js', '.cjs'));
54+
const rendererPath = path.join(
55+
pluginRoot,
56+
'dist',
57+
'lib',
58+
'rn-renderer',
59+
rendererFileName.replace('.js', '.cjs')
60+
);
5661
return rendererPath;
5762
}
58-
63+
5964
return null;
6065
} catch (e) {
6166
console.error('🔥 RADON BABEL PLUGIN: Failed to resolve renderer path:', e);
@@ -64,17 +69,25 @@ module.exports = function(api, options = {}) {
6469
};
6570

6671
const injectCode = (programPath, code, prepend = false) => {
67-
const ast = parse(code, { sourceType: 'module', filename: 'radon.injection.js', parserOpts: { allowReturnOutsideFunction: true } });
72+
const ast = parse(code, {
73+
sourceType: 'module',
74+
filename: 'radon.injection.js',
75+
parserOpts: { allowReturnOutsideFunction: true },
76+
});
6877
if (prepend) {
6978
programPath.unshiftContainer('body', ast.program.body);
7079
} else {
7180
programPath.pushContainer('body', ast.program.body);
7281
}
7382
};
74-
83+
7584
const replaceModuleWith = (programPath, code) => {
76-
const ast = parse(code, { sourceType: 'module', filename: 'radon.injection.js', parserOpts: { allowReturnOutsideFunction: true } });
77-
programPath.get('body').forEach(p => p.remove());
85+
const ast = parse(code, {
86+
sourceType: 'module',
87+
filename: 'radon.injection.js',
88+
parserOpts: { allowReturnOutsideFunction: true },
89+
});
90+
programPath.get('body').forEach((p) => p.remove());
7891
programPath.pushContainer('body', ast.program.body);
7992
};
8093

@@ -83,13 +96,15 @@ module.exports = function(api, options = {}) {
8396
return {
8497
name: 'radon-injector-plugin',
8598
visitor: {
99+
...jsxSourceVisitor,
100+
86101
Program: {
87102
enter(programPath, state) {
88103
const filename = state.file.opts.filename;
89104
if (!filename || state.file.metadata.radonInjected) {
90105
return;
91106
}
92-
107+
93108
const isTransforming = (modulePath) => {
94109
try {
95110
const resolvedPath = require.resolve(modulePath, { paths: [appRoot] });
@@ -100,21 +115,19 @@ module.exports = function(api, options = {}) {
100115
};
101116

102117
let injected = false;
103-
118+
104119
// This MUST be the first check.
105-
106-
if (isTransforming("react-native/Libraries/Renderer/implementations/ReactFabric-dev.js") ||
107-
isTransforming("react-native/Libraries/Renderer/implementations/ReactNativeRenderer-dev.js")) {
108-
109-
110-
const { version } = requireFromAppDir("react-native/package.json");
120+
121+
if (
122+
isTransforming('react-native/Libraries/Renderer/implementations/ReactFabric-dev.js') ||
123+
isTransforming('react-native/Libraries/Renderer/implementations/ReactNativeRenderer-dev.js')
124+
) {
125+
const { version } = requireFromAppDir('react-native/package.json');
111126
const rendererFileName = path.basename(filename);
112-
113-
127+
114128
const rendererPath = createRendererPath(rendererFileName, version);
115-
129+
116130
if (rendererPath && fs.existsSync(rendererPath)) {
117-
118131
try {
119132
const rendererCode = fs.readFileSync(rendererPath, 'utf8');
120133
// Add a fingerprint to verify the custom renderer is loaded.
@@ -128,21 +141,25 @@ module.exports = function(api, options = {}) {
128141
}
129142
} else {
130143
console.warn(`🔥 RADON BABEL PLUGIN: ⚠️ Custom renderer not found: ${rendererPath}`);
131-
if (version.startsWith("0.72")) {
132-
console.log(`🔥 RADON BABEL PLUGIN: ⚠️ 0.72 version detected but no custom renderer found. This might be the issue!`);
144+
if (version.startsWith('0.72')) {
145+
console.log(
146+
`🔥 RADON BABEL PLUGIN: ⚠️ 0.72 version detected but no custom renderer found. This might be the issue!`
147+
);
133148
}
134149
}
135150
}
136-
151+
137152
if (injected) {
138153
state.file.metadata.radonInjected = true;
139154
}
140-
if (isTransforming("react-native/Libraries/Core/InitializeCore.js") && !options.disableRuntimeInjection) {
155+
if (isTransforming('react-native/Libraries/Core/InitializeCore.js') && !options.disableRuntimeInjection) {
141156
try {
142-
const pluginPackageJsonPath = require.resolve('@granite-js/plugin-radon/package.json', { paths: [appRoot] });
157+
const pluginPackageJsonPath = require.resolve('@granite-js/plugin-radon/package.json', {
158+
paths: [appRoot],
159+
});
143160
const pluginRoot = path.dirname(pluginPackageJsonPath);
144161
const runtimePath = path.join(pluginRoot, 'dist', 'lib', 'runtime.cjs');
145-
162+
146163
const runtimeCode = fs.readFileSync(runtimePath, 'utf8');
147164

148165
const devtoolsPort = process.env.RCT_DEVTOOLS_PORT;
@@ -156,7 +173,7 @@ module.exports = function(api, options = {}) {
156173
const finalSafeCode = `setImmediate(() => { try { \n${finalCodeToInject}\n } catch (e) { console.error('Radon runtime error:', e); } });`;
157174

158175
injectCode(programPath, finalSafeCode, false); // Append to the end
159-
176+
160177
state.file.metadata.radonInjected = true;
161178
} catch (e) {
162179
console.error('🔥 RADON BABEL PLUGIN: 🚨 FAILED TO READ RUNTIME BUNDLE.', e);
@@ -166,26 +183,23 @@ module.exports = function(api, options = {}) {
166183
return;
167184
}
168185

169-
if (isTransforming("@granite-js/react-native")) {
186+
if (isTransforming('@granite-js/react-native')) {
170187
try {
171188
const scannedRoutes = scanGraniteRoutes();
172189
const injected = injectGraniteGlobals(injectCode, programPath, scannedRoutes);
173-
190+
174191
if (injected) {
175192
state.file.metadata.radonInjected = true;
176193
}
177194
} catch (e) {
178195
console.error('🔥 RADON BABEL PLUGIN: Failed to inject Granite detection code:', e);
179-
}
196+
}
180197
}
181198

182199
// Process page files for navigation auto-registration
183200
processPageFile(filename, programPath, parse, t, state);
184-
}
201+
},
185202
},
186-
187-
// Add JSX Source visitor here
188-
...jsxSourceVisitor
189-
}
203+
},
190204
};
191-
};
205+
};

0 commit comments

Comments
 (0)