html-minifier 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -34
- package/cli.js +218 -282
- package/package.json +4 -8
- package/sample-cli-config-file.conf +27 -18
- package/src/htmllint.js +67 -55
- package/src/htmlminifier.js +223 -114
- package/src/htmlparser.js +132 -130
- package/src/tokenchain.js +58 -0
- package/CHANGELOG.md +0 -120
- package/dist/htmlminifier.js +0 -32753
- package/dist/htmlminifier.min.js +0 -18
package/cli.js
CHANGED
|
@@ -27,325 +27,261 @@
|
|
|
27
27
|
|
|
28
28
|
'use strict';
|
|
29
29
|
|
|
30
|
-
var cli = require('cli');
|
|
31
|
-
var concat = require('concat-stream');
|
|
32
30
|
var changeCase = require('change-case');
|
|
33
|
-
var
|
|
31
|
+
var concat = require('concat-stream');
|
|
34
32
|
var fs = require('fs');
|
|
35
|
-
var appName = require('./package.json').name;
|
|
36
|
-
var appVersion = require('./package.json').version;
|
|
37
|
-
var minify = require('.').minify;
|
|
38
33
|
var HTMLLint = require('./src/htmllint').HTMLLint;
|
|
39
|
-
var
|
|
40
|
-
var
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
cli.option_width = 40;
|
|
44
|
-
cli.setApp(appName, appVersion);
|
|
45
|
-
|
|
46
|
-
var usage = appName + ' [OPTIONS] [FILE(s)]\n\n';
|
|
47
|
-
usage += ' If no input files are specified then STDIN will be used for input.\n';
|
|
48
|
-
usage += ' If more than one input file is specified then those will be concatenated and minified together.\n\n';
|
|
49
|
-
usage += ' When you specify a config file with the --config-file option (see sample-cli-config-file.conf for format)\n';
|
|
50
|
-
usage += ' you can still override some of its contents by providing individual command line options, too.\n\n';
|
|
51
|
-
usage += ' When you want to provide an array of strings for --ignore-custom-comments or --process-scripts options\n';
|
|
52
|
-
usage += ' on the command line you must escape those such as --ignore-custom-comments "[\\"string1\\",\\"string1\\"]"\n';
|
|
53
|
-
|
|
54
|
-
cli.setUsage(usage);
|
|
55
|
-
|
|
56
|
-
var mainOptions = {
|
|
57
|
-
caseSensitive: [[false, 'Treat attributes in case sensitive manner (useful for SVG; e.g. viewBox)']],
|
|
58
|
-
collapseBooleanAttributes: [[false, 'Omit attribute values from boolean attributes']],
|
|
59
|
-
collapseInlineTagWhitespace: [[false, 'Collapse white space around inline tag']],
|
|
60
|
-
collapseWhitespace: [[false, 'Collapse white space that contributes to text nodes in a document tree.']],
|
|
61
|
-
conservativeCollapse: [[false, 'Always collapse to 1 space (never remove it entirely)']],
|
|
62
|
-
customAttrAssign: [[false, 'Arrays of regex\'es that allow to support custom attribute assign expressions (e.g. \'<div flex?="{{mode != cover}}"></div>\')', 'string'], 'json-regex'],
|
|
63
|
-
customAttrCollapse: [[false, 'Regex that specifies custom attribute to strip newlines from (e.g. /ng\-class/)', 'string'], 'string-regex'],
|
|
64
|
-
customAttrSurround: [[false, 'Arrays of regex\'es that allow to support custom attribute surround expressions (e.g. <input {{#if value}}checked="checked"{{/if}}>)', 'string'], 'json-regex'],
|
|
65
|
-
customEventAttributes: [[false, 'Arrays of regex\'es that allow to support custom event attributes for minifyJS (e.g. ng-click)', 'string'], 'json-regex'],
|
|
66
|
-
html5: [[false, 'Parse input according to HTML5 specifications']],
|
|
67
|
-
ignoreCustomComments: [[false, 'Array of regex\'es that allow to ignore certain comments, when matched', 'string'], 'json-regex'],
|
|
68
|
-
ignoreCustomFragments: [[false, 'Array of regex\'es that allow to ignore certain fragments, when matched (e.g. <?php ... ?>, {{ ... }})', 'string'], 'json-regex'],
|
|
69
|
-
includeAutoGeneratedTags: [[false, 'Insert tags generated by HTML parser'], true],
|
|
70
|
-
keepClosingSlash: [[false, 'Keep the trailing slash on singleton elements']],
|
|
71
|
-
lint: [[false, 'Toggle linting']],
|
|
72
|
-
maxLineLength: [[false, 'Max line length', 'number'], true],
|
|
73
|
-
minifyCSS: [[false, 'Minify CSS in style elements and style attributes (uses clean-css)']],
|
|
74
|
-
minifyJS: [[false, 'Minify Javascript in script elements and on* attributes (uses UglifyJS)']],
|
|
75
|
-
minifyURLs: [[false, 'Minify URLs in various attributes (uses relateurl)', 'string'], 'site-url'],
|
|
76
|
-
preserveLineBreaks: [[false, 'Always collapse to 1 line break (never remove it entirely) when whitespace between tags include a line break.']],
|
|
77
|
-
preventAttributesEscaping: [[false, 'Prevents the escaping of the values of attributes.']],
|
|
78
|
-
processConditionalComments: [[false, 'Process contents of conditional comments through minifier']],
|
|
79
|
-
processScripts: [[false, 'Array of strings corresponding to types of script elements to process through minifier (e.g. "text/ng-template", "text/x-handlebars-template", etc.)', 'string'], 'json'],
|
|
80
|
-
quoteCharacter: [[false, 'Type of quote to use for attribute values (\' or ")', 'string'], true],
|
|
81
|
-
removeAttributeQuotes: [[false, 'Remove quotes around attributes when possible.']],
|
|
82
|
-
removeComments: [[false, 'Strip HTML comments']],
|
|
83
|
-
removeEmptyAttributes: [[false, 'Remove all attributes with whitespace-only values']],
|
|
84
|
-
removeEmptyElements: [[false, 'Remove all elements with empty contents']],
|
|
85
|
-
removeOptionalTags: [[false, 'Remove unrequired tags']],
|
|
86
|
-
removeRedundantAttributes: [[false, 'Remove attributes when value matches default.']],
|
|
87
|
-
removeScriptTypeAttributes: [[false, 'Remove type="text/javascript" from script tags. Other type attribute values are left intact.']],
|
|
88
|
-
removeStyleLinkTypeAttributes: [[false, 'Remove type="text/css" from style and link tags. Other type attribute values are left intact.']],
|
|
89
|
-
removeTagWhitespace: [[false, 'Remove space between attributes whenever possible.']],
|
|
90
|
-
useShortDoctype: [[false, 'Replaces the doctype with the short (HTML5) doctype']]
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
var cliOptions = {
|
|
94
|
-
version: ['v', 'Version information'],
|
|
95
|
-
output: ['o', 'Specify output file (if not specified STDOUT will be used for output)', 'file'],
|
|
96
|
-
'config-file': ['c', 'Use config file', 'file'],
|
|
97
|
-
'input-dir': [false, 'Specify an input directory', 'dir'],
|
|
98
|
-
'output-dir': [false, 'Specify an output directory', 'dir']
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
var mainOptionKeys = Object.keys(mainOptions);
|
|
102
|
-
mainOptionKeys.forEach(function(key) {
|
|
103
|
-
cliOptions[changeCase.paramCase(key)] = mainOptions[key][0];
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
cli.parse(cliOptions);
|
|
34
|
+
var info = require('./package.json');
|
|
35
|
+
var minify = require('.').minify;
|
|
36
|
+
var path = require('path');
|
|
37
|
+
var program = require('commander');
|
|
107
38
|
|
|
108
|
-
|
|
39
|
+
program.version(info.version).description(info.name + ' command-line tool');
|
|
109
40
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
// a forward slash. To get a RegExp from a JSON string, it must be
|
|
115
|
-
// constructed explicitly in JavaScript.
|
|
116
|
-
//
|
|
117
|
-
// The likelihood of actually wanting to match text that is enclosed in
|
|
118
|
-
// forward slashes is probably quite rare, so if forward slashes were
|
|
119
|
-
// included in an argument that requires a regex, the user most likely
|
|
120
|
-
// thought they were part of the syntax for specifying a regex.
|
|
121
|
-
//
|
|
122
|
-
// In the unlikely case that forward slashes are indeed desired in the
|
|
123
|
-
// search string, the user would need to enclose the expression in a
|
|
124
|
-
// second set of slashes:
|
|
125
|
-
//
|
|
126
|
-
// --customAttrSrround "[\"//matchString//\"]"
|
|
127
|
-
//
|
|
128
|
-
if (value) {
|
|
129
|
-
var stripSlashes = /^\/(.*)\/$/.exec(value);
|
|
130
|
-
if (stripSlashes) {
|
|
131
|
-
value = stripSlashes[1];
|
|
132
|
-
}
|
|
133
|
-
return new RegExp(value);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
41
|
+
function fatal(message) {
|
|
42
|
+
console.error(message);
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
136
45
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
46
|
+
/**
|
|
47
|
+
* JSON does not support regexes, so, e.g., JSON.parse() will not create
|
|
48
|
+
* a RegExp from the JSON value `[ "/matchString/" ]`, which is
|
|
49
|
+
* technically just an array containing a string that begins and end with
|
|
50
|
+
* a forward slash. To get a RegExp from a JSON string, it must be
|
|
51
|
+
* constructed explicitly in JavaScript.
|
|
52
|
+
*
|
|
53
|
+
* The likelihood of actually wanting to match text that is enclosed in
|
|
54
|
+
* forward slashes is probably quite rare, so if forward slashes were
|
|
55
|
+
* included in an argument that requires a regex, the user most likely
|
|
56
|
+
* thought they were part of the syntax for specifying a regex.
|
|
57
|
+
*
|
|
58
|
+
* In the unlikely case that forward slashes are indeed desired in the
|
|
59
|
+
* search string, the user would need to enclose the expression in a
|
|
60
|
+
* second set of slashes:
|
|
61
|
+
*
|
|
62
|
+
* --customAttrSrround "[\"//matchString//\"]"
|
|
63
|
+
*/
|
|
64
|
+
function parseRegExp(value) {
|
|
65
|
+
if (value) {
|
|
66
|
+
return new RegExp(value.replace(/^\/(.*)\/$/, '$1'));
|
|
151
67
|
}
|
|
68
|
+
}
|
|
152
69
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
var minified = null;
|
|
70
|
+
function parseJSON(value) {
|
|
71
|
+
if (value) {
|
|
156
72
|
try {
|
|
157
|
-
|
|
73
|
+
return JSON.parse(value);
|
|
158
74
|
}
|
|
159
75
|
catch (e) {
|
|
160
|
-
|
|
161
|
-
cli.error('Minification error');
|
|
162
|
-
process.stderr.write((e.stack || e).toString());
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
if (minifyOptions.lint) {
|
|
166
|
-
minifyOptions.lint.populate();
|
|
76
|
+
fatal('Could not parse JSON value \'' + value + '\'');
|
|
167
77
|
}
|
|
168
|
-
|
|
169
|
-
if (minified !== null) {
|
|
170
|
-
// Write the output
|
|
171
|
-
if (output) {
|
|
172
|
-
try {
|
|
173
|
-
fs.writeFileSync(path.resolve(output), minified);
|
|
174
|
-
}
|
|
175
|
-
catch (e) {
|
|
176
|
-
status = 4;
|
|
177
|
-
cli.error('Cannot write to ' + output);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
else {
|
|
181
|
-
process.stdout.write(minified);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
return status;
|
|
186
78
|
}
|
|
79
|
+
}
|
|
187
80
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
catch (e) {
|
|
193
|
-
if (e.code !== 'EEXIST') {
|
|
194
|
-
cli.fatal('Can not create directory ' + path);
|
|
195
|
-
return 3;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
return 0;
|
|
81
|
+
function parseJSONArray(value) {
|
|
82
|
+
if (value) {
|
|
83
|
+
value = parseJSON(value);
|
|
84
|
+
return Array.isArray(value) ? value : [value];
|
|
199
85
|
}
|
|
86
|
+
}
|
|
200
87
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
88
|
+
function parseJSONRegExpArray(value) {
|
|
89
|
+
value = parseJSONArray(value);
|
|
90
|
+
return value && value.map(parseRegExp);
|
|
91
|
+
}
|
|
204
92
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
var outputFilePath = outputDir + '/' + fileName;
|
|
93
|
+
function parseSiteURL(value) {
|
|
94
|
+
return value && { site: value };
|
|
95
|
+
}
|
|
209
96
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
214
|
-
else {
|
|
215
|
-
var originalContent = fs.readFileSync(inputFilePath, 'utf8');
|
|
216
|
-
status = runMinify(originalContent, outputFilePath);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
97
|
+
function parseString(value) {
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
219
100
|
|
|
220
|
-
|
|
101
|
+
var mainOptions = {
|
|
102
|
+
caseSensitive: 'Treat attributes in case sensitive manner (useful for SVG; e.g. viewBox)',
|
|
103
|
+
collapseBooleanAttributes: 'Omit attribute values from boolean attributes',
|
|
104
|
+
collapseInlineTagWhitespace: 'Collapse white space around inline tag',
|
|
105
|
+
collapseWhitespace: 'Collapse white space that contributes to text nodes in a document tree.',
|
|
106
|
+
conservativeCollapse: 'Always collapse to 1 space (never remove it entirely)',
|
|
107
|
+
customAttrAssign: ['Arrays of regex\'es that allow to support custom attribute assign expressions (e.g. \'<div flex?="{{mode != cover}}"></div>\')', parseJSONRegExpArray],
|
|
108
|
+
customAttrCollapse: ['Regex that specifies custom attribute to strip newlines from (e.g. /ng-class/)', parseRegExp],
|
|
109
|
+
customAttrSurround: ['Arrays of regex\'es that allow to support custom attribute surround expressions (e.g. <input {{#if value}}checked="checked"{{/if}}>)', parseJSONRegExpArray],
|
|
110
|
+
customEventAttributes: ['Arrays of regex\'es that allow to support custom event attributes for minifyJS (e.g. ng-click)', parseJSONRegExpArray],
|
|
111
|
+
decodeEntities: 'Use direct Unicode characters whenever possible',
|
|
112
|
+
html5: 'Parse input according to HTML5 specifications',
|
|
113
|
+
ignoreCustomComments: ['Array of regex\'es that allow to ignore certain comments, when matched', parseJSONRegExpArray],
|
|
114
|
+
ignoreCustomFragments: ['Array of regex\'es that allow to ignore certain fragments, when matched (e.g. <?php ... ?>, {{ ... }})', parseJSONRegExpArray],
|
|
115
|
+
includeAutoGeneratedTags: 'Insert tags generated by HTML parser',
|
|
116
|
+
keepClosingSlash: 'Keep the trailing slash on singleton elements',
|
|
117
|
+
lint: 'Toggle linting',
|
|
118
|
+
maxLineLength: ['Max line length', parseInt],
|
|
119
|
+
minifyCSS: ['Minify CSS in style elements and style attributes (uses clean-css)', parseJSON],
|
|
120
|
+
minifyJS: ['Minify Javascript in script elements and on* attributes (uses uglify-js)', parseJSON],
|
|
121
|
+
minifyURLs: ['Minify URLs in various attributes (uses relateurl)', parseSiteURL],
|
|
122
|
+
preserveLineBreaks: 'Always collapse to 1 line break (never remove it entirely) when whitespace between tags include a line break.',
|
|
123
|
+
preventAttributesEscaping: 'Prevents the escaping of the values of attributes.',
|
|
124
|
+
processConditionalComments: 'Process contents of conditional comments through minifier',
|
|
125
|
+
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],
|
|
126
|
+
quoteCharacter: ['Type of quote to use for attribute values (\' or ")', parseString],
|
|
127
|
+
removeAttributeQuotes: 'Remove quotes around attributes when possible.',
|
|
128
|
+
removeComments: 'Strip HTML comments',
|
|
129
|
+
removeEmptyAttributes: 'Remove all attributes with whitespace-only values',
|
|
130
|
+
removeEmptyElements: 'Remove all elements with empty contents',
|
|
131
|
+
removeOptionalTags: 'Remove unrequired tags',
|
|
132
|
+
removeRedundantAttributes: 'Remove attributes when value matches default.',
|
|
133
|
+
removeScriptTypeAttributes: 'Remove type="text/javascript" from script tags. Other type attribute values are left intact.',
|
|
134
|
+
removeStyleLinkTypeAttributes: 'Remove type="text/css" from style and link tags. Other type attribute values are left intact.',
|
|
135
|
+
removeTagWhitespace: 'Remove space between attributes whenever possible',
|
|
136
|
+
sortAttributes: 'Sort attributes by frequency',
|
|
137
|
+
sortClassName: 'Sort style classes by frequency',
|
|
138
|
+
useShortDoctype: 'Replaces the doctype with the short (HTML5) doctype'
|
|
139
|
+
};
|
|
140
|
+
var mainOptionKeys = Object.keys(mainOptions);
|
|
141
|
+
mainOptionKeys.forEach(function(key) {
|
|
142
|
+
var option = mainOptions[key];
|
|
143
|
+
key = '--' + changeCase.paramCase(key);
|
|
144
|
+
if (Array.isArray(option)) {
|
|
145
|
+
var optional = option[1] === parseJSON;
|
|
146
|
+
program.option(key + (optional ? ' [value]' : ' <value>'), option[0], option[1]);
|
|
221
147
|
}
|
|
148
|
+
else {
|
|
149
|
+
program.option(key, option);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
program.option('-o --output <file>', 'Specify output file (if not specified STDOUT will be used for output)', function(outputPath) {
|
|
153
|
+
return fs.createWriteStream(outputPath).on('error', function(e) {
|
|
154
|
+
fatal('Cannot write ' + outputPath + '\n' + e.message);
|
|
155
|
+
});
|
|
156
|
+
}, process.stdout);
|
|
222
157
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
158
|
+
function readFile(file) {
|
|
159
|
+
try {
|
|
160
|
+
return fs.readFileSync(file, { encoding: 'utf8' });
|
|
226
161
|
}
|
|
162
|
+
catch (e) {
|
|
163
|
+
fatal('Cannot read ' + file + '\n' + e.message);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
227
166
|
|
|
228
|
-
|
|
229
|
-
|
|
167
|
+
var config = {};
|
|
168
|
+
program.option('-c --config-file <file>', 'Use config file', function(configPath) {
|
|
169
|
+
var data = readFile(configPath);
|
|
170
|
+
try {
|
|
171
|
+
config = JSON.parse(data);
|
|
172
|
+
}
|
|
173
|
+
catch (je) {
|
|
230
174
|
try {
|
|
231
|
-
|
|
232
|
-
try {
|
|
233
|
-
configData = fs.readFileSync(configPath, { encoding: 'utf8' });
|
|
234
|
-
}
|
|
235
|
-
catch (e) {
|
|
236
|
-
cli.fatal('The specified config file doesn’t exist or is unreadable:\n' + configPath);
|
|
237
|
-
}
|
|
238
|
-
configData = JSON.parse(configData);
|
|
239
|
-
mainOptionKeys.forEach(function(key) {
|
|
240
|
-
var value = configData[key];
|
|
241
|
-
if (value !== undefined) {
|
|
242
|
-
switch (mainOptions[key][1]) {
|
|
243
|
-
case 'json-regex':
|
|
244
|
-
minifyOptions[key] = value.map(stringToRegExp);
|
|
245
|
-
break;
|
|
246
|
-
case 'string-regex':
|
|
247
|
-
minifyOptions[key] = stringToRegExp(value);
|
|
248
|
-
break;
|
|
249
|
-
default:
|
|
250
|
-
minifyOptions[key] = value;
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
});
|
|
175
|
+
config = require(path.resolve(configPath));
|
|
254
176
|
}
|
|
255
|
-
catch (
|
|
256
|
-
|
|
257
|
-
minifyOptions = require(configPath);
|
|
258
|
-
}
|
|
259
|
-
catch (ne) {
|
|
260
|
-
cli.fatal('Cannot read the specified config file.\nAs JSON: ' + je.message + '\nAs module: ' + ne.message);
|
|
261
|
-
}
|
|
177
|
+
catch (ne) {
|
|
178
|
+
fatal('Cannot read the specified config file.\nAs JSON: ' + je.message + '\nAs module: ' + ne.message);
|
|
262
179
|
}
|
|
263
180
|
}
|
|
264
181
|
mainOptionKeys.forEach(function(key) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
minifyOptions[key] = parseJSONOption(value);
|
|
271
|
-
break;
|
|
272
|
-
case 'json-regex':
|
|
273
|
-
minifyOptions[key] = parseJSONOption(value, true);
|
|
274
|
-
break;
|
|
275
|
-
case 'string-regex':
|
|
276
|
-
minifyOptions[key] = stringToRegExp(value);
|
|
277
|
-
break;
|
|
278
|
-
case 'site-url':
|
|
279
|
-
minifyOptions[key] = { site: value };
|
|
280
|
-
break;
|
|
281
|
-
case true:
|
|
282
|
-
minifyOptions[key] = value;
|
|
283
|
-
break;
|
|
284
|
-
default:
|
|
285
|
-
minifyOptions[key] = true;
|
|
182
|
+
if (key in config) {
|
|
183
|
+
var option = mainOptions[key];
|
|
184
|
+
if (Array.isArray(option)) {
|
|
185
|
+
var value = config[key];
|
|
186
|
+
config[key] = option[1](typeof value === 'string' ? value : JSON.stringify(value));
|
|
286
187
|
}
|
|
287
188
|
}
|
|
288
189
|
});
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
try {
|
|
304
|
-
fs.statSync(inputDir);
|
|
305
|
-
}
|
|
306
|
-
catch (e) {
|
|
307
|
-
cli.error('The input directory does not exist');
|
|
308
|
-
cli.exit(2);
|
|
190
|
+
});
|
|
191
|
+
program.option('--input-dir <dir>', 'Specify an input directory');
|
|
192
|
+
program.option('--output-dir <dir>', 'Specify an output directory');
|
|
193
|
+
var content;
|
|
194
|
+
program.arguments('[files...]').action(function(files) {
|
|
195
|
+
content = files.map(readFile).join('');
|
|
196
|
+
}).parse(process.argv);
|
|
197
|
+
|
|
198
|
+
function createOptions() {
|
|
199
|
+
var options = {};
|
|
200
|
+
mainOptionKeys.forEach(function(key) {
|
|
201
|
+
var param = program[changeCase.camelCase(key)];
|
|
202
|
+
if (typeof param !== 'undefined') {
|
|
203
|
+
options[key] = param;
|
|
309
204
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
if (!outputDir) {
|
|
313
|
-
cli.error('You need to specify where to write the output files with the option --output-dir');
|
|
314
|
-
cli.exit(2);
|
|
205
|
+
else if (key in config) {
|
|
206
|
+
options[key] = config[key];
|
|
315
207
|
}
|
|
208
|
+
});
|
|
209
|
+
if (options.lint) {
|
|
210
|
+
options.lint = new HTMLLint();
|
|
211
|
+
}
|
|
212
|
+
return options;
|
|
213
|
+
}
|
|
316
214
|
|
|
317
|
-
|
|
318
|
-
|
|
215
|
+
function processDirectory(inputDir, outputDir) {
|
|
216
|
+
fs.readdir(inputDir, function(err, files) {
|
|
217
|
+
if (err) {
|
|
218
|
+
fatal('Cannot read directory ' + inputDir + '\n' + err.message);
|
|
319
219
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
220
|
+
fs.mkdir(outputDir, function(err) {
|
|
221
|
+
if (err && err.code !== 'EEXIST') {
|
|
222
|
+
fatal('Cannot create directory ' + outputDir + '\n' + err.message);
|
|
223
|
+
}
|
|
224
|
+
files.forEach(function(file) {
|
|
225
|
+
var inputFile = path.join(inputDir, file);
|
|
226
|
+
var outputFile = path.join(outputDir, file);
|
|
227
|
+
fs.readFile(inputFile, { encoding: 'utf8' }, function(err, data) {
|
|
228
|
+
if (!err) {
|
|
229
|
+
var minified;
|
|
230
|
+
try {
|
|
231
|
+
minified = minify(data, createOptions());
|
|
232
|
+
}
|
|
233
|
+
catch (e) {
|
|
234
|
+
fatal('Minification error on ' + inputFile + '\n' + e.message);
|
|
235
|
+
}
|
|
236
|
+
fs.writeFile(outputFile, minified, { encoding: 'utf8' }, function(err) {
|
|
237
|
+
if (err) {
|
|
238
|
+
fatal('Cannot write ' + outputFile + '\n' + err.message);
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
else if (err.code === 'EISDIR') {
|
|
243
|
+
processDirectory(inputFile, outputFile);
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
fatal('Cannot read ' + inputFile + '\n' + err.message);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function writeMinify(content) {
|
|
255
|
+
var minified;
|
|
256
|
+
try {
|
|
257
|
+
var options = createOptions();
|
|
258
|
+
minified = minify(content, options);
|
|
259
|
+
if (options.lint) {
|
|
260
|
+
options.lint.populate();
|
|
324
261
|
}
|
|
325
262
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
output = options.output;
|
|
263
|
+
catch (e) {
|
|
264
|
+
fatal('Minification error:\n' + e.message);
|
|
329
265
|
}
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
catch (e) {
|
|
339
|
-
cli.error('Cannot read file ' + afile);
|
|
340
|
-
cli.exit(2);
|
|
341
|
-
}
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
cli.exit(runMinify(original, output));
|
|
266
|
+
program.output.write(minified);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
var inputDir = program.inputDir;
|
|
270
|
+
var outputDir = program.outputDir;
|
|
271
|
+
if (inputDir || outputDir) {
|
|
272
|
+
if (!inputDir) {
|
|
273
|
+
fatal('The option output-dir needs to be used with the option input-dir. If you are working with a single file, use -o.');
|
|
345
274
|
}
|
|
346
|
-
else
|
|
347
|
-
|
|
348
|
-
cli.exit(runMinify(content, output));
|
|
349
|
-
}));
|
|
275
|
+
else if (!outputDir) {
|
|
276
|
+
fatal('You need to specify where to write the output files with the option --output-dir');
|
|
350
277
|
}
|
|
351
|
-
|
|
278
|
+
processDirectory(inputDir, outputDir);
|
|
279
|
+
}
|
|
280
|
+
// Minifying one or more files specified on the CMD line
|
|
281
|
+
else if (typeof content === 'string') {
|
|
282
|
+
writeMinify(content);
|
|
283
|
+
}
|
|
284
|
+
// Minifying input coming from STDIN
|
|
285
|
+
else {
|
|
286
|
+
process.stdin.pipe(concat({ encoding: 'string' }, writeMinify));
|
|
287
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "html-minifier",
|
|
3
3
|
"description": "HTML minifier with lint-like capabilities.",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.5.0",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"html",
|
|
7
7
|
"minifier",
|
|
@@ -39,8 +39,9 @@
|
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"change-case": "2.3.x",
|
|
41
41
|
"clean-css": "3.4.x",
|
|
42
|
-
"
|
|
42
|
+
"commander": "2.9.x",
|
|
43
43
|
"concat-stream": "1.5.x",
|
|
44
|
+
"he": "1.0.x",
|
|
44
45
|
"ncname": "1.0.x",
|
|
45
46
|
"relateurl": "0.2.x",
|
|
46
47
|
"uglify-js": "2.6.x"
|
|
@@ -49,14 +50,11 @@
|
|
|
49
50
|
"brotli": "1.2.x",
|
|
50
51
|
"chalk": "1.1.x",
|
|
51
52
|
"cli-table": "0.3.x",
|
|
52
|
-
"grunt": "0.
|
|
53
|
+
"grunt": "1.0.x",
|
|
53
54
|
"grunt-browserify": "5.0.x",
|
|
54
|
-
"grunt-cli": "1.1.x",
|
|
55
55
|
"grunt-contrib-uglify": "1.0.x",
|
|
56
56
|
"grunt-eslint": "18.0.x",
|
|
57
|
-
"grunt-exec": "0.4.x",
|
|
58
57
|
"grunt-lib-phantomjs": "1.0.x",
|
|
59
|
-
"load-grunt-tasks": "3.4.x",
|
|
60
58
|
"lzma": "2.3.x",
|
|
61
59
|
"minimize": "1.8.x",
|
|
62
60
|
"progress": "1.1.x",
|
|
@@ -64,11 +62,9 @@
|
|
|
64
62
|
"time-grunt": "1.3.x"
|
|
65
63
|
},
|
|
66
64
|
"files": [
|
|
67
|
-
"dist",
|
|
68
65
|
"src",
|
|
69
66
|
"cli.js",
|
|
70
67
|
"sample-cli-config-file.conf",
|
|
71
|
-
"CHANGELOG.md",
|
|
72
68
|
"LICENSE"
|
|
73
69
|
]
|
|
74
70
|
}
|
|
@@ -1,28 +1,37 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
2
|
+
"caseSensitive": false,
|
|
3
|
+
"collapseBooleanAttributes": true,
|
|
4
|
+
"collapseInlineTagWhitespace": false,
|
|
3
5
|
"collapseWhitespace": true,
|
|
4
6
|
"conservativeCollapse": false,
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"removeRedundantAttributes": true,
|
|
9
|
-
"useShortDoctype": true,
|
|
10
|
-
"removeEmptyAttributes": true,
|
|
11
|
-
"removeScriptTypeAttributes": true,
|
|
12
|
-
"removeStyleLinkTypeAttributes": true,
|
|
13
|
-
"removeOptionalTags": true,
|
|
14
|
-
"removeEmptyElements": true,
|
|
15
|
-
"lint": false,
|
|
16
|
-
"keepClosingSlash": false,
|
|
17
|
-
"caseSensitive": false,
|
|
18
|
-
"minifyJS": true,
|
|
19
|
-
"minifyCSS": true,
|
|
20
|
-
"includeAutoGeneratedTags": false,
|
|
7
|
+
"customAttrCollapse": ".*",
|
|
8
|
+
"decodeEntities": true,
|
|
9
|
+
"html5": true,
|
|
21
10
|
"ignoreCustomFragments": [
|
|
22
11
|
"<#[\\s\\S]*?#>"
|
|
23
12
|
],
|
|
13
|
+
"includeAutoGeneratedTags": false,
|
|
14
|
+
"keepClosingSlash": false,
|
|
15
|
+
"lint": false,
|
|
16
|
+
"maxLineLength": 0,
|
|
17
|
+
"minifyCSS": true,
|
|
18
|
+
"minifyJS": true,
|
|
19
|
+
"preserveLineBreaks": false,
|
|
20
|
+
"preventAttributesEscaping": false,
|
|
24
21
|
"processConditionalComments": true,
|
|
25
22
|
"processScripts": [
|
|
26
23
|
"text/html"
|
|
27
|
-
]
|
|
24
|
+
],
|
|
25
|
+
"removeAttributeQuotes": true,
|
|
26
|
+
"removeComments": true,
|
|
27
|
+
"removeEmptyAttributes": true,
|
|
28
|
+
"removeEmptyElements": true,
|
|
29
|
+
"removeOptionalTags": true,
|
|
30
|
+
"removeRedundantAttributes": true,
|
|
31
|
+
"removeScriptTypeAttributes": true,
|
|
32
|
+
"removeStyleLinkTypeAttributes": true,
|
|
33
|
+
"removeTagWhitespace": true,
|
|
34
|
+
"sortAttributes": true,
|
|
35
|
+
"sortClassName": true,
|
|
36
|
+
"useShortDoctype": true
|
|
28
37
|
}
|