rossmartin / cordova-uglify

Cordova hook that allows you to uglify or minify your apps JavaScript and CSS.
MIT License
115 stars 45 forks source link

uglify JS files reduced to 'undefined' #46

Open rolinger opened 6 years ago

rolinger commented 6 years ago

I know this there is an open issue on this thread already, however, most of the info on that thread is about deploying from a windows machine.

I am having problems with 'cordova-uglify' on my windows machine deploying my project for Android, but now I am having the same problem on my mac deploying for iOS.

The current version of cordova-uglify is causing 3 files on my windows -> android deploying to be undefined, and 9 files on my mac->iOS to be undefined. Can't use this plugin anywhere.

rodrigograca31 commented 5 years ago

I was adding the plugins folder to be compressed and started facing the same issue and I'm not on Windows like you @rolinger

one of the plugins JavaScript file is turned into "undefined": This file from this plugin to be more precise: https://github.com/pushandplay/cordova-plugin-apprate/blob/master/www/AppRate.js

rodrigograca31 commented 5 years ago

Ok, I tracked it down:

As stated in uglify-js:

* uglify-js only supports JavaScript (ECMAScript 5).
* To minify ECMAScript 2015 or above, transpile using tools like Babel.

The file I mentioned above uses const in which is part of ES6. I found it out trying to uglify this file directly and getting the following output:

Parse error at AppRate.js:300,2
  const dateTimestamp = date.getTime();
  ^
ERROR: Unexpected token: keyword «const»

After changing const to var and uglifying again it works.

rolinger commented 5 years ago

Is there any kind of parser that error checks before attempting to uglify? When I did this time it royally hosed my entire project. I couldn't undo things and I basically had to reinstall my entire project from scratch. I have avoided it since because of this...though I want minify and uglify applied to my project, I can't do it until I know what in my project is breaking things.

rodrigograca31 commented 5 years ago

@rolinger yes there is.

I used es-check: https://github.com/dollarshaveclub/es-check

If anyone has time this package could be used to check the files before uglifying until we have ES6 support...

also, IDK how you hosed your project but I was also messing with some files earlier and kinda doing the same so here goes 2 tips:

  1. remove and re-add the platform, this should "re-copy" all the files cordova platform rm android && cordova platform add android
  2. After using this plugin go to "PROJECT/platforms/android/app/src/main/assets/www" and use your file browser to filter by .js then order them by size and the undefined ones should be the smallest with 9 bytes only....
rodrigograca31 commented 4 years ago

coming back to this. uglify-js doesn't support ES6.

I'm now using Terser 5 as an alternative. outputs a few errors but works.