javascript-obfuscator / webpack-obfuscator

javascript-obfuscator plugin for Webpack
https://github.com/javascript-obfuscator/javascript-obfuscator
BSD 2-Clause "Simplified" License
856 stars 84 forks source link

webpack.config.js - Cannot read property 'emit' of undefined #80

Open bengrah-miller opened 4 years ago

bengrah-miller commented 4 years ago

Hi - I'm not sure if this is the right place to post this but I could use some help.

I'm developing a project using Cordova/Ionic 3, and I'm trying to add the javascript-obfuscator plugin following this guide.

I've installed the javascript-obfuscator package via NPM, I've got a config folder with a copy of webpack.config.js that's being recognised by my project and used. I've added my plugin:

var JavaScriptObfuscator = require('webpack-obfuscator');

And in the plugins section:

  plugins: [
    new Dotenv({
      path: '.env.dev', // load this now instead of the ones in '.env'
      systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs.
      silent: true // hide any errors
    }),
    ionicWebpackFactory.getIonicEnvironmentPlugin(),
    ionicWebpackFactory.getCommonChunksPlugin(),
    new JavaScriptObfuscator({
      stringArrayEncoding: 'base64'
    })
  ],

But when I run the ionic cordova run android command, I get the following output:

PS C:\MobileDev\sd-site-induction> ionic cordova build android
> ionic-app-scripts.cmd build --target cordova --platform android
[15:53:49]  ionic-app-scripts 3.2.4 
[15:53:49]  build dev started ...
[15:53:50]  clean started ...
[15:53:50]  clean finished in 7 ms
[15:53:50]  copy started ...
[15:53:50]  deeplinks started ...
[15:53:50]  deeplinks finished in 451 ms
[15:53:50]  transpile started ...
[15:54:04]  transpile finished in 13.94 s
[15:54:04]  preprocess started ...
[15:54:04]  preprocess finished in 1 ms
[15:54:04]  webpack started ...
[15:54:04]  ionic-app-script task: "build" 
[15:54:04]  TypeError: Cannot read property 'emit' of undefined 
TypeError: Cannot read property 'emit' of undefined
    at WebpackObfuscator.apply (C:\MobileDev\sd-site-induction\node_modules\webpack-obfuscator\dist\index.js:22:24)
    at Compiler.apply (C:\MobileDev\sd-site-induction\node_modules\@ionic\app-scripts\node_modules\tapable\lib\Tapable.js:375:16)
    at webpack (C:\MobileDev\sd-site-induction\node_modules\@ionic\app-scripts\node_modules\webpack\lib\webpack.js:33:19)
    at C:\MobileDev\sd-site-induction\node_modules\@ionic\app-scripts\dist\webpack.js:132:24
    at new Promise (<anonymous>)
    at runWebpackFullBuild (C:\MobileDev\sd-site-induction\node_modules\@ionic\app-scripts\dist\webpack.js:113:12)
    at webpackWorker (C:\MobileDev\sd-site-induction\node_modules\@ionic\app-scripts\dist\webpack.js:70:19)
    at Object.webpack (C:\MobileDev\sd-site-induction\node_modules\@ionic\app-scripts\dist\webpack.js:29:12)
    at bundleWorker (C:\MobileDev\sd-site-induction\node_modules\@ionic\app-scripts\dist\bundle.js:13:22)
    at Object.bundle (C:\MobileDev\sd-site-induction\node_modules\@ionic\app-scripts\dist\bundle.js:6:12)
[ERROR] An error occurred while running subprocess ionic-app-scripts.

Any ideas why this is happening, or what the error means?

Thanks, bengrah

FlorinSerban commented 4 years ago

Hi, I have the exact same problem and I'm following the same guide. Have you found a fix?

bengrah-miller commented 4 years ago

Hi there,

No I haven't managed to find a fix - I think it's likely something to do with the version of Ionic I'm on. I checked out a starter project on the latest Ionic and I could get it to work fine there. Sorry not much help.

bengrah

virtualvishwam commented 3 years ago

I am also using Ionic 3 for my application and facing this issue. Initial debugging results -

So I am assuming this is webpack compatibility issue.

Can someone tell what version of webpack-obfuscator is compatible with webpack@3.12.0?

virtualvishwam commented 3 years ago

I am also using Ionic 3 for my application and facing this issue. Initial debugging results -

  • My webpack version is 3.12.0
  • I was facing similar issue ( Cannot read property 'compilation' of undefined ) with webpack-obfuscator@3.2.0, then realised its for webpack@5 only
  • Downgraded to webpack-obfuscator@2.6.0, no getting this error. ( Cannot read property 'emit' of undefined )

So I am assuming this is webpack compatibility issue.

Can someone tell what version of webpack-obfuscator is compatible with webpack@3.12.0?

Okay did some more testing. Conclusion - My above theory was correct. It is indeed a versioning issue which is not documented properly in this repository's commit logs.

Result - For webpack@3.12.0 which is being used in Ionic 3, webpack-obfuscator@0.16.0 works like a charm.

Further work - I was short of time so did not test even higher versions of webpack-obfuscator, someone needs to test that.

@sanex3339 @mauricedoepke to please update the documents to reflect the compatibility of webpack-obfuscator versions with different versions of webpack. This is a great open-source initiative, and I would love that this has proper documentation as well. 😊😊