Repository navigation
Expand file tree
/
Copy pathstringify.js
More file actions
174 lines (152 loc) · 4.64 KB
/
Copy pathstringify.js
File metadata and controls
174 lines (152 loc) · 4.64 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
'use strict';
var htmlMinifier = require('html-minifier'),
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
};
/**
* 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 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 = module.exports.DEFAULT_EXTENSIONS;
if (options) {
if (Object.prototype.toString.call(options) === '[object Array]') {
extensions = options;
} 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 || module.exports.DEFAULT_MINIFIER_EXTENSIONS,
options: minifier.options || module.exports.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 = module.exports.getMinifierOptions(options);
if (minifier.requested && module.exports.hasStringifiableExtension(filename, minifier.extensions)) {
return htmlMinifier.minify(contents, minifier.options);
}
return contents;
}
/**
* Creates the Browserify transform function which Browserify will pass the
* file to.
* @param {object | array} options
* @param {object} options.extensions
* @returns {stream}
*/
module.exports = function (options) {
var extensions = module.exports.getExtensions(options);
/**
* The function Browserify will use to transform the input.
* @param {string} file
* @returns {stream}
*/
function browserifyTransform (file) {
if (!module.exports.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(module.exports.stringify(module.exports.minify(file, contents, options)));
this.queue(null);
};
return through(write, end);
}
return browserifyTransform;
};
// Allow specific exports to be overriden if wanted...
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;