Skip to content

Instantly share code, notes, and snippets.

@Leftyx
Created September 17, 2015 14:37
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/99883d737a865341d153 to your computer and use it in GitHub Desktop.
Save Leftyx/99883d737a865341d153 to your computer and use it in GitHub Desktop.
Ionic hooks: useref index.html file.
#!/usr/bin/env node
// useref index.html
// v1.0
// searches the node-useref tags in your index.html to replace it width
// the bundle.
// https://www.npmjs.com/package/node-useref
var fs = require('fs');
var path = require('path');
var useref = require('node-useref');
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('... useref index.html ...');
console.log('************************************************************************************');
userefIndexHtml(path.join(platformPath, "index.html"));
function userefIndexHtml(file)
{
var opts = {};
try {
var res = String(fs.readFileSync(file));
var output = useref(res, opts);
var html = output[0];
console.log('html', html);
fs.writeFileSync(file, html);
} catch (err) {
console.log('userefIndexHtml: Error', err);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment