@@ -12,16 +12,16 @@ import { ScriptTarget } from 'typescript';
12
12
import { loadEsmModule } from '../utils/load-esm' ;
13
13
import { ApplicationPresetOptions , I18nPluginCreators } from './presets/application' ;
14
14
15
- interface AngularCustomOptions extends Pick < ApplicationPresetOptions , 'angularLinker' | 'i18n' > {
16
- forceAsyncTransformation : boolean ;
17
- forceES5 : boolean ;
18
- optimize ?: {
19
- looseEnums : boolean ;
20
- pureTopLevel : boolean ;
21
- wrapDecorators : boolean ;
15
+ interface AngularCustomOptions extends Omit < ApplicationPresetOptions , 'instrumentCode' > {
16
+ instrumentCode ?: {
17
+ /** node_modules and test files are always excluded. */
18
+ excludedPaths : Set < String > ;
19
+ includedBasePath : string ;
22
20
} ;
23
21
}
24
22
23
+ export type AngularBabelLoaderOptions = AngularCustomOptions & Record < string , unknown > ;
24
+
25
25
// Extract Sourcemap input type from the remapping function since it is not currently exported
26
26
type SourceMapInput = Exclude < Parameters < typeof remapping > [ 0 ] , unknown [ ] > ;
27
27
@@ -62,7 +62,7 @@ async function requiresLinking(path: string, source: string): Promise<boolean> {
62
62
return needsLinking ( path , source ) ;
63
63
}
64
64
65
- export default custom < AngularCustomOptions > ( ( ) => {
65
+ export default custom < ApplicationPresetOptions > ( ( ) => {
66
66
const baseOptions = Object . freeze ( {
67
67
babelrc : false ,
68
68
configFile : false ,
@@ -73,15 +73,19 @@ export default custom<AngularCustomOptions>(() => {
73
73
} ) ;
74
74
75
75
return {
76
- async customOptions ( { i18n, scriptTarget, aot, optimize, ...rawOptions } , { source } ) {
76
+ async customOptions ( options , { source } ) {
77
+ const { i18n, scriptTarget, aot, optimize, instrumentCode, ...rawOptions } =
78
+ options as AngularBabelLoaderOptions ;
79
+
77
80
// Must process file if plugins are added
78
81
let shouldProcess = Array . isArray ( rawOptions . plugins ) && rawOptions . plugins . length > 0 ;
79
82
80
- const customOptions : AngularCustomOptions = {
83
+ const customOptions : ApplicationPresetOptions = {
81
84
forceAsyncTransformation : false ,
82
85
forceES5 : false ,
83
86
angularLinker : undefined ,
84
87
i18n : undefined ,
88
+ instrumentCode : undefined ,
85
89
} ;
86
90
87
91
// Analyze file for linking
@@ -117,7 +121,7 @@ export default custom<AngularCustomOptions>(() => {
117
121
customOptions . forceAsyncTransformation =
118
122
! / [ \\ / ] [ _ f ] ? e s m 2 0 1 5 [ \\ / ] / . test ( this . resourcePath ) && source . includes ( 'async' ) ;
119
123
}
120
- shouldProcess ||= customOptions . forceAsyncTransformation || customOptions . forceES5 ;
124
+ shouldProcess ||= customOptions . forceAsyncTransformation || customOptions . forceES5 || false ;
121
125
}
122
126
123
127
// Analyze for i18n inlining
@@ -163,6 +167,20 @@ export default custom<AngularCustomOptions>(() => {
163
167
shouldProcess = true ;
164
168
}
165
169
170
+ if (
171
+ instrumentCode &&
172
+ ! instrumentCode . excludedPaths . has ( this . resourcePath ) &&
173
+ ! / \. ( e 2 e | s p e c ) \. t s x ? $ | [ \\ / ] n o d e _ m o d u l e s [ \\ / ] / . test ( this . resourcePath ) &&
174
+ this . resourcePath . startsWith ( instrumentCode . includedBasePath )
175
+ ) {
176
+ // `babel-plugin-istanbul` has it's own includes but we do the below so that we avoid running the the loader.
177
+ customOptions . instrumentCode = {
178
+ includedBasePath : instrumentCode . includedBasePath ,
179
+ } ;
180
+
181
+ shouldProcess = true ;
182
+ }
183
+
166
184
// Add provided loader options to default base options
167
185
const loaderOptions : Record < string , unknown > = {
168
186
...baseOptions ,
@@ -184,32 +202,12 @@ export default custom<AngularCustomOptions>(() => {
184
202
return { custom : customOptions , loader : loaderOptions } ;
185
203
} ,
186
204
config ( configuration , { customOptions } ) {
187
- const plugins = configuration . options . plugins ?? [ ] ;
188
- if ( customOptions . optimize ) {
189
- if ( customOptions . optimize . pureTopLevel ) {
190
- plugins . push ( require ( './plugins/pure-toplevel-functions' ) . default ) ;
191
- }
192
-
193
- plugins . push (
194
- require ( './plugins/elide-angular-metadata' ) . default ,
195
- [
196
- require ( './plugins/adjust-typescript-enums' ) . default ,
197
- { loose : customOptions . optimize . looseEnums } ,
198
- ] ,
199
- [
200
- require ( './plugins/adjust-static-class-members' ) . default ,
201
- { wrapDecorators : customOptions . optimize . wrapDecorators } ,
202
- ] ,
203
- ) ;
204
- }
205
-
206
205
return {
207
206
...configuration . options ,
208
207
// Using `false` disables babel from attempting to locate sourcemaps or process any inline maps.
209
208
// The babel types do not include the false option even though it is valid
210
209
// eslint-disable-next-line @typescript-eslint/no-explicit-any
211
210
inputSourceMap : false as any ,
212
- plugins,
213
211
presets : [
214
212
...( configuration . options . presets || [ ] ) ,
215
213
[
@@ -240,16 +238,10 @@ export default custom<AngularCustomOptions>(() => {
240
238
// `@ampproject/remapping` source map objects but both are compatible with Webpack.
241
239
// This method for merging is used because it provides more accurate output
242
240
// and is faster while using less memory.
243
- result . map = {
244
- // Convert the SourceMap back to simple plain object.
245
- // This is needed because otherwise code-coverage will fail with `don't know how to turn this value into a node`
246
- // Which is throw by Babel when it is invoked again from `istanbul-lib-instrument`.
247
- // https://github.com/babel/babel/blob/780aa48d2a34dc55f556843074b6aed45e7eabeb/packages/babel-types/src/converters/valueToNode.ts#L115-L130
248
- ...( remapping (
249
- [ result . map as SourceMapInput , inputSourceMap as SourceMapInput ] ,
250
- ( ) => null ,
251
- ) as typeof result . map ) ,
252
- } ;
241
+ result . map = remapping (
242
+ [ result . map as SourceMapInput , inputSourceMap as SourceMapInput ] ,
243
+ ( ) => null ,
244
+ ) as typeof result . map ;
253
245
}
254
246
255
247
return result ;
0 commit comments