Skip to content

Instantly share code, notes, and snippets.

@Leftyx
Created September 17, 2015 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leftyx/c005ccc382c2ef4cafb1 to your computer and use it in GitHub Desktop.
Save Leftyx/c005ccc382c2ef4cafb1 to your computer and use it in GitHub Desktop.
Ionic hooks: concat all js and css files in index.html
#!/usr/bin/env node
// Parses the index.html file search for js and css files.
// v1.0
// all the files will be concatenated in 2 files.
// Note: It does not touch cordova.js.
// appConcatFolder: folder for the js bundle.
// appConcatFile: name of the js file bundle.
// cssConcatFolder = folder of the css bundle;
// cssConcatFile = name of the css file bundle;
var fs = require('fs');
var path = require('path');
var htmlparser = require("htmlparser2");
var Concat = require('concat-with-sourcemaps');
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 = (cliCommand.indexOf('--release') > -1) || (cliCommand.indexOf('--runrelease') > -1);
// isRelease = true;
if (!isRelease) {
return;
}
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;
}
console.log('************************************************************************************');
console.log('... Parsing index.html for references (js & css) and concatenation ...');
console.log('************************************************************************************');
var appConcatFolder = 'app';
var appConcatFile = 'app.js';
var cssConcatFolder = 'css';
var cssConcatFile = 'app.css';
// Creating folder if it does not exist.
if (!fs.existsSync(path.join(platformPath, appConcatFolder))){
fs.mkdirSync(path.join(platformPath, appConcatFolder));
}
if (!fs.existsSync(path.join(platformPath, cssConcatFolder))){
fs.mkdirSync(path.join(platformPath, cssConcatFolder));
}
// Processing index.html to file js file and css files.
var elements = processIndexHtml(path.join(platformPath, "index.html"));
// Removes all the scripts and css files referenced in the index.html
// Removes other folders.
if (elements) {
if (elements.scripts.length > 0) {
elements.scripts.forEach(function(file) {
console.log('Removing referenced file: ' + path.join(platformPath, file));
fs.unlink(path.join(platformPath, file),
function(err) {
if (err) {
console.log(err);
throw err;
}
});
});
}
if (elements.css.length > 0) {
elements.css.forEach(function(file) {
console.log('Removing referenced file: ' + path.join(platformPath, file));
fs.unlink(path.join(platformPath, file),
function(err) {
if (err) {
console.log(err);
throw err;
}
});
});
}
}
// Read the index.html file and extracts all the references to scripts and css.
// compacts js in one file
// compacts css in one file.
function processIndexHtml(file) {
var html = readIndexHtml(file);
if (!html) {
return null;
}
var elements = parseIndexHtml(html);
if (!elements) {
return null;
}
if (elements.scripts.length > 0) {
var concatJs = new Concat(false, appConcatFile, '\n');
elements.scripts.forEach(function(file) {
console.log('Processing file for concatenation : ' + path.join(platformPath, file));
var fileContent = String(fs.readFileSync(path.join(platformPath, file)));
concatJs.add(file, fileContent);
});
fs.writeFileSync(path.join(platformPath, appConcatFolder + '/' + appConcatFile), concatJs.content);
} else {
console.log('No js scripts to process.');
}
if (elements.css.length > 0) {
var concatCss = new Concat(false, cssConcatFile, '\n');
elements.css.forEach(function(file) {
console.log('Processing file for concatenation : ' + path.join(platformPath, file));
var fileContent = String(fs.readFileSync(path.join(platformPath, file)));
concatCss.add(file, fileContent);
});
fs.writeFileSync(path.join(platformPath, cssConcatFolder + '/' + cssConcatFile), concatCss.content);
} else {
console.log('No css scripts to process.');
}
return elements;
}
// Parse index.html file to get all the references to js scripts and css files.
function parseIndexHtml(html) {
var scripts = [];
var css = [];
var parser = new htmlparser.Parser({
onopentag: function(name, attribs) {
if (name === "script" && attribs.src !== 'cordova.js') {
scripts.push(attribs.src);
} else if (name === "link" && attribs.rel === "stylesheet") {
css.push(attribs.href);
}
},
ontext: function(text) {},
onclosetag: function(tagname) {}
}, {
decodeEntities: true
});
parser.write(html);
parser.end();
return {
scripts: scripts,
css: css
};
}
function readIndexHtml(file) {
// var opts = {};
try {
var html = String(fs.readFileSync(file));
return html;
} catch (err) {
console.log('readIndexHtml => Error', err);
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment