Skip to content

Commit

Permalink
fix(metro-babel-transformer): migrate to @react-native/metro-babel-tr…
Browse files Browse the repository at this point in the history
…ansformer, disable babelRuntime

Use @react-native/metro-babel-transformer instead of metro-babel-transformer, setting
enableBabelRuntime: false to prevent app crash

fix #301
  • Loading branch information
vhqtvn committed Oct 8, 2024
1 parent 9d79116 commit 1dcdae8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions ci/bundle-rnjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ if(isWin) hermescCmd = hermescCmd.replace(/\//g,'\\');

const child2 = child_process.spawnSync(hermescCmd, [
"-emit-binary",
"-g3",
"-out","app/src/main/assets/loader.bundled.tmp.js",
"app/src/main/assets/loader.bundled.js",
"-O"
Expand Down
23 changes: 15 additions & 8 deletions ci/post-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ fs.rmSync("node_modules/resolve/test/resolver/malformed_package_json", { recursi
exec("npx rn-nodeify --hack");
exec("npx jetify");

hack("node_modules/@react-native/metro-babel-transformer/src/index.js", (s) => {
return s.replace(
/fs\.readFileSync\(__filename\)/sg,
JSON.stringify(require("crypto").randomBytes(20).toString('hex'))
)
})

//hack("node_modules/metro-react-native-babel-preset/src/configs/main.js", (s) => {
// // extraPlugins.push([
// // require("@babel/plugin-transform-react-jsx", {
Expand Down Expand Up @@ -107,14 +114,14 @@ hack([
hack([
"node_modules/@react-native/gradle-plugin/src/main/kotlin/com/facebook/react/TaskConfiguration.kt",
], (s) => {
// s=s.replace(
// "val bundleTask =",
// "/*val*//*bundleTask/**/=*/"
// )
// s=s.replace(
// "variant.sources.res?.addGeneratedSourceDirectory(bundleTask, BundleHermesCTask::resourcesDir)",
// "// variant.sources.res?./*we're embedding so, not-*/addGeneratedSourceDirectory(bundleTask, BundleHermesCTask::resourcesDir)"
// )
s=s.replace(
"val bundleTask =",
"/*val*//*bundleTask/**/=*/"
)
s=s.replace(
"variant.sources.res?.addGeneratedSourceDirectory(bundleTask, BundleHermesCTask::resourcesDir)",
"// variant.sources.res?./*we're embedding so, not-*/addGeneratedSourceDirectory(bundleTask, BundleHermesCTask::resourcesDir)"
)
s=s.replace(
"variant.sources.assets?.addGeneratedSourceDirectory(bundleTask, BundleHermesCTask::jsBundleDir)",
"// variant.sources.assets?./*we're embedding so, not-*/addGeneratedSourceDirectory(bundleTask, BundleHermesCTask::jsBundleDir)"
Expand Down
6 changes: 4 additions & 2 deletions js/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const normalize = function (url, base) {
process.version = 'vheditor-custom';
const BASEDIR = '/data/data/vn.vhn.vsc/files/home/vhe-modules';
process.cwd = () => BASEDIR;
const bundler = require('metro-babel-transformer').transform;
const bundler = require('@react-native/metro-babel-transformer').transform;
const generator = require("@babel/generator").default;
const require_cache = require('./require-cached');

Expand Down Expand Up @@ -64,7 +64,9 @@ function compile(content, path) {
const cache = compiler_cache(path, content);
[result, ok] = cache.read()
if (!ok) {
const transformed = bundler({ filename: path, options: {}, src: content })
// enabling babel runtime will cause the app to crash
// > You gave us a visitor for the node type ReferencedIdentifier but it's not a valid type
const transformed = bundler({ filename: path, options: {enableBabelRuntime: false}, src: content })
result = generator(transformed.ast, {}, content)
cache.write(JSON.stringify(result))
ok = true
Expand Down

0 comments on commit 1dcdae8

Please sign in to comment.