Search code examples
androidcordovacordova-pluginsminifybundling-and-minification

Should we minify Cordova source files?


I am using Cordova to build an android app (.apk).

I would like to keep the app size as small as possible. Should I minify the .html, .css, and .js files as well as the files of the various plugins I am using, with some third-party web sites such as https://www.minifier.org/ ?

Does Cordova minify those files automatically for us?

What is the best way to go about app minification in this regard?


Solution

  • I just discovered this handy plugin specifically for this purpose.

    It is somewhat outdated. To use it, I made the following modifications in [PROJECT]\hooks\after_prepare\minify.js:

    1) LINE 5:

    var UglifyJS = require('uglify-js');
    

    to

    var UglifyJS = require('terser');
    

    then installed that plugin with npm.

    2) LINE 7:

    var ImageMin = require('image-min');
    

    to

    var ImageMin = require('imagemin');
    

    3) LINE 101:

        platformPath = path.join(platformPath, platform, "assets", "www");
    

    to

        platformPath = path.join(platformPath, platform, 'app', 'src', 'main', 'assets', 'www'); 
    

    Minifying the files in www/, www/css/, www/js, www/img/ and www/library/, It shrank my .apk file from 6.50MB to 5.94MB. Note that the last path was custom-added by me in LINE111 of minify.js.