Skip to content

Commit f10a828

Browse files
clydinfilipesilva
authored andcommitted
fix(@angular-devkit/build-angular): improve fidelity and performance of babel loader sourcemaps
This change uses the `@ampproject/remapping` package to merge sourcemaps generated from the customized babel loader used to perform the partial compilation linking in applications as well as other optimization and downleveling transformations. This provides more accurate output mapppings as well as improved performance and lower memory usage when source maps are enabled within a build.
1 parent c9b26c4 commit f10a828

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

packages/angular_devkit/build_angular/src/babel/babel-loader.d.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
*/
88

99
declare module 'babel-loader' {
10-
type BabelLoaderCustomizer<T> = (
11-
babel: typeof import('@babel/core'),
12-
) => {
10+
type BabelLoaderCustomizer<T> = (babel: typeof import('@babel/core')) => {
1311
customOptions?(
1412
this: import('webpack').loader.LoaderContext,
1513
loaderOptions: Record<string, unknown>,
@@ -20,6 +18,17 @@ declare module 'babel-loader' {
2018
configuration: import('@babel/core').PartialConfig,
2119
loaderArguments: { source: string; map?: unknown; customOptions: T },
2220
): import('@babel/core').TransformOptions;
21+
result?(
22+
this: import('webpack').loader.LoaderContext,
23+
result: import('@babel/core').BabelFileResult,
24+
context: {
25+
source: string;
26+
map?: unknown;
27+
customOptions: T;
28+
configuration: import('@babel/core').PartialConfig;
29+
options: import('@babel/core').TransformOptions;
30+
},
31+
): import('@babel/core').BabelFileResult;
2332
};
2433
function custom<T>(customizer: BabelLoaderCustomizer<T>): import('webpack').loader.Loader;
2534
}

packages/angular_devkit/build_angular/src/babel/webpack-loader.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import remapping from '@ampproject/remapping';
910
import { custom } from 'babel-loader';
1011
import { ScriptTarget } from 'typescript';
1112
import { loadEsmModule } from '../utils/load-esm';
@@ -21,6 +22,9 @@ interface AngularCustomOptions extends Pick<ApplicationPresetOptions, 'angularLi
2122
};
2223
}
2324

25+
// Extract Sourcemap input type from the remapping function since it is not currently exported
26+
type SourceMapInput = Exclude<Parameters<typeof remapping>[0], unknown[]>;
27+
2428
/**
2529
* Cached instance of the compiler-cli linker's needsLinking function.
2630
*/
@@ -201,10 +205,10 @@ export default custom<AngularCustomOptions>(() => {
201205

202206
return {
203207
...configuration.options,
204-
// Workaround for https://github.com/babel/babel-loader/pull/896 is available
205-
// Delete once the above PR is released
208+
// Using `false` disables babel from attempting to locate sourcemaps or process any inline maps.
209+
// The babel types do not include the false option even though it is valid
206210
// eslint-disable-next-line @typescript-eslint/no-explicit-any
207-
inputSourceMap: configuration.options.inputSourceMap || (false as any), // Typings are not correct
211+
inputSourceMap: false as any,
208212
plugins,
209213
presets: [
210214
...(configuration.options.presets || []),
@@ -229,5 +233,20 @@ export default custom<AngularCustomOptions>(() => {
229233
],
230234
};
231235
},
236+
result(result, { map: inputSourceMap }) {
237+
if (result.map && inputSourceMap) {
238+
// Merge the intermediate sourcemap generated by babel with the input source map.
239+
// The casting is required due to slight differences in the types for babel and
240+
// `@ampproject/remapping` source map objects but both are compatible with Webpack.
241+
// This method for merging is used because it provides more accurate output
242+
// and is faster while using less memory.
243+
result.map = remapping(
244+
[result.map as SourceMapInput, inputSourceMap as SourceMapInput],
245+
() => null,
246+
) as typeof result.map;
247+
}
248+
249+
return result;
250+
},
232251
};
233252
});

0 commit comments

Comments
 (0)