cli.js
11.4 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env node
/**
* html-minifier CLI tool
*
* The MIT License (MIT)
*
* Copyright (c) 2014-2016 Zoltan Frombach
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
'use strict';
var camelCase = require('camel-case');
var fs = require('fs');
var info = require('./package.json');
var minify = require('./' + info.main).minify;
var paramCase = require('param-case');
var path = require('path');
var program = require('commander');
program._name = info.name;
program.version(info.version);
function fatal(message) {
console.error(message);
process.exit(1);
}
/**
* JSON does not support regexes, so, e.g., JSON.parse() will not create
* a RegExp from the JSON value `[ "/matchString/" ]`, which is
* technically just an array containing a string that begins and end with
* a forward slash. To get a RegExp from a JSON string, it must be
* constructed explicitly in JavaScript.
*
* The likelihood of actually wanting to match text that is enclosed in
* forward slashes is probably quite rare, so if forward slashes were
* included in an argument that requires a regex, the user most likely
* thought they were part of the syntax for specifying a regex.
*
* In the unlikely case that forward slashes are indeed desired in the
* search string, the user would need to enclose the expression in a
* second set of slashes:
*
* --customAttrSrround "[\"//matchString//\"]"
*/
function parseRegExp(value) {
if (value) {
return new RegExp(value.replace(/^\/(.*)\/$/, '$1'));
}
}
function parseJSON(value) {
if (value) {
try {
return JSON.parse(value);
}
catch (e) {
if (/^{/.test(value)) {
fatal('Could not parse JSON value \'' + value + '\'');
}
return value;
}
}
}
function parseJSONArray(value) {
if (value) {
value = parseJSON(value);
return Array.isArray(value) ? value : [value];
}
}
function parseJSONRegExpArray(value) {
value = parseJSONArray(value);
return value && value.map(parseRegExp);
}
function parseString(value) {
return value;
}
var mainOptions = {
caseSensitive: 'Treat attributes in case sensitive manner (useful for SVG; e.g. viewBox)',
collapseBooleanAttributes: 'Omit attribute values from boolean attributes',
collapseInlineTagWhitespace: 'Collapse white space around inline tag',
collapseWhitespace: 'Collapse white space that contributes to text nodes in a document tree.',
conservativeCollapse: 'Always collapse to 1 space (never remove it entirely)',
customAttrAssign: ['Arrays of regex\'es that allow to support custom attribute assign expressions (e.g. \'<div flex?="{{mode != cover}}"></div>\')', parseJSONRegExpArray],
customAttrCollapse: ['Regex that specifies custom attribute to strip newlines from (e.g. /ng-class/)', parseRegExp],
customAttrSurround: ['Arrays of regex\'es that allow to support custom attribute surround expressions (e.g. <input {{#if value}}checked="checked"{{/if}}>)', parseJSONRegExpArray],
customEventAttributes: ['Arrays of regex\'es that allow to support custom event attributes for minifyJS (e.g. ng-click)', parseJSONRegExpArray],
decodeEntities: 'Use direct Unicode characters whenever possible',
html5: 'Parse input according to HTML5 specifications',
ignoreCustomComments: ['Array of regex\'es that allow to ignore certain comments, when matched', parseJSONRegExpArray],
ignoreCustomFragments: ['Array of regex\'es that allow to ignore certain fragments, when matched (e.g. <?php ... ?>, {{ ... }})', parseJSONRegExpArray],
includeAutoGeneratedTags: 'Insert tags generated by HTML parser',
keepClosingSlash: 'Keep the trailing slash on singleton elements',
maxLineLength: ['Max line length', parseInt],
minifyCSS: ['Minify CSS in style elements and style attributes (uses clean-css)', parseJSON],
minifyJS: ['Minify Javascript in script elements and on* attributes (uses uglify-js)', parseJSON],
minifyURLs: ['Minify URLs in various attributes (uses relateurl)', parseJSON],
preserveLineBreaks: 'Always collapse to 1 line break (never remove it entirely) when whitespace between tags include a line break.',
preventAttributesEscaping: 'Prevents the escaping of the values of attributes.',
processConditionalComments: 'Process contents of conditional comments through minifier',
processScripts: ['Array of strings corresponding to types of script elements to process through minifier (e.g. "text/ng-template", "text/x-handlebars-template", etc.)', parseJSONArray],
quoteCharacter: ['Type of quote to use for attribute values (\' or ")', parseString],
removeAttributeQuotes: 'Remove quotes around attributes when possible.',
removeComments: 'Strip HTML comments',
removeEmptyAttributes: 'Remove all attributes with whitespace-only values',
removeEmptyElements: 'Remove all elements with empty contents',
removeOptionalTags: 'Remove unrequired tags',
removeRedundantAttributes: 'Remove attributes when value matches default.',
removeScriptTypeAttributes: 'Remove type="text/javascript" from script tags. Other type attribute values are left intact.',
removeStyleLinkTypeAttributes: 'Remove type="text/css" from style and link tags. Other type attribute values are left intact.',
removeTagWhitespace: 'Remove space between attributes whenever possible',
sortAttributes: 'Sort attributes by frequency',
sortClassName: 'Sort style classes by frequency',
trimCustomFragments: 'Trim white space around ignoreCustomFragments.',
useShortDoctype: 'Replaces the doctype with the short (HTML5) doctype'
};
var mainOptionKeys = Object.keys(mainOptions);
mainOptionKeys.forEach(function(key) {
var option = mainOptions[key];
if (Array.isArray(option)) {
key = key === 'minifyURLs' ? '--minify-urls' : '--' + paramCase(key);
key += option[1] === parseJSON ? ' [value]' : ' <value>';
program.option(key, option[0], option[1]);
}
else if (~['html5', 'includeAutoGeneratedTags'].indexOf(key)) {
program.option('--no-' + paramCase(key), option);
}
else {
program.option('--' + paramCase(key), option);
}
});
program.option('-o --output <file>', 'Specify output file (if not specified STDOUT will be used for output)');
function readFile(file) {
try {
return fs.readFileSync(file, { encoding: 'utf8' });
}
catch (e) {
fatal('Cannot read ' + file + '\n' + e.message);
}
}
var config = {};
program.option('-c --config-file <file>', 'Use config file', function(configPath) {
var data = readFile(configPath);
try {
config = JSON.parse(data);
}
catch (je) {
try {
config = require(path.resolve(configPath));
}
catch (ne) {
fatal('Cannot read the specified config file.\nAs JSON: ' + je.message + '\nAs module: ' + ne.message);
}
}
mainOptionKeys.forEach(function(key) {
if (key in config) {
var option = mainOptions[key];
if (Array.isArray(option)) {
var value = config[key];
config[key] = option[1](typeof value === 'string' ? value : JSON.stringify(value));
}
}
});
});
program.option('--input-dir <dir>', 'Specify an input directory');
program.option('--output-dir <dir>', 'Specify an output directory');
program.option('--file-ext <text>', 'Specify an extension to be read, ex: html');
var content;
program.arguments('[files...]').action(function(files) {
content = files.map(readFile).join('');
}).parse(process.argv);
function createOptions() {
var options = {};
mainOptionKeys.forEach(function(key) {
var param = program[camelCase(key)];
if (typeof param !== 'undefined') {
options[key] = param;
}
else if (key in config) {
options[key] = config[key];
}
});
return options;
}
function mkdir(outputDir, callback) {
fs.mkdir(outputDir, function(err) {
if (err) {
switch (err.code) {
case 'ENOENT':
return mkdir(path.join(outputDir, '..'), function() {
mkdir(outputDir, callback);
});
case 'EEXIST':
break;
default:
fatal('Cannot create directory ' + outputDir + '\n' + err.message);
}
}
callback();
});
}
function processFile(inputFile, outputFile) {
fs.readFile(inputFile, { encoding: 'utf8' }, function(err, data) {
if (err) {
fatal('Cannot read ' + inputFile + '\n' + err.message);
}
var minified;
try {
minified = minify(data, createOptions());
}
catch (e) {
fatal('Minification error on ' + inputFile + '\n' + e.message);
}
fs.writeFile(outputFile, minified, { encoding: 'utf8' }, function(err) {
if (err) {
fatal('Cannot write ' + outputFile + '\n' + err.message);
}
});
});
}
function processDirectory(inputDir, outputDir, fileExt) {
fs.readdir(inputDir, function(err, files) {
if (err) {
fatal('Cannot read directory ' + inputDir + '\n' + err.message);
}
files.forEach(function(file) {
var inputFile = path.join(inputDir, file);
var outputFile = path.join(outputDir, file);
fs.stat(inputFile, function(err, stat) {
if (err) {
fatal('Cannot read ' + inputFile + '\n' + err.message);
}
else if (stat.isDirectory()) {
processDirectory(inputFile, outputFile, fileExt);
}
else if (!fileExt || path.extname(file) === '.' + fileExt) {
mkdir(outputDir, function() {
processFile(inputFile, outputFile);
});
}
});
});
});
}
function writeMinify() {
var minified;
try {
minified = minify(content, createOptions());
}
catch (e) {
fatal('Minification error:\n' + e.message);
}
(program.output ? fs.createWriteStream(program.output).on('error', function(e) {
fatal('Cannot write ' + program.output + '\n' + e.message);
}) : process.stdout).write(minified);
}
var inputDir = program.inputDir;
var outputDir = program.outputDir;
var fileExt = program.fileExt;
if (inputDir || outputDir) {
if (!inputDir) {
fatal('The option output-dir needs to be used with the option input-dir. If you are working with a single file, use -o.');
}
else if (!outputDir) {
fatal('You need to specify where to write the output files with the option --output-dir');
}
processDirectory(inputDir, outputDir, fileExt);
}
// Minifying one or more files specified on the CMD line
else if (typeof content === 'string') {
writeMinify();
}
// Minifying input coming from STDIN
else {
content = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data) {
content += data;
}).on('end', writeMinify);
}