Skip to content

Commit 8925674

Browse files
clydinalan-agius4
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 mappings as well as improved performance and lower memory usage when source maps are enabled within a build. (cherry picked from commit f10a828)
1 parent e8df33b commit 8925674

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 { needsLinking } from '@angular/compiler-cli/linker';
1011
import { custom } from 'babel-loader';
1112
import { ScriptTarget } from 'typescript';
@@ -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
function requiresLinking(path: string, source: string): boolean {
2529
// @angular/core and @angular/compiler will cause false positives
2630
// Also, TypeScript files do not require linking
@@ -146,10 +150,10 @@ export default custom<AngularCustomOptions>(() => {
146150

147151
return {
148152
...configuration.options,
149-
// Workaround for https://github.com/babel/babel-loader/pull/896 is available
150-
// Delete once the above PR is released
153+
// Using `false` disables babel from attempting to locate sourcemaps or process any inline maps.
154+
// The babel types do not include the false option even though it is valid
151155
// eslint-disable-next-line @typescript-eslint/no-explicit-any
152-
inputSourceMap: configuration.options.inputSourceMap || (false as any), // Typings are not correct
156+
inputSourceMap: false as any,
153157
plugins,
154158
presets: [
155159
...(configuration.options.presets || []),
@@ -174,5 +178,20 @@ export default custom<AngularCustomOptions>(() => {
174178
],
175179
};
176180
},
181+
result(result, { map: inputSourceMap }) {
182+
if (result.map && inputSourceMap) {
183+
// Merge the intermediate sourcemap generated by babel with the input source map.
184+
// The casting is required due to slight differences in the types for babel and
185+
// `@ampproject/remapping` source map objects but both are compatible with Webpack.
186+
// This method for merging is used because it provides more accurate output
187+
// and is faster while using less memory.
188+
result.map = remapping(
189+
[result.map as SourceMapInput, inputSourceMap as SourceMapInput],
190+
() => null,
191+
) as typeof result.map;
192+
}
193+
194+
return result;
195+
},
177196
};
178197
});

0 commit comments

Comments
 (0)