Cannot minify project

I tried https://github.com/rossmartin/cordova-uglify and https://github.com/Jiansen/ionic-minify
both the release apk is not minified.
I think the problem could be I do not have a .\platforms\android\assets folder.

I noticed when I run ionic cordova build --release I get those errors:

An error ocurred while reading directories:
 Error: ENOENT: no such file or directory, scandir 'C:\nodejs\myproject\platforms\android\assets\www\js'
An error ocurred while reading directories:
 Error: ENOENT: no such file or directory, scandir 'C:\nodejs\myproject\platforms\android\assets\www\css'
An error ocurred while reading directories:
 Error: ENOENT: no such file or directory, scandir 'C:\nodejs\myproject\platforms\android\assets\www\img'
Running command: "C:\Program Files\nodejs\node.exe" C:\nodejs\myproject\hooks\after_prepare\uglify.js C:\nodejs\myproject

processFiles err: Error: ENOENT: no such file or directory, scandir 'C:\nodejs\myproject\platforms\android\assets\www\js'
processFiles err: Error: ENOENT: no such file or directory, scandir 'C:\nodejs\myproject\platforms\android\assets\www\css'
processFiles err: Error: ENOENT: no such file or directory, scandir 'C:\nodejs\myproject\platforms\android\assets\www\img'
processFiles err: Error: ENOENT: no such file or directory, scandir 'C:\nodejs\myproject\platforms\android\assets\www\build'

ionic info prints:

[WARN] Error with .\www\lib\ionic\version.json file: FILE_NOT_FOUND, trying
       .\bower.json.

cli packages: (C:\Program Files\nodejs\node_modules)

    @ionic/cli-utils  : 1.19.1
    ionic (Ionic CLI) : 3.19.1

global packages:

    cordova (Cordova CLI) : 8.0.0
    Gulp CLI              : not installed globally

local packages:

    Cordova Platforms : android 7.0.0 ios 4.5.4
    Ionic Framework   : ionic1 1.3.4

System:

    Android SDK Tools : 26.0.1
    Node              : v8.9.4
    npm               : 5.6.0
    OS                : Windows 7

what is your project structure and whatā€™s on the uglify.json config

This is a screen of the structure

This is the content of ./hooks/uglify-config.json

{
    "alwaysRun": true,
    "recursiveFolderSearch": true,
    "foldersToProcess": [
        "js",
        "css",
        "img",
        "build"
    ],
    "uglifyJsOptions": {
        "compress": {
            "drop_console": true
        },
        "mangle": false,
        "output": {
            "code": true
        }
    },
    "cleanCssOptions": {
        "specialComments": "all"
    }
}

Again, Iā€™m pretty sure the problems comes from the fact I do not have a .\platforms\android\assets
I donā€™t have a .\platforms\platforms.json either. I think there is something ā€œbrokeā€ in my project.

The different structure in platforms is probably because the newer cordova-android versions have a different folder structure. Can you find the assets folder somewhere else, in platforms/android/app for example? Then you might just have to fork and modify the libraries you are trying to use.

You can try by changing the uglify.js and add_platform_class.js files in your after_prepare folder

 switch (platform) {      
 case 'android':
    wwwPath = path.join(platformPath, platform, 'assets', 'www'); ////REMOVE ASSETS HERE
    break;

  case 'ios':
  case 'browser':
  case 'wp8':
  case 'windows':
    wwwPath = path.join(platformPath, platform, 'www');
    break;

  default:
    console.log('this hook only supports android, ios, wp8, windows, and browser currently');
    return;

@Sujan12 I have an assets here platforms\android\app\src\main\assets

@thesourav I tried to remove assets in uglify.js and in 010_add_platform_class.js the error

Maybe I could solve this mess creating a brand new Ionic1 project and copy my html, js, img, css files?

No, the tools you are trying to use just donā€™t work together - the ā€œminifyā€ stuff assumes the files are in different locations than they actually are, as cordova-android was updated only a few months ago.

As @thesourav says, you should fix the paths. Is there a www in the assets folder you do have? Then update the script to point to that and see if this fixes things.

Just one thing to add what @Sujan12 said,
your uglify.js and platforms class.js would look like

switch (platform) {      
   case 'android':
   wwwPath = path.join(platformPath, platform, 'app','src','main','assets', 'www'); 
   console.log(wwwPath); //You can add console.log to make sure your path is correct
   break;

  case 'ios':
  case 'browser':
  case 'wp8':
  case 'windows':
    wwwPath = path.join(platformPath, platform, 'www');
    break;

   default:
console.log('this hook only supports android, ios, wp8, windows, and browser currently');
return;

I tried to change those script to my assets path, it works!
The strange part is I still get an error during release building

Running command: "C:\Program Files\nodejs\node.exe" C:\nodejs\myproject\hooks\after_prepare\ionic-minify.js C:\nodejs\myproject

WARN: The use of the --release flag is deprecated!! Use --minify instead!
Starting minifying your files...
An error ocurred while reading directories:
 Error: ENOENT: no such file or directory, scandir 'C:\nodejs\myproject\platforms\android\assets\www\js'
An error ocurred while reading directories:
 Error: ENOENT: no such file or directory, scandir 'C:\nodejs\myproject\platforms\android\assets\www\css'
An error ocurred while reading directories:
 Error: ENOENT: no such file or directory, scandir 'C:\nodejs\myproject\platforms\android\assets\www\img'
Running command: "C:\Program Files\nodejs\node.exe" C:\nodejs\myproject\hooks\after_prepare\uglify.js C:\nodejs\myproject

should I remove ionic-minify module? Currently I have 2 minify hooks: ionic-minify.js and uglify.js
Somewhere a script still search for the old assets path

If you will be using --release use uglify.js( which i use ) or if you want to use --minify then use ionic-minify

thanks, there are differences between the boudled module uglify.js and ionic-minify?

I think there are none but i couldā€™t make it to work properly so i used uglify js :slight_smile:

2 Likes

Did you ever manage to minify/uglify your v1 project? I have tried several times and run into the same issues you did but never found a solution.

can you elaborate more why its not working at your side ? if you install the latest cordova-uglify, it already handle 2 possibilities of platform asset location :

  1. platform, ā€˜assetsā€™, ā€˜wwwā€™
  2. platform, ā€˜appā€™, ā€˜srcā€™, ā€˜mainā€™, ā€˜assetsā€™, ā€˜wwwā€™

my question is which command you use to build your apk ? is it include --release parameter ? if not, then you should set alwaysRun parameter to true in uglify-config.json.
I have just install and test cordova-uglify successfully.