javascript-obfuscator / react-native-obfuscating-transformer

Obfuscation for React Native bundles
MIT License
196 stars 100 forks source link

TypeError: Cannot read property 'transformFile' of undefined #16

Open ExoMemphiz opened 5 years ago

ExoMemphiz commented 5 years ago

Versions:

"react-native": "0.59.10", "react-native-obfuscating-transformer": "^1.0.0",

Files:

metro.config.js:

module.exports = {
    transformer: {
        babelTransformerPath: require.resolve("./transformer"),
        getTransformOptions: async () => ({
            transform: {
                experimentalImportSupport: false,
                inlineRequires: false,
            },
        }),
    },
};

transformer.js:

const obfuscatingTransformer = require('react-native-obfuscating-transformer')

module.exports = obfuscatingTransformer({})

Error I am getting:

Loading dependency graph, done.
Loading dependency graph...error Cannot read property 'transformFile' of undefined
debug TypeError: Cannot read property 'transformFile' of undefined
    at /home/chris/Documents/JSObfuscation/ObfuscationTest/node_modules/metro/src/Bundler.js:83:34
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/home/chris/Documents/JSObfuscation/ObfuscationTest/node_modules/metro/src/Bundler.js:14:24)
    at _next (/home/chris/Documents/JSObfuscation/ObfuscationTest/node_modules/metro/src/Bundler.js:34:9)

Repo

Simplest repo I could make that reproduces this error:

https://github.com/ExoMemphiz/ObfuscationTest/

ExoMemphiz commented 5 years ago

Alright, I got slightly further with a different repo that I found that used this library.

I needed to add these lines of code to the getMetroTransformer.js file:

const metroVersion = semver(require('metro/package.json').version).minor;
if (metroVersion >= 51) {
  return require('metro-react-native-babel-transformer/src/index');
}

And of course have the metro-react-native-babel-transformer installed, as well as having this .babelrc file:

{
  "presets": ["module:metro-react-native-babel-preset"]
}

It seems to work (Tried react-native v0.59.10 and v0.61.1 currently), but minification happens on the bundle, after obfuscating, which I think is the reason that the output is basically the same, if I obfuscate or not.

Btw. these are the dependencies:

  "dependencies": {
    "react": "16.8.3",
    "react-native": "0.61.1"
  },
  "devDependencies": {
    "babel-core": "6.26.0",
    "metro-react-native-babel-preset": "0.56.0",
    "react-native-obfuscating-transformer": "^1.0.0-rc.0"
  }

I get this in the log when I run the ./gradlew assembleRelease

> Task :app:bundleReleaseJsAndAssets
warning: the transform cache was reset.
Loading dependency graph, done.
transform[stdout]: src/App.js
transform[stdout]: Obfuscating src/App.js
info Writing bundle output to:, .../index.android.bundle
info Done writing bundle output

Which should mean it actually obfuscates the code, unfortunately it is not shown in the output bundle itself. Also the selfDefending option breaks the app, I assume because of the above reason - minifying the output changes the code, and thus breaks the selfDefending code.

Any way to keep the obfuscated code, while bundling it?

mrcarjul commented 4 years ago

I have the same issue, did you managed to solve it?

jamal-rahimzadegan commented 4 years ago

same here

anhdevit commented 4 years ago

Can every one give me the solution?

anhdevit commented 4 years ago

I find the solution when using with react-native 0.61 This is my metro.config.js My problem disappear when I change babelTransformerPath: require.resolve("./transformer") to getTransformModulePath: () => { return require.resolve('./transformer'); },

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
    getTransformModulePath: () => {
      return require.resolve('./transformer');
    },
  },
};
mahsanfarooq commented 4 years ago

I find the solution when using with react-native 0.61 This is my metro.config.js My problem disappear when I change babelTransformerPath: require.resolve("./transformer") to getTransformModulePath: () => { return require.resolve('./transformer'); },

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
    getTransformModulePath: () => {
      return require.resolve('./transformer');
    },
  },
};

yes this issue has been resolved from this Solution. thanks a lot man.

timorss commented 2 years ago

npm i babylon --save-dev