rossmartin / cordova-uglify

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

js files have content undefined #35

Open rchovan opened 6 years ago

rchovan commented 6 years ago

Hi, when I try uglify, it overwrites all .js files, but they contet is only one single word "undefined".

image

stripped debug log: cordova -d prepare

No scripts found for hook "before_prepare".
Checking config.xml and package.json for saved platforms that haven't been added to the project
Config.xml and package.json platforms are the same. No pkg.json modification.
Package.json and config.xml platforms are different. Updating config.xml with most current list of platforms.
Failed to require PlatformApi instance for platform "browser". Using polyfill instead.
Checking config.xml for saved plugins that haven't been added to the project
Checking for any plugins added to the project that have not been installed in browser platform
No differences found between plugins added to project and installed in browser platform. Continuing...
Generating config.xml from defaults for platform "browser"
Executing script found in hooks directory for hook "after_prepare": hooks\after_prepare\uglify.js
Running command: "C:\Program Files\nodejs\node.exe" D:\NetBeansProjects\ReturnReporting\hooks\after_prepare\uglify.js D:\NetBeansProjects\ReturnReporting
processFiles err: Error: ENOENT: no such file or directory, scandir 'D:\NetBeansProjects\ReturnReporting\platforms\browser\www\module'
uglifying js file D:\NetBeansProjects\ReturnReporting\platforms\browser\www\controller\BaseController.js
uglifying js file D:\NetBeansProjects\ReturnReporting\platforms\browser\www\controller\Home.controller.js
uglifying js file D:\NetBeansProjects\ReturnReporting\platforms\browser\www\controller\Login.controller.js
uglifying js file D:\NetBeansProjects\ReturnReporting\platforms\browser\www\controller\Settings.controller.js
minifying css file D:\NetBeansProjects\ReturnReporting\platforms\browser\www\css\style.css
uglifying js file D:\NetBeansProjects\ReturnReporting\platforms\browser\www\Component.js
encountered a .xml file, not compressing it
uglifying js file D:\NetBeansProjects\ReturnReporting\platforms\browser\www\confighelper.js
uglifying js file D:\NetBeansProjects\ReturnReporting\platforms\browser\www\controller\App.controller.js
uglifying js file D:\NetBeansProjects\ReturnReporting\platforms\browser\www\cordova.js
uglifying js file D:\NetBeansProjects\ReturnReporting\platforms\browser\www\exec.js
encountered a .html file, not compressing it
encountered a .json file, not compressing it

full debug log: uglify_debug.txt

uglify installed from npm

{ name: 'cordova-uglify',
  description: 'Cordova hook that allows you to uglify or minify your apps JavaScript and CSS.',
  'dist-tags': { latest: '0.2.8' },
  versions: 
   [ '0.0.1',
     '0.0.2',
     '0.0.3',
     '0.0.4',
     '0.0.5',
     '0.0.6',
     '0.0.7',
     '0.0.8',
     '0.0.9',
     '0.1.0',
     '0.1.1',
     '0.1.2',
     '0.1.3',
     '0.1.4',
     '0.1.5',
     '0.1.6',
     '0.1.7',
     '0.1.8',
     '0.1.9',
     '0.2.0',
     '0.2.1',
     '0.2.2',
     '0.2.3',
     '0.2.4',
     '0.2.5',
     '0.2.6',
     '0.2.7',
     '0.2.8' ],
  maintainers: [ 'rossmartin <rmartin311@gmail.com>' ],
  time: 
   { modified: '2017-01-19T15:51:35.841Z',
     created: '2014-06-18T03:56:05.284Z',
     '0.0.1': '2014-06-18T03:56:05.284Z',
     '0.0.2': '2014-06-18T05:35:27.107Z',
     '0.0.3': '2014-06-18T05:42:27.368Z',
     '0.0.4': '2014-06-18T05:49:43.994Z',
     '0.0.5': '2014-06-18T05:52:58.761Z',
     '0.0.6': '2014-06-19T05:21:03.197Z',
     '0.0.7': '2014-06-22T22:23:42.514Z',
     '0.0.8': '2014-06-23T00:22:46.231Z',
     '0.0.9': '2014-11-12T05:09:29.435Z',
     '0.1.0': '2014-11-12T05:14:26.594Z',
     '0.1.1': '2014-11-12T05:24:53.626Z',
     '0.1.2': '2014-12-19T15:56:15.972Z',
     '0.1.3': '2014-12-19T16:12:12.345Z',
     '0.1.4': '2015-01-12T22:09:19.752Z',
     '0.1.5': '2015-04-09T20:55:45.840Z',
     '0.1.6': '2015-06-22T14:47:17.993Z',
     '0.1.7': '2015-07-11T02:08:10.764Z',
     '0.1.8': '2015-07-11T02:31:39.325Z',
     '0.1.9': '2015-08-03T21:01:21.157Z',
     '0.2.0': '2015-09-30T13:38:50.084Z',
     '0.2.1': '2015-10-03T16:06:54.283Z',
     '0.2.2': '2015-12-13T22:54:54.170Z',
     '0.2.3': '2015-12-16T01:08:51.901Z',
     '0.2.4': '2016-07-15T15:12:03.110Z',
     '0.2.5': '2016-09-11T17:32:27.083Z',
     '0.2.6': '2016-10-24T14:35:28.131Z',
     '0.2.7': '2016-11-27T04:43:01.635Z',
     '0.2.8': '2017-01-19T15:51:35.841Z' },
  homepage: 'https://github.com/rossmartin/cordova-uglify',
  keywords: [ 'cordova', 'uglify', 'minify', 'hook', 'hooks' ],
  author: 'Ross Martin',
  bugs: { url: 'https://github.com/rossmartin/cordova-uglify/issues' },
  license: 'MIT',
  readmeFilename: 'README.md',
  repository: 
   { type: 'git',
     url: 'git+https://github.com/rossmartin/cordova-uglify.git' },
  users: 
   { okmogwai: true,
     batosai: true,
     cnring18: true,
     gdevise: true,
     ebbm: true },
  version: '0.2.8',
  dependencies: 
   { 'clean-css': '3.4.8',
     'ng-annotate': '0.15.4',
     shelljs: '^0.7.0',
     'uglify-js': '2.4.14' },
  scripts: 
   { postinstall: 'node scripts/install.js',
     postuninstall: 'node scripts/uninstall.js' },
  gitHead: '395775651d1b3a5f3625e4fa3d3ec2ca823664b1',
  dist: 
   { shasum: '5aa9f69797da4c9d51c2f5184f4dcf0510d1b327',
     tarball: 'https://registry.npmjs.org/cordova-uglify/-/cordova-uglify-0.2.8.tgz' },
  directories: {} }
rossmartin commented 6 years ago

Sorry for the late response. Can you please provide the contents of your uglify-config.json? I think this issue is isolated to Windows.

rchovan commented 6 years ago

Hello, this is my used config:

{
    "alwaysRun": true,
    "recursiveFolderSearch": true,
    "foldersToProcess": [
        "js",
        "css",
        "img",
        "build"
    ],
    "uglifyJsOptions": {
        "compress": {
            "drop_console": true
        },
        "fromString": true,
        "mangle": false
    },
    "cleanCssOptions": {
        "noAdvanced": true,
        "keepSpecialComments": 0
    }
} 
rossmartin commented 6 years ago

It is odd that it is modifying files outside of your foldersToProcess - that is how it appears from your debug file. This behavior doesn't happen on OSX or Linux - I'll have to see if I can reproduce your issue on Windows.

GeeKanJi commented 6 years ago

I'm not sure this is the same problem, but it may help:

I had the same problem, ie .js files with 'undefined' content.

After debugging uglify.js, the following line result = UglifyJS.minify (res.src, hookConfig.uglifyJsOptions); return a "result.code" undefined

an overview before correction:

result = UglifyJS.minify(res.src, hookConfig.uglifyJsOptions);
console.log('result.code ' + result.code);
fs.writeFileSync(file, result.code, 'utf8'); // overwrite the original unminified file
break;`

Correction of the line (removed parameter "hookConfig.uglifyJsOptions" ,fast correction, there is surely better)

result = UglifyJS.minify(res.src);
        console.log('result.code ' + result.code);
        fs.writeFileSync(file, result.code, 'utf8'); // overwrite the original unminified file
        break;

Why? Updates with npm have pushed new version (uglify and other), the uglifyJsOptions parameters did not match the version of UglifyJs

ps: no more time to spend on this project, it works!

rossmartin commented 6 years ago

@jeromeXoo Thank you for providing this info. I had some time this evening to look into this issue and it appears that the last PR introduced a few problems. I thought I had tested it after I merged but apparently I didn't have the latest commit.

I've fixed the issue and published a new version (0.2.9) to npm. Let me know if you are still having issues.

rchovan commented 6 years ago

Hi, it crashes on first file:

cordova -d prepare
No scripts found for hook "before_prepare".
Checking config.xml and package.json for saved platforms that haven't been added to the project
Config.xml and package.json platforms are the same. No pkg.json modification.
Package.json and config.xml platforms are different. Updating config.xml with most current list of platforms.
Failed to require PlatformApi instance for platform "browser". Using polyfill instead.
Checking config.xml for saved plugins that haven't been added to the project
Checking for any plugins added to the project that have not been installed in browser platform
No differences found between plugins added to project and installed in browser platform. Continuing...
Generating config.xml from defaults for platform "browser"
Executing script found in hooks directory for hook "after_prepare": hooks\after_prepare\uglify.js
Running command: "C:\Program Files\nodejs\node.exe" D:\NetBeansProjects\ReturnReporting\hooks\after_prepare\uglify.js D:\NetBeansProjects\ReturnReporting
minifying css file D:\NetBeansProjects\ReturnReporting\platforms\browser\www\css\style.css
uglifying js file D:\NetBeansProjects\ReturnReporting\platforms\browser\www\controller\App.controller.js
fs.js:584
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open 'D:\NetBeansProjects\ReturnReporting\sap.ui.define([
    'sap\ui\returnreporting\controller\BaseController',
...
...
 at Error (native)
    at Object.fs.openSync (fs.js:584:18)
    at Object.fs.readFileSync (fs.js:431:33)
    at addFile (D:\NetBeansProjects\ReturnReporting\node_modules\uglify-js\tools\node.js:70:22)
    at D:\NetBeansProjects\ReturnReporting\node_modules\uglify-js\tools\node.js:81:17
    at Array.forEach (native)
    at Object.exports.minify (D:\NetBeansProjects\ReturnReporting\node_modules\uglify-js\tools\node.js:79:26)
    at compress (D:\NetBeansProjects\ReturnReporting\hooks\after_prepare\uglify.js:127:25)
    at D:\NetBeansProjects\ReturnReporting\hooks\after_prepare\uglify.js:94:11
    at FSReqWrap.oncomplete (fs.js:82:15)
 Command finished with error code 1: C:\Program Files\nodejs\node.exe D:\NetBeansProjects\ReturnReporting\hooks\after_prepare\uglify.js,D:\NetBeansProjects\ReturnReporting
Error: Hook failed with error code 1: D:\NetBeansProjects\ReturnReporting\hooks\after_prepare\uglify.js

my config:

{
    "alwaysRun": true,
    "recursiveFolderSearch": true,
    "foldersToProcess": [
        "css",
        "controller",
        "model",
        "view"
    ],
    "uglifyJsOptions": {
        "compress": {
            "drop_console": true
        },
        "mangle": false,
        "output": {
            "code": true
        }
    },
    "cleanCssOptions": {
        "noAdvanced": true,
        "keepSpecialComments": 0
    }
}

cordova uglify installed today by npm


D:\NetBeansProjects\ReturnReporting>npm show cordova-uglify

{ name: 'cordova-uglify',
  description: 'Cordova hook that allows you to uglify or minify your apps JavaScript and CSS.',
  'dist-tags': { latest: '0.2.9' },
  versions:
   [ '0.0.1',
     '0.0.2',
     '0.0.3',
     '0.0.4',
     '0.0.5',
     '0.0.6',
     '0.0.7',
     '0.0.8',
     '0.0.9',
     '0.1.0',
     '0.1.1',
     '0.1.2',
     '0.1.3',
     '0.1.4',
     '0.1.5',
     '0.1.6',
     '0.1.7',
     '0.1.8',
     '0.1.9',
     '0.2.0',
     '0.2.1',
     '0.2.2',
     '0.2.3',
     '0.2.4',
     '0.2.5',
     '0.2.6',
     '0.2.7',
     '0.2.8',
     '0.2.9' ],
  maintainers: [ 'rossmartin <2498502+rossmartin@users.noreply.github.com>' ],
  time:
   { modified: '2017-09-22T02:55:35.360Z',
     created: '2014-06-18T03:56:05.284Z',
     '0.0.1': '2014-06-18T03:56:05.284Z',
     '0.0.2': '2014-06-18T05:35:27.107Z',
     '0.0.3': '2014-06-18T05:42:27.368Z',
     '0.0.4': '2014-06-18T05:49:43.994Z',
     '0.0.5': '2014-06-18T05:52:58.761Z',
     '0.0.6': '2014-06-19T05:21:03.197Z',
     '0.0.7': '2014-06-22T22:23:42.514Z',
     '0.0.8': '2014-06-23T00:22:46.231Z',
     '0.0.9': '2014-11-12T05:09:29.435Z',
     '0.1.0': '2014-11-12T05:14:26.594Z',
     '0.1.1': '2014-11-12T05:24:53.626Z',
     '0.1.2': '2014-12-19T15:56:15.972Z',
     '0.1.3': '2014-12-19T16:12:12.345Z',
     '0.1.4': '2015-01-12T22:09:19.752Z',
     '0.1.5': '2015-04-09T20:55:45.840Z',
     '0.1.6': '2015-06-22T14:47:17.993Z',
     '0.1.7': '2015-07-11T02:08:10.764Z',
     '0.1.8': '2015-07-11T02:31:39.325Z',
     '0.1.9': '2015-08-03T21:01:21.157Z',                                                                                                                             10:42
     '0.2.0': '2015-09-30T13:38:50.084Z',
     '0.2.1': '2015-10-03T16:06:54.283Z',
     '0.2.2': '2015-12-13T22:54:54.170Z',
     '0.2.3': '2015-12-16T01:08:51.901Z',
     '0.2.4': '2016-07-15T15:12:03.110Z',
     '0.2.5': '2016-09-11T17:32:27.083Z',
     '0.2.6': '2016-10-24T14:35:28.131Z',
     '0.2.7': '2016-11-27T04:43:01.635Z',
     '0.2.8': '2017-01-19T15:51:35.841Z',
     '0.2.9': '2017-09-22T02:55:35.360Z' },
  homepage: 'https://github.com/rossmartin/cordova-uglify',
  keywords: [ 'cordova', 'uglify', 'minify', 'hook', 'hooks' ],
  author: 'Ross Martin',
  bugs: { url: 'https://github.com/rossmartin/cordova-uglify/issues' },
  license: 'MIT',
  readmeFilename: 'README.md',
  repository:
   { type: 'git',
     url: 'git+https://github.com/rossmartin/cordova-uglify.git' },
  users: { okmogwai: true, cnring18: true, gdevise: true, ebbm: true },
  version: '0.2.9',
  dependencies:
   { 'clean-css': '3.4.8',
     'ng-annotate': '0.15.4',
     shelljs: '^0.7.0',
     'uglify-js': '3.1.1' },
  scripts:
   { postinstall: 'node scripts/install.js',
     postuninstall: 'node scripts/uninstall.js' },
  gitHead: 'e101c6e9f673e43f2431d2a238c4fdbf301870b2',
  dist:
   { shasum: 'af25bcd17b1687d91d60856ad317590901eb3565',
     tarball: 'https://registry.npmjs.org/cordova-uglify/-/cordova-uglify-0.2.9.tgz' },
  directories: {} }```
rossmartin commented 6 years ago

@rchovan Thanks for reporting the issue. I have a theatre PC with Windows on it I'll try it this weekend. I'm not experiencing any issues on Ubuntu or Mac OS. I wonder if the issue is because the project is on a separate drive than node/windows.

koga73 commented 6 years ago

@rossmartin I'm experiencing the same issue where the JS output files only contain "undefined". I am running the latest version of cordova-uglify 0.3.0 as reported by npm and node v8.9.1 on Windows 10.

rossmartin commented 6 years ago

@koga73 Thanks for letting me know. Unfortunately my theatre PC that had Windows 10 on it died! Bad motherboard or power supply I think but I don't plan on reviving it. Recently I installed 2 Windows VMs to an external HDD on my Macbook for my day job to test IE11 issues. I'll have to use a VM to test this out and get it fixed. I haven't forgotten about this just haven't had time - it's frustrating because it works fine on Linux and Mac OS.

koga73 commented 6 years ago

@rossmartin Did some digging. Turns out your plugin is fine. Issue is we had an old uglify-config.json. Specifically this line was causing it: "uglifyJsOptions": { "fromString": true }

rossmartin commented 6 years ago

@koga73 Thanks a bunch for letting me know. I wonder if this is the issue you are having @rchovan ? Can you try it with the options from the readme?

  "uglifyJsOptions": {
    "compress": {
      "drop_console": true
    },
    "mangle": false,
    "output": {
      "code": true
    }
  }

I could update the install script to overwrite the uglify-config.json but I think a better solution is to just document it in the readme and output after installing the package. I don't want anyone to lose their config on update.

iliakonnov commented 6 years ago

I have this error too. Using default options from README doesn't help.

luigi37 commented 6 years ago

Hi all, started experiencing the same issue. What is generating the empty file in my case is the following code:

BackgroundGeolocation.on('location', (location) => { // handle your locations here // to perform long running operation on iOS // you need to create background task BackgroundGeolocation.startTask(taskKey => { // execute long running task // eg. ajax post location // IMPORTANT: task has to be ended by endTask BackgroundGeolocation.endTask(taskKey); }); });

If I remove it then it works properly

I'm not sure though what should I change... Suggestions?

luigi37 commented 6 years ago

Running uglijs manually, I get

BackgroundGeolocation.on('location', (position) => { ERROR: Unexpected token: operator (>)

How could I fix it considering is legitimate js code?

Thanks

luigi37 commented 6 years ago

I'm trying https://github.com/mishoo/UglifyJS2/tree/harmony

And it seems to solve the issue.

In uglify hook I changed this line: var UglifyJS = require(path.join(dependencyPath, 'uglify-js')); to var UglifyJS = require(path.join(dependencyPath, 'uglify-es'));

So far seems ok, will test more extensively

rossmartin commented 6 years ago

@luigi37 I think you may have revealed why others are experiencing this same issue. uglify-js does not work with ES6. I think most transpile their "modern" JS to ES5 before feeding it to uglify-js. It appears that uglify-es might be the way to go moving forward to handle minifying code if it is ES6.

There is also this interesting compress option with uglify-es -

Thanks for bringing this up @luigi37. I'm going to give it a go and see how it works out. If it works with the old and new stuff I'll publish a new version today.

luigi37 commented 6 years ago

Cool, thanks!

rossmartin commented 6 years ago

@luigi37 I published a new version (0.3.1) that uses uglify-es. I tried the latest code with some old projects that were plain ES5 and a new one with ES6 - I had no issues.

I'll be leaving this issue open for now.

rossmartin commented 6 years ago

@luigi37 and anyone else, please see #39 about the decision to abandon uglify-es in https://github.com/rossmartin/cordova-uglify/commit/eeb41a851fbf23b44439485885394d9e243fce9c (v0.3.2)

Make sure your cordova project's code is transpiled to ES5 (I think you should even if you don't use this hook).

luigi37 commented 6 years ago

@rossmartin thank you. I'm not familiar with ES6 enough to confirm if transpiling back to ES5 can affect the functionality. Could you please confirm? If no issue, I'm happy with your solution. Thanks a lot! Luigi

rossmartin commented 6 years ago

@luigi37 if you use babel to transpile your ES6 to ES5 it won't affect functionality. Not all mobile device webviews support ES6 so it would be much safer to transpile.

luigi37 commented 6 years ago

Thank you. Is there a way to include the transpile into cordova-uglify to avoid multiple steps? Thanks Luigi

rossmartin commented 6 years ago

@luigi37 That would be a nice feature to have if you're using something like framework 7 & react without using react-scripts. Those that use Ionic it won't be of any benefit.

I'll think about it - this may be a better candidate for a separate hook.

luigi37 commented 6 years ago

Thanks Ross, it would be much appreciated. Specifically I'm using Cordova with Framework7... Anyway it's a specific cordova plugin which I use which brought me to the ES6 thing... Luigi

rolinger commented 5 years ago

This same issue is happening to me too, however its only happening to three files, all three files are 9 bytes and all say undefined.

I am running an Ionic v1 app, so no Ionic v2 or ES issues to worry about. Just straight old AngularJS and JS.

My system information:

Cordova CLI: 8.0.0
Gulp version:  CLI version 3.9.1
Gulp local:   Local version 3.9.1
Ionic Version: 1.3.4
Ionic CLI Version: 1.7.14
Ionic App Lib Version: 0.7.0
OS: Windows 7 SP1
Node Version: v6.10.3

Cordova-android: 7.1.1
rodrigograca31 commented 5 years ago

I'm having the same issue, 1 file only has "undefined" and I'm not on Windows, please read more in #46