2015-06-25 06:27:18 +02:00
|
|
|
#!/usr/bin/env node
|
|
|
|
'use strict';
|
|
|
|
var os = require('os');
|
|
|
|
var fs = require('fs');
|
|
|
|
var meow = require('meow');
|
|
|
|
var indentString = require('indent-string');
|
|
|
|
var stdin = require('get-stdin');
|
|
|
|
var css = require('css');
|
|
|
|
var _ = require('lodash');
|
|
|
|
var inlineCritical = require('./');
|
|
|
|
var ok;
|
|
|
|
|
|
|
|
var help = [
|
2015-11-13 22:59:10 +01:00
|
|
|
'Usage: inline-critical <input> [<option>]',
|
|
|
|
'',
|
|
|
|
'Options:',
|
|
|
|
' -c, --css Path to CSS file',
|
|
|
|
' -h, --html Path to HTML file',
|
|
|
|
' -i, --ignore Skip matching stylesheets',
|
2016-04-17 00:58:04 +02:00
|
|
|
' -m, --minify Minify the styles before inlining (default)',
|
2015-11-13 22:59:10 +01:00
|
|
|
' -e, --extract Remove the inlined styles from any stylesheets referenced in the HTML',
|
|
|
|
' -b, --base Is used when extracting styles to find the files references by `href` attributes',
|
|
|
|
' -s, --selector Optionally defines the element used by loadCSS as a reference for inlining'
|
2015-06-25 06:27:18 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
var cli = meow({
|
2015-11-13 22:59:10 +01:00
|
|
|
help: help
|
2015-06-25 06:27:18 +02:00
|
|
|
}, {
|
2015-11-13 22:59:10 +01:00
|
|
|
alias: {
|
|
|
|
c: 'css',
|
|
|
|
h: 'html',
|
|
|
|
i: 'ignore',
|
|
|
|
m: 'minify',
|
|
|
|
b: 'base',
|
|
|
|
e: 'extract',
|
|
|
|
s: 'selector'
|
|
|
|
}
|
2015-06-25 06:27:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// cleanup cli flags
|
|
|
|
cli.flags = _.reduce(cli.flags, function (res, val, key) {
|
2015-11-13 22:59:10 +01:00
|
|
|
if (key.length <= 1) {
|
|
|
|
return res;
|
|
|
|
}
|
2015-06-25 06:27:18 +02:00
|
|
|
|
2015-11-13 22:59:10 +01:00
|
|
|
switch (key) {
|
|
|
|
case 'css':
|
|
|
|
case 'html':
|
|
|
|
try {
|
|
|
|
res[key] = read(val);
|
|
|
|
} catch (err) {
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'base':
|
|
|
|
res.basePath = val;
|
|
|
|
break;
|
|
|
|
case 'ignore':
|
|
|
|
if (_.isString(val) || _.isRegExp(val)) {
|
|
|
|
val = [val];
|
|
|
|
}
|
|
|
|
res.ignore = _.map(val || [], function (ignore) {
|
|
|
|
// check regex
|
|
|
|
var match = ignore.match(/^\/(.*)\/([igmy]+)?$/);
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
return new RegExp(_.escapeRegExp(match[1]), match[2]);
|
|
|
|
}
|
|
|
|
return ignore;
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res[key] = val;
|
|
|
|
break;
|
|
|
|
}
|
2015-06-25 06:27:18 +02:00
|
|
|
|
2015-11-13 22:59:10 +01:00
|
|
|
return res;
|
2015-06-25 06:27:18 +02:00
|
|
|
}, {});
|
|
|
|
|
|
|
|
function error(err) {
|
2015-11-13 22:59:10 +01:00
|
|
|
process.stderr.write(indentString(err.message || err, ' Error: '));
|
|
|
|
process.stderr.write(os.EOL);
|
|
|
|
process.stderr.write(indentString(help.join(os.EOL), ' '));
|
|
|
|
process.exit(1);
|
2015-06-25 06:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function read(file) {
|
2015-11-13 22:59:10 +01:00
|
|
|
try {
|
|
|
|
return fs.readFileSync(file, 'utf8');
|
|
|
|
} catch (err) {
|
|
|
|
error(err);
|
|
|
|
}
|
2015-06-25 06:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function run(data) {
|
2015-11-13 22:59:10 +01:00
|
|
|
var opts = _.defaults(cli.flags, {basePath: process.cwd()});
|
|
|
|
ok = true;
|
|
|
|
|
|
|
|
if (data) {
|
|
|
|
// detect html
|
|
|
|
try {
|
|
|
|
css.parse(data);
|
|
|
|
opts.css = data;
|
|
|
|
} catch (err) {
|
|
|
|
opts.html = data;
|
|
|
|
}
|
|
|
|
}
|
2015-06-25 06:27:18 +02:00
|
|
|
|
2015-11-13 22:59:10 +01:00
|
|
|
_.forEach(cli.input, function (file) {
|
|
|
|
var tmp = read(file);
|
|
|
|
try {
|
|
|
|
css.parse(tmp);
|
|
|
|
opts.css = tmp;
|
|
|
|
} catch (err) {
|
|
|
|
opts.html = tmp;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!opts.html || !opts.css) {
|
|
|
|
cli.showHelp();
|
2015-06-25 06:27:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2015-11-13 22:59:10 +01:00
|
|
|
var out = inlineCritical(opts.html, opts.css, opts);
|
|
|
|
console.log(out.toString());
|
2015-06-25 06:27:18 +02:00
|
|
|
} catch (err) {
|
2015-11-13 22:59:10 +01:00
|
|
|
error(err);
|
2015-06-25 06:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get stdin
|
2015-09-16 06:28:54 +02:00
|
|
|
stdin().then(run);
|
2015-06-25 23:27:56 +02:00
|
|
|
setTimeout(function () {
|
2015-11-13 22:59:10 +01:00
|
|
|
if (ok) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
run();
|
2015-06-25 23:27:56 +02:00
|
|
|
}, 100);
|