@@ -34,8 +34,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
34
34
} ;
35
35
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
36
36
const core_1 = require ( "@babel/core" ) ;
37
+ const node_fs_1 = __importDefault ( require ( "node:fs" ) ) ;
38
+ const node_path_1 = __importDefault ( require ( "node:path" ) ) ;
37
39
const piscina_1 = __importDefault ( require ( "piscina" ) ) ;
38
- const application_1 = __importStar ( require ( "../../tools/babel/presets/application" ) ) ;
39
40
const load_esm_1 = require ( "../../utils/load-esm" ) ;
40
41
const textDecoder = new TextDecoder ( ) ;
41
42
const textEncoder = new TextEncoder ( ) ;
@@ -47,22 +48,41 @@ async function transformJavaScript(request) {
47
48
return piscina_1 . default . move ( textEncoder . encode ( transformedData ) ) ;
48
49
}
49
50
exports . default = transformJavaScript ;
51
+ /**
52
+ * Cached instance of the compiler-cli linker's createEs2015LinkerPlugin function.
53
+ */
50
54
let linkerPluginCreator ;
55
+ /**
56
+ * Cached instance of the compiler-cli linker's needsLinking function.
57
+ */
58
+ let needsLinking ;
51
59
async function transformWithBabel ( filename , data , options ) {
52
- const shouldLink = ! options . skipLinker && ( await ( 0 , application_1 . requiresLinking ) ( filename , data ) ) ;
60
+ const shouldLink = ! options . skipLinker && ( await requiresLinking ( filename , data ) ) ;
53
61
const useInputSourcemap = options . sourcemap &&
54
62
( ! ! options . thirdPartySourcemaps || ! / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] / . test ( filename ) ) ;
63
+ const plugins = [ ] ;
64
+ // Lazy load the linker plugin only when linking is required
65
+ if ( shouldLink ) {
66
+ const linkerPlugin = await createLinkerPlugin ( options ) ;
67
+ plugins . push ( linkerPlugin ) ;
68
+ }
69
+ if ( options . advancedOptimizations ) {
70
+ const sideEffectFree = options . sideEffects === false ;
71
+ const safeAngularPackage = sideEffectFree && / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] @ a n g u l a r [ \\ / ] / . test ( filename ) ;
72
+ const { adjustStaticMembers, adjustTypeScriptEnums, elideAngularMetadata, markTopLevelPure } = await Promise . resolve ( ) . then ( ( ) => __importStar ( require ( '../babel/plugins' ) ) ) ;
73
+ if ( safeAngularPackage ) {
74
+ plugins . push ( markTopLevelPure ) ;
75
+ }
76
+ plugins . push ( elideAngularMetadata , adjustTypeScriptEnums , [
77
+ adjustStaticMembers ,
78
+ { wrapDecorators : sideEffectFree } ,
79
+ ] ) ;
80
+ }
55
81
// If no additional transformations are needed, return the data directly
56
- if ( ! options . advancedOptimizations && ! shouldLink ) {
82
+ if ( plugins . length === 0 ) {
57
83
// Strip sourcemaps if they should not be used
58
84
return useInputSourcemap ? data : data . replace ( / ^ \/ \/ # s o u r c e M a p p i n g U R L = [ ^ \r \n ] * / gm, '' ) ;
59
85
}
60
- const sideEffectFree = options . sideEffects === false ;
61
- const safeAngularPackage = sideEffectFree && / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] @ a n g u l a r [ \\ / ] / . test ( filename ) ;
62
- // Lazy load the linker plugin only when linking is required
63
- if ( shouldLink ) {
64
- linkerPluginCreator ??= ( await ( 0 , load_esm_1 . loadEsmModule ) ( '@angular/compiler-cli/linker/babel' ) ) . createEs2015LinkerPlugin ;
65
- }
66
86
const result = await ( 0 , core_1 . transformAsync ) ( data , {
67
87
filename,
68
88
inputSourceMap : ( useInputSourcemap ? undefined : false ) ,
@@ -71,23 +91,7 @@ async function transformWithBabel(filename, data, options) {
71
91
configFile : false ,
72
92
babelrc : false ,
73
93
browserslistConfigFile : false ,
74
- plugins : [ ] ,
75
- presets : [
76
- [
77
- application_1 . default ,
78
- {
79
- angularLinker : linkerPluginCreator && {
80
- shouldLink,
81
- jitMode : options . jit ,
82
- linkerPluginCreator,
83
- } ,
84
- optimize : options . advancedOptimizations && {
85
- pureTopLevel : safeAngularPackage ,
86
- wrapDecorators : sideEffectFree ,
87
- } ,
88
- } ,
89
- ] ,
90
- ] ,
94
+ plugins,
91
95
} ) ;
92
96
const outputCode = result ?. code ?? data ;
93
97
// Strip sourcemaps if they should not be used.
@@ -96,3 +100,55 @@ async function transformWithBabel(filename, data, options) {
96
100
? outputCode
97
101
: outputCode . replace ( / ^ \/ \/ # s o u r c e M a p p i n g U R L = [ ^ \r \n ] * / gm, '' ) ;
98
102
}
103
+ async function requiresLinking ( path , source ) {
104
+ // @angular /core and @angular /compiler will cause false positives
105
+ // Also, TypeScript files do not require linking
106
+ if ( / [ \\ / ] @ a n g u l a r [ \\ / ] (?: c o m p i l e r | c o r e ) | \. t s x ? $ / . test ( path ) ) {
107
+ return false ;
108
+ }
109
+ if ( ! needsLinking ) {
110
+ // Load ESM `@angular/compiler-cli/linker` using the TypeScript dynamic import workaround.
111
+ // Once TypeScript provides support for keeping the dynamic import this workaround can be
112
+ // changed to a direct dynamic import.
113
+ const linkerModule = await ( 0 , load_esm_1 . loadEsmModule ) ( '@angular/compiler-cli/linker' ) ;
114
+ needsLinking = linkerModule . needsLinking ;
115
+ }
116
+ return needsLinking ( path , source ) ;
117
+ }
118
+ async function createLinkerPlugin ( options ) {
119
+ linkerPluginCreator ??= ( await ( 0 , load_esm_1 . loadEsmModule ) ( '@angular/compiler-cli/linker/babel' ) ) . createEs2015LinkerPlugin ;
120
+ const linkerPlugin = linkerPluginCreator ( {
121
+ linkerJitMode : options . jit ,
122
+ // This is a workaround until https://github.com/angular/angular/issues/42769 is fixed.
123
+ sourceMapping : false ,
124
+ logger : {
125
+ level : 1 , // Info level
126
+ debug ( ...args ) {
127
+ // eslint-disable-next-line no-console
128
+ console . debug ( args ) ;
129
+ } ,
130
+ info ( ...args ) {
131
+ // eslint-disable-next-line no-console
132
+ console . info ( args ) ;
133
+ } ,
134
+ warn ( ...args ) {
135
+ // eslint-disable-next-line no-console
136
+ console . warn ( args ) ;
137
+ } ,
138
+ error ( ...args ) {
139
+ // eslint-disable-next-line no-console
140
+ console . error ( args ) ;
141
+ } ,
142
+ } ,
143
+ fileSystem : {
144
+ resolve : node_path_1 . default . resolve ,
145
+ exists : node_fs_1 . default . existsSync ,
146
+ dirname : node_path_1 . default . dirname ,
147
+ relative : node_path_1 . default . relative ,
148
+ readFile : node_fs_1 . default . readFileSync ,
149
+ // Node.JS types don't overlap the Compiler types.
150
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
151
+ } ,
152
+ } ) ;
153
+ return linkerPlugin ;
154
+ }
0 commit comments