Skip to content

Instantly share code, notes, and snippets.

@agustinhaller
Last active June 27, 2021 18:57
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 19 You must be signed in to fork a gist
  • Save agustinhaller/426351993c70a0329ad0 to your computer and use it in GitHub Desktop.
Save agustinhaller/426351993c70a0329ad0 to your computer and use it in GitHub Desktop.
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;
}
}
@morganabel
Copy link

@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

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