-
Notifications
You must be signed in to change notification settings - Fork 42
/
buildProduction
executable file
·437 lines (413 loc) · 18 KB
/
buildProduction
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env node
/*eslint indent:0*/
var chalk = require('chalk'),
parseExpression = require('assetgraph/lib/parseExpression'),
optimist = require('optimist')
.usage('$0 --root <inputRootDirectory> --outroot <dir> [options] <htmlFile(s)>')
.wrap(72)
.options('h', {
alias: 'help',
describe: 'Show this help',
type: 'boolean',
default: false
})
.options('plugin', {
alias: 'p',
describe: 'Install a plugin. Must be a require-able, eg. assetgraph-i18n. Can be repeated.',
type: 'string',
default: false
})
.options('root', {
describe: 'Path to your web root (will be deduced from your input files if not specified)',
type: 'string',
demand: false
})
.options('outroot', {
describe: 'Path to the output folder. Will be generated if non-existing',
type: 'string',
demand: true
})
.options('canonicalroot', {
describe: 'URI root where the site will be deployed. Must be either an absolute or a protocol-relative url',
type: 'string',
demand: false
})
.options('cdnroot', {
describe: 'URI root where the static assets will be deployed. Must be either an absolute or a protocol-relative url',
type: 'string',
demand: false
})
.options('sourcemaps', {
describe: 'Whether to include source maps',
type: 'boolean',
default: false
})
.options('contentsecuritypolicy', {
describe: 'Whether to update existing Content-Security-Policy meta tags',
type: 'boolean',
default: false
})
.options('subresourceintegrity', {
describe: 'Whether to add integrity=... attributes to stylesheets and scripts that are part of the build',
type: 'boolean',
default: false
})
.options('sourcescontent', {
describe: 'Whether to include the source contents in the source maps (requires --sourcemaps), only works for CSS (not yet JavaScript)',
type: 'boolean',
default: false
})
.options('optimizeimages', {
describe: 'Perform automatic lossless optimization of all images using pngcrush, pngquant, optipng, and jpegtran',
type: 'boolean',
default: false
})
.options('browsers', {
alias: 'b',
describe: 'Specify which browsers to support. Configures autoprefixer and controls which IE hacks to apply. Syntax: https://github.com/ai/browserslist',
type: 'string',
demand: false
})
.options('debug', {
describe: 'Keep statement level console.*() calls and debugger statements in JavaScript assets',
type: 'boolean',
default: false
})
.options('version', {
describe: 'Adds or updates <html data-version="..."> to the specified value. Use {0} to refer to the current value, eg. --version {0}/production or --version `git describe --long --tags --always --dirty 2>/dev/null || echo unknown`',
type: 'string'
})
.options('gzip', {
describe: 'Include a gzipped copy of text-based assets > 860 bytes for which it yields a saving',
default: false
})
.options('deferscripts', {
describe: 'Sets the "defer" attribute on all script tags',
type: 'boolean',
default: false
})
.options('asyncscripts', {
describe: 'Sets the "async" attribute on all script tags',
type: 'boolean',
default: false
})
.options('reservednames', {
describe: 'Exclude certain variable names from mangling (equivalent to uglifyjs --reserved-names ...)'
})
.options('stoponwarning', {
describe: 'Whether to stop with a non-zero exit code when a warning is encountered',
type: 'boolean',
default: false
})
.options('recursive', {
describe: 'Follow local HTML anchors when populating the graph (use --no-recursive to turn off this behavior)',
type: 'boolean',
default: true
})
.options('minify', {
describe: 'Minifies HTML, CSS and Javascript.',
type: 'boolean',
default: true
})
.options('svgo', {
describe: 'Minify SVG with svgo (requires the svgo module to be installed in your app)',
type: 'boolean',
default: true
})
.options('nocompress', {
describe: 'Disables JavaScript compression via UglifyJS',
alias: 'no-compress',
type: 'boolean',
default: false
})
.options('pretty', {
describe: 'Whether to pretty-print JavaScript, CSS and HTML assets',
type: 'boolean',
default: false
})
.options('space', {
describe: 'The space character to use when serializing JavaScript',
type: 'string',
default: ' '
})
.options('newline', {
describe: 'The newline character to use when serializing JavaScript',
type: 'string',
default: ''
})
.options('nofilerev', {
describe: 'Revision files with a hash of their content in their file name for optimal far future caching',
type: 'boolean',
default: false
})
.options('nocdnflash', {
describe: 'Avoid putting flash files on the cdnroot. Use this if you have problems setting up CORS',
type: 'boolean',
default: false
})
.options('define', {
alias: 'd',
describe: '--define SYMBOL[=value] will be passed to UglifyJS as is (see the docs at https://github.com/mishoo/UglifyJS#usage). Can be repeated. Remember to protect quotes from the shell, eg. --define foo=\\"bar\\".',
type: 'string'
})
.options('condition', {
describe: '--condition name[=value] will be passed to System.js builder and used in #{...} interpolations (as described on http://jspm.io/0.17-beta-guide/conditional-interpolation.html). Can be repeated.',
type: 'string'
})
.options('splitcondition', {
describe: '--splitcondition name will create a separate .html file for each condition value. Can be repeated.',
type: 'string'
})
.options('inline', {
describe: 'Set size threshold for inlining. Supported values: false (never inline), true (always inline, except when found inside @media queries), number (inline if target is smaller than this number of bytes). Also supported: --inlinehtmlscript true, --inlinecssimage 8192 etc.',
default: false
})
.options('cdnhtml', {
describe: 'Put non-initial HTML files on the cdnroot as well. Some CDN packages (such as Akamai\'s cheapest one) don\'t allow this',
type: 'boolean',
default: false
})
.options('sharedbundles', {
describe: 'Try to create shared bundles including commin files across multiple pages',
type: 'boolean',
default: false
})
.options('manifest', {
describe: 'Generates an appcache manifest file with all static assets included',
type: 'boolean',
default: false
})
.options('repl', {
describe: 'Start the REPL after a particular transform (or "error")',
type: 'string'
})
.options('sweep', {
describe: 'Experimental: Free up memory by cleaning up relations and assets after they have been removed from the graph',
type: 'boolean'
});
// These names originally come from UglifyJS' code generator, but are now mapped to their escodegen equivalents
var javaScriptSerializationOptionNames = [ 'indent_level', 'ascii_only' ];
javaScriptSerializationOptionNames.forEach(function (javaScriptSerializationOptionName) {
var type = (javaScriptSerializationOptionName === 'indent_level') ? 'number' : 'boolean';
// Also accept the option without underscores, or with the underscores replaced with dashes:
optimist.options(javaScriptSerializationOptionName.replace(/_/g, ''), {
alias: [javaScriptSerializationOptionName, javaScriptSerializationOptionName.replace(/_/g, '-')],
type: type,
description: 'UglifyJS serialization option, see http://lisperator.net/uglifyjs/codegen'
});
});
var commandLineOptions = optimist.argv;
var browsers = commandLineOptions.browsers;
var javaScriptSerializationOptions = {
compact: commandLineOptions.compact,
space: commandLineOptions.space,
newline: commandLineOptions.newline
};
[ 'preserve_line', 'preamble', 'quote_char', 'indent_start', 'quote_keys', 'space_colon', 'unescape_regexps', 'width', 'max_line_len', 'beautify', 'bracketize' ].forEach(function (deprecatedUglifyJsOption) {
[deprecatedUglifyJsOption, deprecatedUglifyJsOption.replace(/_/g, ''), deprecatedUglifyJsOption.replace(/_/g, '-')].forEach(function (alias) {
if (typeof commandLineOptions[alias] !== 'undefined') {
throw new Error('--' + alias + ' is no longer supported after UglifyJS was replaced with esprima/escodegen');
}
});
});
if (Array.isArray(browsers)) {
browsers = browsers.join(',');
}
javaScriptSerializationOptionNames.forEach(function (javaScriptSerializationOptionName) {
var value = commandLineOptions[javaScriptSerializationOptionName.replace(/_/g, '')];
if (typeof value !== 'undefined') {
javaScriptSerializationOptions[javaScriptSerializationOptionName] = value;
}
});
if (commandLineOptions.h) {
optimist.showHelp();
process.exit(0);
}
// Temporary deprecation message
if (commandLineOptions.stripdebug) {
console.warn(chalk.yellow('INFO: the --stripdebug switch is deprecated. This behavior is now default. Use --debug to keep debugging in build output'));
}
// Temporary deprecation message
if (commandLineOptions.cdnflash) {
console.warn(chalk.yellow('INFO: the --cdnflash switch is deprecated. This is now default functionality. Use --nocdnflash to get the old default behavior.'));
}
// Temporary deprecation message
if (commandLineOptions.cdnoutroot) {
console.warn(chalk.yellow('INFO: the --cdnoutroot switch is deprecated. Default location for your cdn assets is now <outroot>/static/cdn'));
}
// Temporary deprecation message
if (commandLineOptions.canonicalurl) {
console.warn(chalk.red('INFO: the --canonicalurl switch is deprecated. Please use --canonicalroot for the same effect plus more features'));
process.exit(1);
}
var _ = require('lodash'),
AssetGraph = require('../lib/AssetGraph'),
query = AssetGraph.query,
urlTools = require('urltools'),
outRoot = urlTools.fsDirToFileUrl(commandLineOptions.outroot),
cdnRoot = commandLineOptions.cdnroot && urlTools.ensureTrailingSlash(commandLineOptions.cdnroot),
fullCdnRoot = (/^\/\//.test(cdnRoot) ? 'http:' : '') + cdnRoot,
rootUrl = commandLineOptions.root && urlTools.urlOrFsPathToUrl(commandLineOptions.root, true),
reservedNames = commandLineOptions.reservednames && _.flatten(_.flatten([commandLineOptions.reservednames]).map(function (reservedName) {
return reservedName.split(',');
})),
plugins = commandLineOptions.plugin ? _.flatten(_.flatten([commandLineOptions.plugin])) : [],
inputUrls,
inlineByRelationType = {};
if (commandLineOptions.inline) {
inlineByRelationType['*'] = true;
}
// Doesn't touch non-string values or values that don't look like something boolean:
function convertStringToBoolean(str) {
if (typeof str === 'string') {
if (/^(?:on|true|yes|)$/.test(str)) {
return true;
} else if (/^(?:off|false|no)$/.test(str)) {
return false;
}
}
return str;
}
// Look for --inline<relationType> command line arguments:
Object.keys(AssetGraph).forEach(function (propertyName) {
var inlineThreshold = convertStringToBoolean(commandLineOptions['inline' + propertyName.toLowerCase()]);
if (typeof inlineThreshold !== 'undefined') {
inlineByRelationType[propertyName] = inlineThreshold;
}
});
// Use a default inline threshold of 8192 bytes for HtmlStyle and HtmlScript, unless --inlinehtmlscript/--inlinehtmlstyle (or --inline) was given
if (typeof inlineByRelationType['*'] === 'undefined') {
['HtmlScript', 'HtmlStyle'].forEach(function (relationType) {
if (typeof inlineByRelationType[relationType] === 'undefined') {
inlineByRelationType[relationType] = 8192;
}
});
}
if (commandLineOptions.inlinesize) {
console.warn(chalk.yellow('INFO: the --inlinesize switch is deprecated. Please use --inlinecssimage <number> instead'));
inlineByRelationType.CssImage = commandLineOptions.inlinesize;
} else if (!('CssImage' in inlineByRelationType)) {
inlineByRelationType.CssImage = 8192;
}
var defines = {};
(commandLineOptions.define ? _.flatten(_.flatten([commandLineOptions.define])) : []).forEach(function (define) {
var matchDefine = define.match(/^(\w+)(?:=(.*))?$/);
if (matchDefine) {
var valueAst;
if (matchDefine[2]) {
try {
valueAst = parseExpression(matchDefine[2]);
} catch (e) {
console.error('Invalid --define ' + matchDefine[1] + ': Could not parse ' + matchDefine[2] + ' as a JavaScript expression. Missing shell escapes?');
console.error(e.message + ' (line ' + e.line + ', column ' + e.col + ')');
process.exit(1);
}
} else {
valueAst = { type: 'Literal', value: true };
}
defines[matchDefine[1]] = valueAst;
}
});
var splitConditions = commandLineOptions.splitcondition && _.flatten(_.flatten([commandLineOptions.splitcondition]).map(function (name) {
return name.split(',');
}));
var conditions = {};
(commandLineOptions.condition ? _.flatten(_.flatten([commandLineOptions.condition])) : []).forEach(function (condition) {
var matchCondition = condition.match(/^([^=]+)=(.*)?$/);
if (matchCondition) {
var value = matchCondition[2];
if (value.indexOf(',') !== -1) {
// Array of values to trace
value = value.split(',');
}
conditions[matchCondition[1]] = value;
} else {
console.error('Invalid --condition ' + condition);
process.exit(1);
}
});
if (commandLineOptions._.length > 0) {
inputUrls = commandLineOptions._.map(function (urlOrFsPath) {
return urlTools.urlOrFsPathToUrl(String(urlOrFsPath), false);
});
if (!rootUrl) {
rootUrl = urlTools.findCommonUrlPrefix(inputUrls.filter(function (inputUrl) {
return /^file:/.test(inputUrl);
}));
if (rootUrl) {
console.warn('Guessing --root from input files: ' + rootUrl);
}
}
} else if (rootUrl && /^file:/.test(rootUrl)) {
inputUrls = [rootUrl + '*.html'];
console.warn('No input files specified, defaulting to ' + inputUrls[0]);
} else {
throw new Error('No input files and no --root specified (or it isn\'t file:), cannot proceed');
}
var buildProductionOptions = {
version: commandLineOptions.version,
browsers: browsers,
sourceMaps: commandLineOptions.sourcemaps,
sourcesContent: commandLineOptions.sourcescontent,
contentSecurityPolicy: commandLineOptions.contentsecuritypolicy,
subResourceIntegrity: commandLineOptions.subresourceintegrity,
optimizeImages: commandLineOptions.optimizeimages,
inlineByRelationType: inlineByRelationType,
gzip: commandLineOptions.gzip,
noFileRev: commandLineOptions.nofilerev,
defines: defines,
conditions: conditions,
splitConditions: splitConditions,
reservedNames: reservedNames,
localeCookieName: commandLineOptions.localecookiename,
manifest: commandLineOptions.manifest,
asyncScripts: commandLineOptions.asyncscripts,
deferScripts: commandLineOptions.deferscripts,
cdnRoot: cdnRoot,
recursive: commandLineOptions.recursive,
cdnFlash: !commandLineOptions.nocdnflash,
cdnHtml: commandLineOptions.cdnhtml,
svgo: commandLineOptions.svgo,
minify: commandLineOptions.minify,
noCompress: commandLineOptions.nocompress,
pretty: commandLineOptions.pretty,
sharedBundles: commandLineOptions.sharedbundles,
stripDebug: !commandLineOptions.debug,
addInitialHtmlExtension: commandLineOptions.addinitialhtmlextension,
javaScriptSerializationOptions: javaScriptSerializationOptions
};
new AssetGraph({
root: rootUrl,
canonicalRoot: commandLineOptions.canonicalroot
})
.queue(function (assetGraph) {
plugins.forEach(function (plugin) {
require(plugin)(assetGraph, buildProductionOptions, commandLineOptions);
});
if (commandLineOptions.sweep) {
assetGraph.on('removeAsset', function (asset) {
setImmediate(function () {
if (!asset.assetGraph) {
asset.unload();
}
});
}).on('removeRelation', function (relation) {
setImmediate(function () {
if (!relation.assetGraph) {
Object.keys(relation).forEach(function (key) {
relation[key] = null;
});
}
});
});
}
})
.logEvents({repl: commandLineOptions.repl, stopOnWarning: commandLineOptions.stoponwarning, suppressJavaScriptCommonJsRequireWarnings: true})
.loadAssets(inputUrls)
.buildProduction(buildProductionOptions)
.writeAssetsToDisc({url: /^file:/, isLoaded: true}, outRoot)
.if(cdnRoot)
.writeAssetsToDisc({url: query.createPrefixMatcher(fullCdnRoot), isLoaded: true}, outRoot + 'static/cdn/', fullCdnRoot)
.endif()
.writeStatsToStderr()
.run();