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