Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ionic After Prepare Hooks
#!/usr/bin/env node
/**
* After prepare, files are copied to the platforms/ios and platforms/android folders.
* Lets clean up some of those files that arent needed with this hook.
*/
var fs = require('fs');
var path = require('path');
var deleteFolderRecursive = function(removePath) {
if( fs.existsSync(removePath) ) {
fs.readdirSync(removePath).forEach(function(file,index){
var curPath = path.join(removePath, file);
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(removePath);
}
};
var iosPlatformsDir = path.resolve(__dirname, '../../platforms/ios/www/lib/ionic/scss');
var androidPlatformsDir = path.resolve(__dirname, '../../platforms/android/assets/www/lib/ionic/scss');
deleteFolderRecursive(iosPlatformsDir);
deleteFolderRecursive(androidPlatformsDir);
#!/usr/bin/env node
/**
* After prepare, files are copied to the platforms/ios and platforms/android folders.
* Lets clean up some of those files that arent needed with this hook.
*/
var fs = require('fs');
var path = require('path');
var deleteFolderRecursive = function(removePath) {
if( fs.existsSync(removePath) ) {
fs.readdirSync(removePath).forEach(function(file,index){
var curPath = path.join(removePath, file);
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(removePath);
}
};
var iosPlatformsDir_1 = path.resolve(__dirname, '../../platforms/ios/www/css');
var iosPlatformsDir_2 = path.resolve(__dirname, '../../platforms/ios/www/js');
// var iosPlatformsDir_3 = path.resolve(__dirname, '../../platforms/ios/www/lib');
var iosPlatformsDir_4 = path.resolve(__dirname, '../../platforms/ios/www/templates');
var iosPlatformsDir_5 = path.resolve(__dirname, '../../platforms/ios/www/dist/dist_js/app');
var androidPlatformsDir_1 = path.resolve(__dirname, '../../platforms/android/assets/www/css');
var androidPlatformsDir_2 = path.resolve(__dirname, '../../platforms/android/assets/www/js');
// var androidPlatformsDir_3 = path.resolve(__dirname, '../../platforms/android/assets/www/lib');
var androidPlatformsDir_4 = path.resolve(__dirname, '../../platforms/android/assets/www/templates');
var androidPlatformsDir_5 = path.resolve(__dirname, '../../platforms/android/assets/www/dist/dist_js/app');
deleteFolderRecursive(iosPlatformsDir_1);
deleteFolderRecursive(iosPlatformsDir_2);
// deleteFolderRecursive(iosPlatformsDir_3);
deleteFolderRecursive(iosPlatformsDir_4);
deleteFolderRecursive(iosPlatformsDir_5);
deleteFolderRecursive(androidPlatformsDir_1);
deleteFolderRecursive(androidPlatformsDir_2);
// deleteFolderRecursive(androidPlatformsDir_3);
deleteFolderRecursive(androidPlatformsDir_4);
deleteFolderRecursive(androidPlatformsDir_5);
#!/usr/bin/env node
/**
* After prepare, files are copied to the platforms/ios and platforms/android folders.
* Lets clean up some of those files that arent needed with this hook.
*/
var path = require('path');
var mv = require('mv');
var iosPlatformsDir_dist_css = path.resolve(__dirname, '../../platforms/ios/www/dist/dist_css');
var iosPlatformsDir_dist_js = path.resolve(__dirname, '../../platforms/ios/www/dist/dist_js');
var iosPlatformsDir_dist_index = path.resolve(__dirname, '../../platforms/ios/www/dist/index.html');
var iosPlatformsDir_www_css = path.resolve(__dirname, '../../platforms/ios/www/dist_css');
var iosPlatformsDir_www_js = path.resolve(__dirname, '../../platforms/ios/www/dist_js');
var iosPlatformsDir_www_index = path.resolve(__dirname, '../../platforms/ios/www/index.html');
console.log("Moving dist files to iOS platform");
mv(iosPlatformsDir_dist_css, iosPlatformsDir_www_css, {mkdirp: true}, function(err) {
if(typeof err != 'undefined')
{
console.log("err");
console.log(err);
console.log("ERROR when moving CSS folder to iOS platform");
}
else
{
console.log("CSS folder moved OK to iOS platform");
}
});
mv(iosPlatformsDir_dist_js, iosPlatformsDir_www_js, {mkdirp: true}, function(err) {
if(typeof err != 'undefined')
{
console.log("err");
console.log(err);
console.log("ERROR when moving JS folder to iOS platform");
}
else
{
console.log("JS folder moved OK to iOS platform");
}
});
mv(iosPlatformsDir_dist_index, iosPlatformsDir_www_index, function(err) {
if(typeof err != 'undefined')
{
console.log("err");
console.log(err);
console.log("ERROR when moving index.html file to iOS platform");
}
else
{
console.log("index.html file moved OK to iOS platform");
}
});
var androidPlatformsDir_dist_css = path.resolve(__dirname, '../../platforms/android/assets/www/dist/dist_css');
var androidPlatformsDir_dist_js = path.resolve(__dirname, '../../platforms/android/assets/www/dist/dist_js');
var androidPlatformsDir_dist_index = path.resolve(__dirname, '../../platforms/android/assets/www/dist/index.html');
var androidPlatformsDir_www_css = path.resolve(__dirname, '../../platforms/android/assets/www/dist_css');
var androidPlatformsDir_www_js = path.resolve(__dirname, '../../platforms/android/assets/www/dist_js');
var androidPlatformsDir_www_index = path.resolve(__dirname, '../../platforms/android/assets/www/index.html');
console.log("Moving dist files to Android platform");
mv(androidPlatformsDir_dist_css, androidPlatformsDir_www_css, {mkdirp: true}, function(err) {
if(typeof err != 'undefined')
{
console.log("err");
console.log(err);
console.log("ERROR when moving CSS folder to Android platform");
}
else
{
console.log("CSS folder moved OK to Android platform");
}
});
mv(androidPlatformsDir_dist_js, androidPlatformsDir_www_js, {mkdirp: true}, function(err) {
if(typeof err != 'undefined')
{
console.log("err");
console.log(err);
console.log("ERROR when moving JS folder to Android platform");
}
else
{
console.log("JS folder moved OK to Android platform");
}
});
mv(androidPlatformsDir_dist_index, androidPlatformsDir_www_index, function(err) {
if(typeof err != 'undefined')
{
console.log("err");
console.log(err);
console.log("ERROR when moving index.html file to Android platform");
}
else
{
console.log("index.html file moved OK to Android platform");
}
});
#!/usr/bin/env node
/**
* After prepare, files are copied to the platforms/ios and platforms/android folders.
* Lets clean up some of those files that arent needed with this hook.
*/
var fs = require('fs');
var path = require('path');
var deleteFolderRecursive = function(removePath) {
if( fs.existsSync(removePath) ) {
fs.readdirSync(removePath).forEach(function(file,index){
var curPath = path.join(removePath, file);
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(removePath);
}
};
var iosPlatformsDir_dist = path.resolve(__dirname, '../../platforms/ios/www/dist');
var androidPlatformsDir_dist = path.resolve(__dirname, '../../platforms/android/assets/www/dist');
deleteFolderRecursive(iosPlatformsDir_dist);
deleteFolderRecursive(androidPlatformsDir_dist);
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
var UglifyJS = require('uglify-js');
var CleanCSS = require('clean-css');
var ngAnnotate = require('ng-annotate');
var cssMinifier = new CleanCSS({
noAdvanced: true, // disable advanced optimizations - selector & property merging, reduction, etc.
keepSpecialComments: 0 // remove all css comments ('*' to keep all, 1 to keep first comment only)
});
var rootDir = process.argv[2];
var platformPath = path.join(rootDir, 'platforms');
var platform = process.env.CORDOVA_PLATFORMS;
var cliCommand = process.env.CORDOVA_CMDLINE;
// hook configuration
var isRelease = true; // by default this hook is always enabled, see the line below on how to execute it only for release
//var isRelease = (cliCommand.indexOf('--release') > -1);
var recursiveFolderSearch = true; // set this to false to manually indicate the folders to process
var foldersToProcess = [ // add other www folders in here if needed (ex. js/controllers)
'dist_js',
'dist_css'
];
if (!isRelease) {
return;
}
console.log('cordova-uglify will always run by default, uncomment the line checking for the release flag otherwise');
switch (platform) {
case 'android':
platformPath = path.join(platformPath, platform, 'assets', 'www');
break;
case 'ios':
platformPath = path.join(platformPath, platform, 'www');
break;
default:
console.log('this hook only supports android and ios currently');
return;
}
foldersToProcess.forEach(function(folder) {
processFiles(path.join(platformPath, folder));
});
function processFiles(dir) {
fs.readdir(dir, function (err, list) {
if (err) {
console.log('processFiles err: ' + err);
return;
}
list.forEach(function(file) {
file = path.join(dir, file);
fs.stat(file, function(err, stat) {
if (recursiveFolderSearch && stat.isDirectory()) {
processFiles(file);
} else{
compress(file);
}
});
});
});
}
function compress(file) {
var ext = path.extname(file);
switch(ext) {
case '.js':
console.log('uglifying js file ' + file);
var res = ngAnnotate(String(fs.readFileSync(file)), { add: true });
var result = UglifyJS.minify(res.src, {
compress: { // pass false here if you only want to minify (no obfuscate)
drop_console: true // remove console.* statements (log, warn, etc.)
},
fromString: true
});
fs.writeFileSync(file, result.code, 'utf8'); // overwrite the original unminified file
break;
case '.css':
console.log('minifying css file ' + file);
var source = fs.readFileSync(file, 'utf8');
var result = cssMinifier.minify(source);
fs.writeFileSync(file, result, 'utf8'); // overwrite the original unminified file
break;
default:
console.log('encountered a ' + ext + ' file, not compressing it');
break;
}
}
@henkie14
Copy link

henkie14 commented Sep 10, 2015

For correctly minifying the css I had to change
fs.writeFileSync(file, result, 'utf8');
to
fs.writeFileSync(file, result.styles, 'utf8');

@maftieu
Copy link

maftieu commented Sep 23, 2015

I've updated it to manage other platforms: https://gist.github.com/maftieu/fc198012d40e077c30c8
It also cleans more Ionic unused files: you should just use ionic.bundle.min.js.

@nhiendat
Copy link

nhiendat commented Oct 11, 2015

I meet error: 'ERROR when moving CSS folder to Android platform' when run command: 'ionic build android'

@nhiendat
Copy link

nhiendat commented Oct 11, 2015

Error above OK if i change path of dest './www/css/' -> './www/dist/dist_css/' in function gulp.task('sass', function(done) {...}); (gulpfile.js)

@codename2299
Copy link

codename2299 commented Jan 14, 2016

What would be the value of __dirname ?

@ikanade
Copy link

ikanade commented Mar 17, 2016

In the last hook "060_uglify.js", where css is getting minified at line 85 var result = cssMinifier.minify(source);
will result in final styles.css containing "[Object][object]" instead of the minified css,
To deal with this replace line 85 with this
var result = cssMinifier.minify(source).styles;

@Danielfloaiza
Copy link

Danielfloaiza commented Apr 2, 2016

I'm watching the results after the hooks execution and the files are not obfuscated just minified, am i doing something wrong?

@fpenninger
Copy link

fpenninger commented Apr 15, 2016

I am facing an issue with your files 030_clean_dev_files_from_platforms.js and 040_move_dist_files_to_platforms.js. As soon as they are run, my ionic app breaks in the emulator after the splash screen, just a white screen is visible. Any idea why?

@ersecchio
Copy link

ersecchio commented Aug 28, 2016

@Danielfloaiza I have the same problem, you have solved?

@SnakeKiller
Copy link

SnakeKiller commented May 17, 2017

When i cordova build android these errors shows up :
Moving dist files to Android platform

err
{ Error: ENOENT: no such file or directory, rename 'C:\Users\meher\Desktop\test\test\platforms\android\assets\www\dist\index.html' -> 'C:\Users\meher\Desktop\test\test\platforms\android\assets\www\index.html'
at Error (native)
errno: -4058,
code: 'ENOENT',
syscall: 'rename',
path: 'C:\Users\meher\Desktop\test\test\platforms\android\assets\www\dist\index.html',
dest: 'C:\Users\meher\Desktop\test\test\platforms\android\assets\www\index.html' }
ERROR when moving index.html file to Android platform

err
{ Error: ENOENT: no such file or directory, rename 'C:\Users\meher\Desktop\test\test\platforms\android\assets\www\dist\dist_css' -> 'C:\Users\meher\Desktop\test\test\platforms\android\assets\www\dist_css'
at Error (native)
errno: -4058,
code: 'ENOENT',
syscall: 'rename',
path: 'C:\Users\meher\Desktop\test\test\platforms\android\assets\www\dist\dist_css',
dest: 'C:\Users\meher\Desktop\test\test\platforms\android\assets\www\dist_css' }
ERROR when moving CSS folder to Android platform
err
{ Error: ENOENT: no such file or directory, rename 'C:\Users\meher\Desktop\test\test\platforms\android\assets\www\dist\dist_js' -> 'C:\Users\meher\Desktop\test\test\platforms\android\assets\www\dist_js'
at Error (native)
errno: -4058,
code: 'ENOENT',
syscall: 'rename',
path: 'C:\Users\meher\Desktop\test\test\platforms\android\assets\www\dist\dist_js',
dest: 'C:\Users\meher\Desktop\test\test\platforms\android\assets\www\dist_js' }
ERROR when moving JS folder to Android platform

@morganabel
Copy link

morganabel commented Jul 28, 2017

@SnakeKiller You probably fixed this by now but for me I just had to change the paths in the 040_move_dist_files_to_platforms.js hook. Specifically, each path was prefixed with ../../ but for me it should have been only ../
Not sure why

@KumarVallem
Copy link

KumarVallem commented Nov 9, 2017

After uglifyJs.minify() in 060_uglify.js, my content of js is becoming as undefined.
kindly suggest.. what all are the check points that i have to revisit for minification.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment