Repository navigation
Expand file tree
/
Copy pathstringify.js
More file actions
235 lines (204 loc) · 6.3 KB
/
Copy pathstringify.js
File metadata and controls
235 lines (204 loc) · 6.3 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
'use strict';
var htmlMinifier = require('html-minifier'),
fs = require('fs'),
path = require('path'),
through = require('through');
var DEFAULT_MINIFIER_EXTENSIONS = [
'.html',
'.htm',
'.tmpl',
'.tpl',
'.hbs'
];
var DEFAULT_EXTENSIONS = DEFAULT_MINIFIER_EXTENSIONS.concat([
'.text',
'.txt'
]);
var DEFAULT_MINIFIER_OPTIONS = {
removeComments: true,
removeCommentsFromCDATA: true,
removeCDATASectionsFromCDATA: true,
collapseWhitespace: true,
conservativeCollapse: false,
preserveLineBreaks: false,
collapseBooleanAttributes: false,
removeAttributeQuotes: true,
removeRedundantAttributes: false,
useShortDoctype: false,
removeEmptyAttributes: false,
removeScriptTypeAttributes: false,
removeStyleLinkTypeAttributes: false,
removeOptionalTags: false,
removeIgnored: false,
removeEmptyElements: false,
lint: false,
keepClosingSlash: false,
caseSensitive: false,
minifyJS: false,
minifyCSS: false,
minifyURLs: false
};
var NODE_REQUIRE_OPTIONS = {};
/**
* Stringifies the content
* @param {string} content
* @returns {string}
*/
function stringify (content) {
return content;
}
/**
* Takes a set of user-supplied options, and determines which set of file-
* extensions to run Stringify on.
* @param {object | array} options
* @param {object} options.extensions
* @returns {string[]}
*/
function getExtensions (options) {
/**
* The file extensions which are stringified by default.
* @type {string[]}
*/
var extensions = DEFAULT_EXTENSIONS;
if (options) {
if (Object.prototype.toString.call(options) === '[object Array]') {
extensions = options;
} else if (options.extensions && options.extensions._) {
extensions = options.extensions._;
} else if(options.extensions) {
extensions = options.extensions
}
}
// Lowercase all file extensions for case-insensitive matching.
extensions = extensions.map(function (ext) {
return ext.toLowerCase();
});
return extensions;
}
/**
* Provides user or default options for html-minifier module
* @param {object} options
* @param {object} options.minifier
* @returns {object}
*/
function getMinifierOptions (_options) {
var options = _options || {};
var minifier = options.minifier || {};
return {
requested : !!options.minify,
extensions: minifier.extensions || DEFAULT_MINIFIER_EXTENSIONS,
options: minifier.options || DEFAULT_MINIFIER_OPTIONS
};
}
/**
* Returns whether the filename ends in a Stringifiable extension. Case
* insensitive.
* @param {string} filename
* @return {boolean}
*/
function hasStringifiableExtension (filename, extensions) {
var file_extension = path.extname(filename).toLowerCase();
return extensions.indexOf(file_extension) > -1;
}
/**
* Returns minified contents if requested
* @param {string} filename
* @param {string} contents
* @param {object} options
* @return {string}
*/
function minify(filename, contents, options) {
var minifier = getMinifierOptions(options);
if (minifier.requested && hasStringifiableExtension(filename, minifier.extensions)) {
return htmlMinifier.minify(contents, minifier.options);
}
return contents;
}
/**
* Reads in a file and stringifies and minifies the contents.
* @param {String} module
* @param {String} filename
* @return {String}
*/
function requireStringify (module, filename) {
var contents;
try {
contents = fs.readFileSync(path.resolve(filename), 'utf8');
} catch (error) {
throw new Error('Cannot find module \'' + filename + '\'');
}
module.exports = minify(filename, contents, NODE_REQUIRE_OPTIONS);
}
/**
* Registers the given extensions with node require.
* @param {Object|Array} options
* @param {Object.Array} options.extensions
* @return {void}
*/
function registerWithRequire (options) {
NODE_REQUIRE_OPTIONS = options || {};
var exts = getExtensions(NODE_REQUIRE_OPTIONS);
for (var i = 0; i < exts.length; i++) {
require.extensions[ exts[i] ] = requireStringify;
}
}
/**
* Exposes the Browserify transform function.
*
* This handles two use cases:
* - Factory: given no arguments or options as first argument it returns
* the transform function
* - Standard: given file (and optionally options) as arguments a stream is
* returned. This follows the standard pattern for browserify transformers.
*
* @param {string} file
* @param {object | array} options
* @returns {stream | function} depending on if first argument is string.
*/
module.exports = function (file, options) {
/**
* The function Browserify will use to transform the input.
* @param {string} file
* @returns {stream}
*/
function browserifyTransform (file) {
var extensions = getExtensions(options);
if (!hasStringifiableExtension(file, extensions)) {
return through();
}
var chunks = [];
var write = function (buffer) {
chunks.push(buffer);
};
var end = function () {
var contents = Buffer.concat(chunks).toString('utf8');
this.queue(stringify(minify(file, contents, options)));
this.queue(null);
};
return through(write, end);
}
if (typeof file !== 'string') {
// Factory: return a function.
// Set options variable here so it is ready for when browserifyTransform
// is called. Note: first argument is the options.
options = file;
return browserifyTransform;
} else {
return browserifyTransform(file);
}
};
// exports registerWithRequire so stringify can be registered with node require.
module.exports.registerWithRequire = registerWithRequire;
// Test-environment specific exports...
if (process.env.NODE_ENV) {
module.exports.NODE_REQUIRE_OPTIONS = NODE_REQUIRE_OPTIONS;
module.exports.requireStringify = requireStringify;
module.exports.stringify = stringify;
module.exports.getExtensions = getExtensions;
module.exports.DEFAULT_EXTENSIONS = DEFAULT_EXTENSIONS;
module.exports.hasStringifiableExtension = hasStringifiableExtension;
module.exports.minify = minify;
module.exports.getMinifierOptions = getMinifierOptions;
module.exports.DEFAULT_MINIFIER_EXTENSIONS = DEFAULT_MINIFIER_EXTENSIONS;
module.exports.DEFAULT_MINIFIER_OPTIONS = DEFAULT_MINIFIER_OPTIONS;
}