Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save maftieu/fc198012d40e077c30c8 to your computer and use it in GitHub Desktop.
Save maftieu/fc198012d40e077c30c8 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/[platform] folder.
* Lets clean up some of those files that arent needed with this hook.
*/
var fs = require('fs');
var path = require('path');
var rootdir = process.argv[2];
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);
}
};
if (rootdir) {
// go through each of the platform directories that have been prepared
var platforms = (process.env.CORDOVA_PLATFORMS ? process.env.CORDOVA_PLATFORMS.split(',') : []);
for (var x=0; x<platforms.length; x++) {
// open up the index.html file at the www root
try {
var platform = platforms[x].trim().toLowerCase();
var wwwPath;
if (platform == 'android') {
wwwPath = path.join('platforms', platform, 'assets', 'www');
} else {
wwwPath = path.join('platforms', platform, 'www');
}
var scssPath = path.join(wwwPath, 'lib', 'ionic', 'scss');
if (fs.existsSync(scssPath)) {
process.stdout.write('removing scss folder: ' + scssPath + '\n');
deleteFolderRecursive(scssPath);
}
} catch(e) {
process.stdout.write(e);
}
}
}
#!/usr/bin/env node
/**
* After prepare, files are copied to the platforms/[platform] folder.
* Lets clean up some of those files that arent needed with this hook.
*/
var fs = require('fs');
var path = require('path');
var rootdir = process.argv[2];
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);
}
};
if (rootdir) {
// list of dev folders to delete from platform folder
var foldersToDelete = ['css', 'js'/*, 'lib'*/, 'templates', 'dist/dist_js/app'];
// go through each of the platform directories that have been prepared
var platforms = (process.env.CORDOVA_PLATFORMS ? process.env.CORDOVA_PLATFORMS.split(',') : []);
for (var x=0; x<platforms.length; x++) {
// open up the index.html file at the www root
try {
var platform = platforms[x].trim().toLowerCase();
var wwwPath;
if (platform == 'android') {
wwwPath = path.join('platforms', platform, 'assets', 'www');
} else {
wwwPath = path.join('platforms', platform, 'www');
}
for (var i=0; i<foldersToDelete.length; ++i) {
var folder = foldersToDelete[i];
var pathToDelete = path.join(wwwPath, folder);
if (fs.existsSync(pathToDelete)) {
process.stdout.write('removing "'+ folder +'" folder: ' + pathToDelete + '\n');
deleteFolderRecursive(pathToDelete);
}
}
} catch(e) {
process.stdout.write(e);
}
}
}
#!/usr/bin/env node
/**
* After prepare, files are copied to the platforms/[platform] folder.
* Lets clean up some of those files that arent needed with this hook.
*/
var path = require('path');
var mv = require('mv');
var rootdir = process.argv[2];
if (rootdir) {
// list of files and folders to move from www/dist to www/
var toMove = ['dist_css', 'dist_js', 'index.html'];
// go through each of the platform directories that have been prepared
var platforms = (process.env.CORDOVA_PLATFORMS ? process.env.CORDOVA_PLATFORMS.split(',') : []);
for (var x=0; x<platforms.length; x++) {
// open up the index.html file at the www root
try {
var platform = platforms[x].trim().toLowerCase();
var wwwPath, distPath;
if (platform == 'android') {
wwwPath = path.join('platforms', platform, 'assets', 'www');
} else {
wwwPath = path.join('platforms', platform, 'www');
}
distPath = path.join(wwwPath, 'dist');
process.stdout.write('Moving dist files to '+ platform +' platform...\n');
for (var i=0; i<toMove.length; ++i) {
var what = toMove[i];
var from = path.join(distPath, what);
var to = path.join(wwwPath, what);
mv(from, to, {mkdirp: true}, (function(what, from, to) {
return function(err) {
if (typeof err !== 'undefined') {
console.log('!! ERROR when moving "'+ what +'" to '+ platform +' platform');
console.log('\tMoving from: "'+ from +'" to "'+ to + '"');
console.log(err);
} else {
console.log('\tMoved "'+ what +'".');
}
};
})(what, from, to));
}
} catch(e) {
process.stdout.write(e);
}
}
}
#!/usr/bin/env node
/**
* After prepare, files are copied to the platforms/[platform] folder.
* Lets clean up some of those files that arent needed with this hook.
*/
var fs = require('fs');
var path = require('path');
var rootdir = process.argv[2];
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);
}
};
if (rootdir) {
// go through each of the platform directories that have been prepared
var platforms = (process.env.CORDOVA_PLATFORMS ? process.env.CORDOVA_PLATFORMS.split(',') : []);
for (var x=0; x<platforms.length; x++) {
// open up the index.html file at the www root
try {
var platform = platforms[x].trim().toLowerCase();
var wwwPath;
if (platform == 'android') {
wwwPath = path.join('platforms', platform, 'assets', 'www');
} else {
wwwPath = path.join('platforms', platform, 'www');
}
var distPath = path.join(wwwPath, 'dist');
if (fs.existsSync(distPath)) {
process.stdout.write('removing dist folder: ' + distPath + '\n');
deleteFolderRecursive(distPath);
}
} catch(e) {
process.stdout.write(e);
}
}
}
#!/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':
case 'wp8':
platformPath = path.join(platformPath, platform, 'www');
break;
default:
console.log('this hook currently does not supports '+platform+' platform');
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;
}
}
@nhiendat
Copy link

I meet error: 'ERROR when moving "dist_css" to android platform' when run command: 'ionic build android'

@nhiendat
Copy link

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

@philip-sterne
Copy link

https://gist.github.com/maftieu/fc198012d40e077c30c8#file-060_uglify-js-L87

Should be:

            fs.writeFileSync(file, result.styles, 'utf8'); // overwrite the original unminified file

Note the addition of .styles. Without that you just end up with [Object obj] in your css file. Which is certainly minified, but I don't think it is valid css!

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