forked from addyosmani/critical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·179 lines (162 loc) · 5.2 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env node
'use strict';
const os = require('os');
const path = require('path');
const chalk = require('chalk');
const meow = require('meow');
const groupArgs = require('group-args');
const indentString = require('indent-string');
const stdin = require('get-stdin');
const assign = require('lodash/assign');
const reduce = require('lodash/reduce');
const isString = require('lodash/isString');
const isRegExp = require('lodash/isRegExp');
const map = require('lodash/map');
const escapeRegExp = require('lodash/escapeRegExp');
const file = require('./lib/file-helper');
const critical = require('./');
let ok;
const help = [
'Usage: critical <input> [<option>]',
'',
'Options:',
' -b, --base Your base directory',
' -c, --css Your CSS Files (optional)',
' -w, --width Viewport width',
' -h, --height Viewport height',
' -m, --minify Minify critical-path CSS when inlining',
' -i, --inline Generate the HTML with inlined critical-path CSS',
' -e, --extract Extract inlined styles from referenced stylesheets',
' -p, --pathPrefix Path to prepend CSS assets with (defaults to /) ',
' -f, --folder HTML Subfolder (default: \'\')',
' --ii, --inlineImages Inline images',
' --ignore RegExp, @type or selector to ignore',
' --include RegExp, @type or selector to include',
' --maxFileSize Sets a max file size (in bytes) for base64 inlined images',
' --assetPaths Directories/Urls where the inliner should start looking for assets.',
' --timeout Sets the maximum timeout (in milliseconds) for the operation (defaults to 30000 ms).',
' ----------------------------------------------------------------------.',
' Deprecated - use "--inline" to retrieve the modified HTML',
' critical source.html --inline > dest.html',
' -----------------------------------------------------------------------',
' -H, --htmlTarget Target for final HTML output',
' -S, --styleTarget Target for generated critical-path CSS (which we inline)'
];
const minimistOpts = {
alias: {
b: 'base',
c: 'css',
w: 'width',
h: 'height',
f: 'folder',
H: 'htmlTarget',
i: 'inline',
I: 'ignore',
S: 'styleTarget',
m: 'minify',
e: 'extract',
p: 'pathPrefix',
ii: 'inlineImages'
}
};
const cli = meow({help}, minimistOpts);
// Group args for inline-critical and penthouse
cli.flags = groupArgs(['inline', 'penthouse'], {
delimiter: '-'
}, minimistOpts);
// Cleanup cli flags and assert cammelcase keeps camelcase
cli.flags = reduce(cli.flags, (res, val, key) => {
if (key.length <= 1) {
return res;
}
switch (key) {
case 'htmltarget':
res.htmlTarget = val;
break;
case 'styletarget':
res.styleTarget = val;
break;
case 'pathprefix':
res.pathPrefix = val;
break;
case 'inlineimages':
res.inlineImages = val;
break;
case 'maxfilesize':
res.maxFileSize = val;
break;
case 'timeout':
res.timeout = val;
break;
case 'assetpaths':
case 'assetPaths':
if (isString(val)) {
val = [val];
}
res.assetPaths = val;
break;
case 'include':
case 'ignore':
if (isString(val) || isRegExp(val)) {
val = [val];
}
res[key] = map(val || [], entry => {
// Check regex
const match = entry.match(/^\/(.*)\/([igmy]+)?$/);
if (match) {
return new RegExp(escapeRegExp(match[1]), match[2]);
}
return entry;
});
break;
default:
res[key] = val;
break;
}
return res;
}, {});
function error(err) {
process.stderr.write(indentString((chalk.red('Error: ') + err.message || err), 3));
process.stderr.write(os.EOL);
process.stderr.write(indentString(help.join(os.EOL), 3));
process.exit(1);
}
function run(data) {
const opts = assign({base: process.cwd()}, cli.flags);
const command = opts.htmlTarget || opts.inline ? 'generateInline' : 'generate';
if (command === 'generate') {
opts.dest = opts.styleTarget || '';
}
ok = true;
if (data) {
opts.html = data;
} else {
opts.src = cli.input[0];
if (opts.src && !file.isExternal(opts.src)) {
opts.src = path.resolve(cli.input[0]);
}
}
try {
critical[command](opts, (err, val) => {
if (err) {
error(err);
} else {
process.stdout.write(val, process.exit);
}
});
} catch (err) {
error(err);
}
}
if (cli.input[0]) {
run();
} else {
// Get stdin
stdin().then(run);
setTimeout(() => {
if (ok) {
return;
}
run();
}, 100);
}