inline-critical/cli.js

177 lines
3.4 KiB
JavaScript
Raw Normal View History

2015-06-25 06:27:18 +02:00
#!/usr/bin/env node
2015-06-25 06:27:18 +02:00
'use strict';
2017-03-20 22:42:19 +01:00
const os = require('os');
const fs = require('fs');
const meow = require('meow');
2018-12-30 23:41:29 +01:00
const chalk = require('chalk');
2017-03-20 22:42:19 +01:00
const indentString = require('indent-string');
const stdin = require('get-stdin');
const css = require('css');
const _ = require('lodash');
2018-03-05 20:56:57 +01:00
const inlineCritical = require('.');
2015-06-25 06:27:18 +02:00
2017-03-20 22:42:19 +01:00
let ok;
2018-03-05 20:56:57 +01:00
const help = `
Usage: inline-critical <input> [<option>]
2015-06-25 06:27:18 +02:00
2018-03-05 20:56:57 +01:00
Options:
-c, --css Path to CSS file
-h, --html Path to HTML file
-i, --ignore Skip matching stylesheets
-m, --minify Minify the styles before inlining (default)
-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
2019-03-26 17:31:54 +01:00
--noscript Position of noscript fallback ('body' - end of body, 'head' - end of head, false - no noscript)
2018-03-05 20:56:57 +01:00
`;
const cli = meow(help, {
2018-12-18 12:51:30 +01:00
autoHelp: true,
autoVersion: true,
flags: {
css: {
type: 'string',
alias: 'c',
},
html: {
type: 'string',
alias: 'h',
},
ignore: {
type: 'string',
alias: 'i',
},
minify: {
type: 'boolean',
alias: 'm',
2018-12-30 23:41:29 +01:00
default: true,
2018-12-18 12:51:30 +01:00
},
extract: {
type: 'boolean',
alias: 'e',
},
base: {
type: 'string',
alias: 'b',
},
selector: {
type: 'string',
alias: 's',
},
2019-03-26 17:31:54 +01:00
noscript: {
type: 'string',
},
2018-12-18 12:51:30 +01:00
},
2015-06-25 06:27:18 +02:00
});
2017-03-20 22:42:19 +01:00
// Cleanup cli flags
2018-12-18 12:51:30 +01:00
cli.flags = _.reduce(
cli.flags,
(res, val, key) => {
2015-11-13 22:59:10 +01:00
if (key.length <= 1) {
2018-12-18 12:51:30 +01:00
return res;
2015-11-13 22:59:10 +01:00
}
2015-06-25 06:27:18 +02:00
2015-11-13 22:59:10 +01:00
switch (key) {
2018-12-18 12:51:30 +01:00
case 'css':
case 'html':
try {
res[key] = read(val);
} catch (error) {}
2019-06-13 23:31:53 +02:00
2018-12-18 12:51:30 +01:00
break;
case 'base':
res.basePath = val;
break;
case 'ignore':
if (_.isString(val) || _.isRegExp(val)) {
val = [val];
}
2019-06-13 23:31:53 +02:00
2018-12-18 12:51:30 +01:00
res.ignore = _.map(val || [], ignore => {
// Check regex
const match = ignore.match(/^\/(.*)\/([igmy]+)?$/);
if (match) {
return new RegExp(_.escapeRegExp(match[1]), match[2]);
}
2019-06-13 23:31:53 +02:00
2018-12-18 12:51:30 +01:00
return ignore;
});
break;
default:
res[key] = val;
break;
2015-11-13 22:59:10 +01:00
}
2015-06-25 06:27:18 +02:00
2015-11-13 22:59:10 +01:00
return res;
2018-12-18 12:51:30 +01:00
},
{}
);
2015-06-25 06:27:18 +02:00
2018-09-15 23:16:53 +02:00
function processError(err) {
process.stderr.write(chalk.red(indentString(`Error: ${err.message || err}`, 2)));
2018-12-18 12:51:30 +01:00
process.stderr.write(os.EOL);
2018-12-30 23:41:29 +01:00
process.stderr.write(indentString(help, 2));
process.exit(1);
2015-06-25 06:27:18 +02:00
}
function read(file) {
2018-12-18 12:51:30 +01:00
try {
return fs.readFileSync(file, 'utf8');
} catch (error) {
processError(error);
}
2015-06-25 06:27:18 +02:00
}
function run(data) {
2018-12-18 12:51:30 +01:00
const opts = _.defaults(cli.flags, {basePath: process.cwd()});
ok = true;
2015-11-13 22:59:10 +01:00
2018-12-18 12:51:30 +01:00
if (data) {
// Detect html
try {
css.parse(data);
opts.css = data;
} catch (error) {
opts.html = data;
2015-06-25 06:27:18 +02:00
}
2018-12-18 12:51:30 +01:00
}
2015-06-25 06:27:18 +02:00
2018-12-18 12:51:30 +01:00
_.forEach(cli.input, file => {
const tmp = read(file);
2015-06-25 06:27:18 +02:00
try {
2018-12-18 12:51:30 +01:00
css.parse(tmp);
opts.css = tmp;
2018-09-15 23:16:53 +02:00
} catch (error) {
2018-12-18 12:51:30 +01:00
opts.html = tmp;
2015-06-25 06:27:18 +02:00
}
2018-12-18 12:51:30 +01:00
});
if (!opts.html || !opts.css) {
cli.showHelp();
}
const {html, css: styles, ...options} = opts;
try {
const out = inlineCritical(html, styles, options);
process.stdout.write(out.toString(), process.exit);
} catch (error) {
processError(error);
}
2015-06-25 06:27:18 +02:00
}
2017-03-20 22:42:19 +01:00
// Get stdin
2015-09-16 06:28:54 +02:00
stdin().then(run);
2017-03-20 22:42:19 +01:00
setTimeout(() => {
2018-12-18 12:51:30 +01:00
if (ok) {
return;
}
2019-06-13 23:31:53 +02:00
2018-12-18 12:51:30 +01:00
run();
}, 100);