Repository navigation
Expand file tree
/
Copy pathstringify.js
More file actions
254 lines (221 loc) · 7.45 KB
/
Copy pathstringify.js
File metadata and controls
254 lines (221 loc) · 7.45 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
'use strict';
var htmlMinifier = require('html-minifier'),
fs = require('fs'),
path = require('path'),
tools = require('browserify-transform-tools');
var MINIFY_TRANSFORM_OPTIONS = {
includeExtensions: [
'.html',
'.htm',
'.tmpl',
'.tpl',
'.hbs'
]
};
var TRANSFORM_OPTIONS = {
includeExtensions: MINIFY_TRANSFORM_OPTIONS.includeExtensions.concat([
'.text',
'.txt'
])
};
var DEFAULT_MINIFY_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 'module.exports = ' + JSON.stringify(content) + ';\n';
}
/**
* Takes a set of user-supplied options, and ensure file configuration
* settings is in the correct form for 'browserify-transform-tools'.
* @param {object | array} options
* @returns {object}
*/
function getTransformOptions (options) {
if (!options) {
return {};
}
if (Object.prototype.toString.call(options) === '[object Array]') {
options = { appliesTo: { includeExtensions: options } };
}
if (options.extensions && !options.appliesTo) {
var extensions = options.extensions._ || options.extensions;
options.appliesTo = { includeExtensions: extensions };
delete options.extensions;
}
return options;
}
/**
* 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 getRequireExtensions (options) {
var extensions = TRANSFORM_OPTIONS.includeExtensions;
if (options && options.appliesTo && options.appliesTo.includeExtensions) {
extensions = options.appliesTo.includeExtensions;
}
// 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
* @returns {object}
*/
function getMinifyOptions (options) {
if (!options || !options.minify) {
return { requested: false };
}
var minifierOpts = options.minifier,
minify = { requested: true, options: DEFAULT_MINIFY_OPTIONS };
if (options.minifyAppliesTo) {
minify.config = { appliesTo: options.minifyAppliesTo };
} else if (minifierOpts && minifierOpts.extensions) {
var extensions = minifierOpts.extensions._ || options.minifier.extensions;
minify.config = { appliesTo: { includeExtensions: extensions } };
}
if (options.minifyOptions) {
minify.options = options.minifyOptions;
} else if (minifierOpts && minifierOpts.options) {
minify.options = minifierOpts.options;
}
return minify;
}
/**
* Returns minified contents if requested
* @param {string} filename
* @param {string} contents
* @param {object} options
* @return {string}
*/
function minify(filename, contents, options) {
var minifier = getMinifyOptions(options);
if (minifier.requested) {
if (!tools.skipFile(filename, minifier.config, MINIFY_TRANSFORM_OPTIONS)) {
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('Stringify could not find module \'' + path.resolve(filename) + '\'.');
}
module.exports = minify(filename, contents, NODE_REQUIRE_OPTIONS);
}
/**
* Registers the given extensions with node require.
* @param {object | array} options
* @return {void}
*/
function registerWithRequire (options) {
NODE_REQUIRE_OPTIONS = getTransformOptions(options);
var exts = getRequireExtensions(NODE_REQUIRE_OPTIONS);
for (var i = 0; i < exts.length; i++) {
require.extensions[ exts[i] ] = requireStringify;
}
}
/**
* Function which is called to do the transform.
*
* - `contents` are the contents of the file.
* - `transformOptions.file` is the name of the file (as would be
* passed to a normal browserify transform.)
* - `transformOptions.config` is the configuration data that has been
* automatically loaded. For details, see the transform configuration documentation
* (https://github.com/benbria/browserify-transform-tools/wiki/Transform-Configuration).
* - `transformOptions.config` is a copy of
* - `done(err, transformed)` is a callback which must be called, passing a
* string with the transformed contents of the file.
*
* @param {string} content
* @param {object} transformOptions
* @param {function} done
* @returns {void}
*/
function transformFn (contents, transformOptions, done) {
var file = transformOptions.file,
options = transformOptions.config;
done(null, stringify(minify(file, contents, options)));
}
/**
* 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) {
var transform = tools.makeStringTransform('stringify', TRANSFORM_OPTIONS, transformFn);
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.
var capturedOptions = getTransformOptions(file);
return function (file) { return transform(file, capturedOptions); };
} else {
return transform(file, getTransformOptions(options));
}
};
// 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.getRequireExtensions = getRequireExtensions;
module.exports.getTransformOptions = getTransformOptions;
module.exports.TRANSFORM_OPTIONS = TRANSFORM_OPTIONS;
module.exports.minify = minify;
module.exports.getMinifyOptions = getMinifyOptions;
module.exports.MINIFY_TRANSFORM_OPTIONS = MINIFY_TRANSFORM_OPTIONS;
module.exports.DEFAULT_MINIFY_OPTIONS = DEFAULT_MINIFY_OPTIONS;
}