-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathprocess-bundle.ts
696 lines (609 loc) · 20.3 KB
/
process-bundle.ts
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { NodePath, ParseResult, parseSync, transformAsync, traverse, types } from '@babel/core';
import { createHash } from 'crypto';
import * as fs from 'fs';
import * as path from 'path';
import { RawSourceMap, SourceMapConsumer, SourceMapGenerator } from 'source-map';
import { minify } from 'terser';
import * as v8 from 'v8';
import { SourceMapSource } from 'webpack-sources';
import { manglingDisabled } from './environment-options';
import { I18nOptions } from './i18n-options';
const cacache = require('cacache');
const deserialize = ((v8 as unknown) as { deserialize(buffer: Buffer): unknown }).deserialize;
export interface ProcessBundleOptions {
filename: string;
code: string;
map?: string;
name: string;
sourceMaps?: boolean;
hiddenSourceMaps?: boolean;
vendorSourceMaps?: boolean;
runtime?: boolean;
optimize?: boolean;
optimizeOnly?: boolean;
ignoreOriginal?: boolean;
cacheKeys?: (string | null)[];
integrityAlgorithm?: 'sha256' | 'sha384' | 'sha512';
runtimeData?: ProcessBundleResult[];
}
export interface ProcessBundleResult {
name: string;
integrity?: string;
original?: ProcessBundleFile;
downlevel?: ProcessBundleFile;
}
export interface ProcessBundleFile {
filename: string;
size: number;
integrity?: string;
map?: {
filename: string;
size: number;
};
}
export const enum CacheKey {
OriginalCode = 0,
OriginalMap = 1,
DownlevelCode = 2,
DownlevelMap = 3,
}
let cachePath: string | undefined;
let i18n: I18nOptions | undefined;
export function setup(data: number[] | { cachePath: string; i18n: I18nOptions }): void {
const options = Array.isArray(data)
? (deserialize(Buffer.from(data)) as { cachePath: string; i18n: I18nOptions })
: data;
cachePath = options.cachePath;
i18n = options.i18n;
}
async function cachePut(content: string, key: string | null, integrity?: string): Promise<void> {
if (cachePath && key) {
await cacache.put(cachePath, key, content, {
metadata: { integrity },
});
}
}
export async function process(options: ProcessBundleOptions): Promise<ProcessBundleResult> {
if (!options.cacheKeys) {
options.cacheKeys = [];
}
const result: ProcessBundleResult = { name: options.name };
if (options.integrityAlgorithm) {
// Store unmodified code integrity value -- used for SRI value replacement
result.integrity = generateIntegrityValue(options.integrityAlgorithm, options.code);
}
// Runtime chunk requires specialized handling
if (options.runtime) {
return { ...result, ...(await processRuntime(options)) };
}
const basePath = path.dirname(options.filename);
const filename = path.basename(options.filename);
const downlevelFilename = filename.replace('es2015', 'es5');
const downlevel = !options.optimizeOnly;
// if code size is larger than 500kB, manually handle sourcemaps with newer source-map package.
// babel currently uses an older version that still supports sync calls
const codeSize = Buffer.byteLength(options.code);
const mapSize = options.map ? Buffer.byteLength(options.map) : 0;
const manualSourceMaps = codeSize >= 500 * 1024 || mapSize >= 500 * 1024;
const sourceCode = options.code;
const sourceMap = options.map ? JSON.parse(options.map) : undefined;
let downlevelCode;
let downlevelMap;
if (downlevel) {
// Downlevel the bundle
const transformResult = await transformAsync(sourceCode, {
filename: options.filename,
inputSourceMap: manualSourceMaps ? undefined : sourceMap,
babelrc: false,
// modules aren't needed since the bundles use webpack's custom module loading
// 'transform-typeof-symbol' generates slower code
presets: [['@babel/preset-env', { modules: false, exclude: ['transform-typeof-symbol'] }]],
minified: options.optimize,
// `false` ensures it is disabled and prevents large file warnings
compact: options.optimize || false,
sourceMaps: !!sourceMap,
});
if (!transformResult || !transformResult.code) {
throw new Error(`Unknown error occurred processing bundle for "${options.filename}".`);
}
downlevelCode = transformResult.code;
if (manualSourceMaps && sourceMap && transformResult.map) {
downlevelMap = await mergeSourceMapsFast(sourceMap, transformResult.map);
} else {
// undefined is needed here to normalize the property type
downlevelMap = transformResult.map || undefined;
}
}
if (options.optimize) {
if (downlevelCode) {
const minifyResult = terserMangle(downlevelCode, {
filename: downlevelFilename,
map: downlevelMap,
compress: true,
});
downlevelCode = minifyResult.code;
downlevelMap = minifyResult.map;
}
if (!options.ignoreOriginal) {
result.original = await mangleOriginal(options);
}
}
if (downlevelCode) {
const downlevelPath = path.join(basePath, downlevelFilename);
let mapContent;
if (downlevelMap) {
if (!options.hiddenSourceMaps) {
downlevelCode += `\n//# sourceMappingURL=${downlevelFilename}.map`;
}
mapContent = JSON.stringify(downlevelMap);
await cachePut(mapContent, options.cacheKeys[CacheKey.DownlevelMap]);
fs.writeFileSync(downlevelPath + '.map', mapContent);
}
result.downlevel = createFileEntry(
path.join(basePath, downlevelFilename),
downlevelCode,
mapContent,
options.integrityAlgorithm,
);
await cachePut(
downlevelCode,
options.cacheKeys[CacheKey.DownlevelCode],
result.downlevel.integrity,
);
fs.writeFileSync(downlevelPath, downlevelCode);
}
// If original was not processed, add info
if (!result.original && !options.ignoreOriginal) {
result.original = createFileEntry(
options.filename,
options.code,
options.map,
options.integrityAlgorithm,
);
}
return result;
}
function mergeSourceMaps(
inputCode: string,
inputSourceMap: RawSourceMap,
resultCode: string,
resultSourceMap: RawSourceMap,
filename: string,
): RawSourceMap {
// More accurate but significantly more costly
// The last argument is not yet in the typings
// tslint:disable-next-line: no-any
return new (SourceMapSource as any)(
resultCode,
filename,
resultSourceMap,
inputCode,
inputSourceMap,
true,
).map();
}
async function mergeSourceMapsFast(first: RawSourceMap, second: RawSourceMap) {
const sourceRoot = first.sourceRoot;
const generator = new SourceMapGenerator();
// sourcemap package adds the sourceRoot to all position source paths if not removed
delete first.sourceRoot;
await SourceMapConsumer.with(first, null, originalConsumer => {
return SourceMapConsumer.with(second, null, newConsumer => {
newConsumer.eachMapping(mapping => {
if (mapping.originalLine === null) {
return;
}
const originalPosition = originalConsumer.originalPositionFor({
line: mapping.originalLine,
column: mapping.originalColumn,
});
if (
originalPosition.line === null ||
originalPosition.column === null ||
originalPosition.source === null
) {
return;
}
generator.addMapping({
generated: {
line: mapping.generatedLine,
column: mapping.generatedColumn,
},
name: originalPosition.name || undefined,
original: {
line: originalPosition.line,
column: originalPosition.column,
},
source: originalPosition.source,
});
});
});
});
const map = generator.toJSON();
map.file = second.file;
map.sourceRoot = sourceRoot;
// Put the sourceRoot back
if (sourceRoot) {
first.sourceRoot = sourceRoot;
}
return map;
}
async function mangleOriginal(options: ProcessBundleOptions): Promise<ProcessBundleFile> {
const result = terserMangle(options.code, {
filename: path.basename(options.filename),
map: options.map ? JSON.parse(options.map) : undefined,
ecma: 6,
});
let mapContent;
if (result.map) {
if (!options.hiddenSourceMaps) {
result.code += `\n//# sourceMappingURL=${path.basename(options.filename)}.map`;
}
mapContent = JSON.stringify(result.map);
await cachePut(
mapContent,
(options.cacheKeys && options.cacheKeys[CacheKey.OriginalMap]) || null,
);
fs.writeFileSync(options.filename + '.map', mapContent);
}
const fileResult = createFileEntry(
options.filename,
result.code,
mapContent,
options.integrityAlgorithm,
);
await cachePut(
result.code,
(options.cacheKeys && options.cacheKeys[CacheKey.OriginalCode]) || null,
fileResult.integrity,
);
fs.writeFileSync(options.filename, result.code);
return fileResult;
}
function terserMangle(
code: string,
options: { filename?: string; map?: RawSourceMap; compress?: boolean; ecma?: 5 | 6 } = {},
) {
// Note: Investigate converting the AST instead of re-parsing
// estree -> terser is already supported; need babel -> estree/terser
// Mangle downlevel code
const minifyOutput = minify(code, {
compress: options.compress || false,
ecma: options.ecma || 5,
mangle: !manglingDisabled,
safari10: true,
output: {
ascii_only: true,
webkit: true,
},
sourceMap:
!!options.map &&
({
filename: options.filename,
// terser uses an old version of the sourcemap typings
// tslint:disable-next-line: no-any
content: options.map as any,
asObject: true,
// typings don't include asObject option
// tslint:disable-next-line: no-any
} as any),
});
if (minifyOutput.error) {
throw minifyOutput.error;
}
// tslint:disable-next-line: no-non-null-assertion
return { code: minifyOutput.code!, map: minifyOutput.map as RawSourceMap | undefined };
}
function createFileEntry(
filename: string,
code: string,
map: string | undefined,
integrityAlgorithm?: string,
): ProcessBundleFile {
return {
filename: filename,
size: Buffer.byteLength(code),
integrity: integrityAlgorithm && generateIntegrityValue(integrityAlgorithm, code),
map: !map
? undefined
: {
filename: filename + '.map',
size: Buffer.byteLength(map),
},
};
}
function generateIntegrityValue(hashAlgorithm: string, code: string) {
return (
hashAlgorithm +
'-' +
createHash(hashAlgorithm)
.update(code)
.digest('base64')
);
}
// The webpack runtime chunk is already ES5.
// However, two variants are still needed due to lazy routing and SRI differences
// NOTE: This should eventually be a babel plugin
async function processRuntime(
options: ProcessBundleOptions,
): Promise<Partial<ProcessBundleResult>> {
let originalCode = options.code;
let downlevelCode = options.code;
// Replace integrity hashes with updated values
if (options.integrityAlgorithm && options.runtimeData) {
for (const data of options.runtimeData) {
if (!data.integrity) {
continue;
}
if (data.original && data.original.integrity) {
originalCode = originalCode.replace(data.integrity, data.original.integrity);
}
if (data.downlevel && data.downlevel.integrity) {
downlevelCode = downlevelCode.replace(data.integrity, data.downlevel.integrity);
}
}
}
// Adjust lazy loaded scripts to point to the proper variant
// Extra spacing is intentional to align source line positions
downlevelCode = downlevelCode.replace('"-es2015.', ' "-es5.');
const downlevelFilePath = options.filename.replace('es2015', 'es5');
let downlevelMap;
let result;
if (options.optimize) {
const minifiyResults = terserMangle(downlevelCode, {
filename: path.basename(downlevelFilePath),
map: options.map === undefined ? undefined : JSON.parse(options.map),
});
downlevelCode = minifiyResults.code;
downlevelMap = JSON.stringify(minifiyResults.map);
result = {
original: await mangleOriginal({ ...options, code: originalCode }),
downlevel: createFileEntry(
downlevelFilePath,
downlevelCode,
downlevelMap,
options.integrityAlgorithm,
),
};
} else {
if (options.map) {
const rawMap = JSON.parse(options.map) as RawSourceMap;
rawMap.file = path.basename(downlevelFilePath);
downlevelMap = JSON.stringify(rawMap);
}
result = {
original: createFileEntry(
options.filename,
originalCode,
options.map,
options.integrityAlgorithm,
),
downlevel: createFileEntry(
downlevelFilePath,
downlevelCode,
downlevelMap,
options.integrityAlgorithm,
),
};
}
if (downlevelMap) {
await cachePut(
downlevelMap,
(options.cacheKeys && options.cacheKeys[CacheKey.DownlevelMap]) || null,
);
fs.writeFileSync(downlevelFilePath + '.map', downlevelMap);
if (!options.hiddenSourceMaps) {
downlevelCode += `\n//# sourceMappingURL=${path.basename(downlevelFilePath)}.map`;
}
}
await cachePut(
downlevelCode,
(options.cacheKeys && options.cacheKeys[CacheKey.DownlevelCode]) || null,
);
fs.writeFileSync(downlevelFilePath, downlevelCode);
return result;
}
export interface InlineOptions {
filename: string;
code: string;
map?: string;
es5: boolean;
outputPath: string;
missingTranslation?: 'warning' | 'error' | 'ignore';
setLocale?: boolean;
}
interface LocalizePosition {
start: number;
end: number;
messageParts: TemplateStringsArray;
expressions: types.Expression[];
}
const localizeName = '$localize';
export async function inlineLocales(options: InlineOptions) {
if (!i18n || i18n.inlineLocales.size === 0) {
return { file: options.filename, diagnostics: [], count: 0 };
}
if (i18n.flatOutput && i18n.inlineLocales.size > 1) {
throw new Error('Flat output is only supported when inlining one locale.');
}
const hasLocalizeName = options.code.includes(localizeName);
if (!hasLocalizeName && !options.setLocale) {
return inlineCopyOnly(options);
}
const { default: MagicString } = await import('magic-string');
const { default: generate } = await import('@babel/generator');
const utils = await import(
// tslint:disable-next-line: trailing-comma no-implicit-dependencies
'@angular/localize/src/tools/src/translate/source_files/source_file_utils'
);
// tslint:disable-next-line: no-implicit-dependencies
const localizeDiag = await import('@angular/localize/src/tools/src/diagnostics');
const diagnostics = new localizeDiag.Diagnostics();
const positions = findLocalizePositions(options, utils);
if (positions.length === 0 && !options.setLocale) {
return inlineCopyOnly(options);
}
let content = new MagicString(options.code);
const inputMap = options.map && (JSON.parse(options.map) as RawSourceMap);
let contentClone;
for (const locale of i18n.inlineLocales) {
const isSourceLocale = locale === i18n.sourceLocale;
// tslint:disable-next-line: no-any
const translations: any = isSourceLocale ? {} : i18n.locales[locale].translation || {};
for (const position of positions) {
const translated = utils.translate(
diagnostics,
translations,
position.messageParts,
position.expressions,
isSourceLocale ? 'ignore' : options.missingTranslation || 'warning',
);
const expression = utils.buildLocalizeReplacement(translated[0], translated[1]);
const { code } = generate(expression);
content.overwrite(position.start, position.end, code);
}
if (options.setLocale) {
const setLocaleText = `var $localize=Object.assign(void 0===$localize?{}:$localize,{locale:"${locale}"});`;
contentClone = content.clone();
content.prepend(setLocaleText);
// If locale data is provided, load it and prepend to file
const localeDataPath = i18n.locales[locale] && i18n.locales[locale].dataPath;
if (localeDataPath) {
const localDataContent = loadLocaleData(localeDataPath, true);
// The semicolon ensures that there is no syntax error between statements
content.prepend(localDataContent + ';');
}
}
const output = content.toString();
const outputPath = path.join(
options.outputPath,
i18n.flatOutput ? '' : locale,
options.filename,
);
fs.writeFileSync(outputPath, output);
if (inputMap) {
const contentMap = content.generateMap();
const outputMap = mergeSourceMaps(
options.code,
inputMap,
output,
contentMap,
options.filename,
);
fs.writeFileSync(outputPath + '.map', JSON.stringify(outputMap));
}
if (contentClone) {
content = contentClone;
contentClone = undefined;
}
}
return { file: options.filename, diagnostics: diagnostics.messages, count: positions.length };
}
function inlineCopyOnly(options: InlineOptions) {
if (!i18n) {
throw new Error('i18n options are missing');
}
for (const locale of i18n.inlineLocales) {
const outputPath = path.join(
options.outputPath,
i18n.flatOutput ? '' : locale,
options.filename,
);
fs.writeFileSync(outputPath, options.code);
if (options.map) {
fs.writeFileSync(outputPath + '.map', options.map);
}
}
return { file: options.filename, diagnostics: [], count: 0 };
}
function findLocalizePositions(
options: InlineOptions,
// tslint:disable-next-line: no-implicit-dependencies
utils: typeof import('@angular/localize/src/tools/src/translate/source_files/source_file_utils'),
): LocalizePosition[] {
let ast: ParseResult | undefined | null;
try {
ast = parseSync(options.code, {
babelrc: false,
sourceType: 'script',
});
} catch (error) {
if (error.message) {
// Make the error more readable.
// Same errors will contain the full content of the file as the error message
// Which makes it hard to find the actual error message.
const index = error.message.indexOf(')\n');
const msg = index !== -1 ? error.message.substr(0, index + 1) : error.message;
throw new Error(`${msg}\nAn error occurred inlining file "${options.filename}"`);
}
}
if (!ast) {
throw new Error(`Unknown error occurred inlining file "${options.filename}"`);
}
const positions: LocalizePosition[] = [];
if (options.es5) {
traverse(ast, {
CallExpression(path: NodePath<types.CallExpression>) {
const callee = path.get('callee');
if (
callee.isIdentifier() &&
callee.node.name === localizeName &&
utils.isGlobalIdentifier(callee)
) {
const messageParts = utils.unwrapMessagePartsFromLocalizeCall(path);
const expressions = utils.unwrapSubstitutionsFromLocalizeCall(path.node);
positions.push({
// tslint:disable-next-line: no-non-null-assertion
start: path.node.start!,
// tslint:disable-next-line: no-non-null-assertion
end: path.node.end!,
messageParts,
expressions,
});
}
},
});
} else {
const traverseFast = ((types as unknown) as {
traverseFast: (node: types.Node, enter: (node: types.Node) => void) => void;
}).traverseFast;
traverseFast(ast, node => {
if (
node.type === 'TaggedTemplateExpression' &&
types.isIdentifier(node.tag) &&
node.tag.name === localizeName
) {
const messageParts = utils.unwrapMessagePartsFromTemplateLiteral(node.quasi.quasis);
positions.push({
// tslint:disable-next-line: no-non-null-assertion
start: node.start!,
// tslint:disable-next-line: no-non-null-assertion
end: node.end!,
messageParts,
expressions: node.quasi.expressions,
});
}
});
}
return positions;
}
function loadLocaleData(path: string, optimize: boolean): string {
// The path is validated during option processing before the build starts
const content = fs.readFileSync(path, 'utf8');
// NOTE: This can be removed once the locale data files are preprocessed in the framework
if (optimize) {
const result = terserMangle(content, {
compress: true,
ecma: 5,
});
return result.code;
}
return content;
}