|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be |
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
| 8 | +import { transformAsync } from '@babel/core'; |
8 | 9 | import { createHash } from 'crypto'; |
9 | 10 | import * as fs from 'fs'; |
10 | 11 | import * as path from 'path'; |
11 | 12 | import { RawSourceMap, SourceMapConsumer, SourceMapGenerator } from 'source-map'; |
12 | 13 | import { minify } from 'terser'; |
13 | | -import { ScriptTarget, transpileModule } from 'typescript'; |
14 | | -import { SourceMapSource } from 'webpack-sources'; |
15 | 14 | import { manglingDisabled } from './mangle-options'; |
16 | 15 |
|
17 | 16 | const cacache = require('cacache'); |
@@ -104,32 +103,37 @@ export async function process(options: ProcessBundleOptions): Promise<ProcessBun |
104 | 103 | let downlevelMap; |
105 | 104 | if (downlevel) { |
106 | 105 | // Downlevel the bundle |
107 | | - const transformResult = transpileModule(sourceCode, { |
108 | | - fileName: downlevelFilename, |
109 | | - compilerOptions: { |
110 | | - downlevelIteration: true, |
111 | | - sourceMap: !!sourceMap, |
112 | | - target: ScriptTarget.ES5, |
113 | | - }, |
| 106 | + const transformResult = await transformAsync(sourceCode, { |
| 107 | + filename: options.filename, |
| 108 | + inputSourceMap: manualSourceMaps ? undefined : sourceMap, |
| 109 | + babelrc: false, |
| 110 | + presets: [ |
| 111 | + [ |
| 112 | + require.resolve('@babel/preset-env'), |
| 113 | + { |
| 114 | + // modules aren't needed since the bundles use webpack's custom module loading |
| 115 | + modules: false, |
| 116 | + // 'transform-typeof-symbol' generates slower code |
| 117 | + exclude: ['transform-typeof-symbol'], |
| 118 | + }, |
| 119 | + ], |
| 120 | + ], |
| 121 | + minified: options.optimize, |
| 122 | + // `false` ensures it is disabled and prevents large file warnings |
| 123 | + compact: options.optimize || false, |
| 124 | + sourceMaps: !!sourceMap, |
114 | 125 | }); |
115 | 126 |
|
116 | | - downlevelCode = transformResult.outputText; |
117 | | - |
118 | | - if (sourceMap && transformResult.sourceMapText) { |
119 | | - if (manualSourceMaps) { |
120 | | - downlevelMap = await mergeSourcemaps(sourceMap, JSON.parse(transformResult.sourceMapText)); |
121 | | - } else { |
122 | | - // More accurate but significantly more costly |
123 | | - const tempSource = new SourceMapSource( |
124 | | - transformResult.outputText, |
125 | | - downlevelFilename, |
126 | | - JSON.parse(transformResult.sourceMapText), |
127 | | - sourceCode, |
128 | | - sourceMap, |
129 | | - ); |
130 | | - |
131 | | - downlevelMap = tempSource.map(); |
132 | | - } |
| 127 | + if (!transformResult || !transformResult.code) { |
| 128 | + throw new Error(`Unknown error occurred processing bundle for "${options.filename}".`); |
| 129 | + } |
| 130 | + downlevelCode = transformResult.code; |
| 131 | + |
| 132 | + if (manualSourceMaps && sourceMap && transformResult.map) { |
| 133 | + downlevelMap = await mergeSourcemaps(sourceMap, transformResult.map); |
| 134 | + } else { |
| 135 | + // undefined is needed here to normalize the property type |
| 136 | + downlevelMap = transformResult.map || undefined; |
133 | 137 | } |
134 | 138 | } |
135 | 139 |
|
|
0 commit comments