-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadd-transforms.js
194 lines (171 loc) · 5.92 KB
/
add-transforms.js
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
'use strict';
const fs = require('fs');
const path = require('path');
const ono = require('ono');
const uglify = require('uglify-js');
const uglifyify = require('uglifyify');
const istanbul = require('browserify-istanbul');
module.exports = addTransforms;
/**
* Adds Browserify transforms.
*
* @param {Browserify} bundler - The Browserify or Watchify instance
* @param {object} manifest - The project manifest (package.json file)
* @param {object} [options]
* @param {boolean} [options.minify] - Whether to add the uglifyify transform
* @param {boolean} [options.test] - Whether to add the istanbul transform
*/
function addTransforms (bundler, manifest, options) {
try {
options = options || {};
manifest.browserify = manifest.browserify || {};
manifest.browserify.transform = manifest.browserify.transform || [];
// Add any user-defined transforms first
addTransformsFromManifest(bundler, manifest);
// Then minify
if (options.minify) {
addUglifyifyTransform(bundler, manifest);
}
// Add code-coverage instrumentation last, so it's not mangled by uglify
if (options.test) {
addIstanbulTransform(bundler, manifest);
}
}
catch (e) {
throw ono(e, 'Error adding Browserify transforms');
}
}
/**
* Adds any Browserify transforms that are specified in the project manifest.
*
* @param {Browserify} bundler - The Browserify or Watchify instance
* @param {object} manifest - The project manifest (package.json file)
*/
function addTransformsFromManifest (bundler, manifest) {
manifest.browserify.transform.forEach(transform => {
// Each transform can be a string (just the transform name)
// or an array (the transform name and its config)
let name, config;
if (Array.isArray(transform)) {
name = transform[0];
config = transform[1];
}
else {
name = transform;
config = undefined;
}
// Add the transform, unless it's one of the ones that we handle separately
if (name !== 'uglifyify' && name !== 'istanbul') {
bundler.transform(require(name), config);
}
});
}
/**
* Adds the Istanbul transform, which adds code-coverage instrumentation.
*
* @param {Browserify} bundler - The Browserify or Watchify instance
* @param {object} manifest - The project manifest (package.json file)
*/
function addIstanbulTransform (bundler, manifest) {
let options = getTransformOptions('istanbul', manifest) || {
// Replace Istanbul's default "ignore" list with our own
defaultIgnore: false,
ignore: [
'**/node_modules/**', '**/bower_components/**',
'**/*.json', '**/*.html', '**/*.md', '**/*.txt'
],
};
bundler.transform(istanbul(options));
}
/**
* Adds the Uglifyify transform to minify the bundle.
*
* Minification is done in two phases, both using UglifyJS:
*
* PHASE 1 - The first phase occurs as a Browserify transform, which minifies each module
* individually. This allows Uglify to eliminate dead code paths within each module.
*
* PHASE 2 - The second phase occurs after Browserify is finished, and minifies the entire
* bundle file. This allows Uglify to achieve the smallest possible file size.
*
* @param {Browserify} bundler - The Browserify or Watchify instance
* @param {object} manifest - The project manifest (package.json file)
*/
function addUglifyifyTransform (bundler, manifest) {
let options = getTransformOptions('uglifyify', manifest) || {};
if (options.global === undefined) {
// Apply uglifyify to ALL modules in the bundle, even third-party ones
options.global = true;
}
if (options.exts === undefined) {
// Uglify can minify JavaScript and JSON files
options.exts = ['.js', '.json'];
}
if (options.output === undefined) {
options.output = {
// Keep important comments when minifying
comments: /^!|^\*!|@preserve|@license|@cc_on/,
};
}
// PHASE 1 - Minify each module individually
bundler.transform(uglifyify, options);
// Change the options a bit for PHASE 2
let postProcessingOptions = cloneObj(options);
let outputFile = path.resolve(bundler.files.outputFile);
let mapFile;
// Don't mangle again, since that was already done in PHASE 1
postProcessingOptions.mangle = false;
postProcessingOptions.mangleProperties = false;
// If the bundle has a sourcemap, then UglifyJS needs to modify it
if (bundler.files.mapFile) {
mapFile = path.resolve(bundler.files.mapFile);
postProcessingOptions.inSourceMap = mapFile;
postProcessingOptions.outSourceMap = mapFile;
postProcessingOptions.sourceMapUrl = path.basename(mapFile);
}
// PHASE 2 - Minify the entire bundle file
bundler.postProcessing = function () {
let minified = uglify.minify(outputFile, postProcessingOptions);
// Overwrite the file(s) from PHASE 1
fs.writeFileSync(outputFile, minified.code);
if (mapFile) {
// Replace the absolute path with the relative path
let map = JSON.parse(minified.map);
map.file = path.basename(outputFile);
fs.writeFileSync(mapFile, JSON.stringify(map));
}
};
}
/**
* Returns the specified transform's options from the manifest file, if any.
*
* @param {string} name - The name of the transform whose options are returned
* @param {object} manifest - The project manifest (package.json file), to search for options
* @returns {object|undefined}
*/
function getTransformOptions (name, manifest) {
let options;
manifest.browserify.transform.some(transform => {
if (Array.isArray(transform) && transform[0] === name) {
options = cloneObj(transform[1]);
}
});
return options;
}
/**
* A simple recursive cloning function for JSON data
*
* @param {object|array} obj
* @returns {object|array}
*/
function cloneObj (obj) {
let clone = Array.isArray(obj) ? [] : {};
Object.keys(obj).forEach(key => {
let value = obj[key];
if (value && typeof value === 'object') {
value = cloneObj(value);
}
clone[key] = value;
});
return clone;
}