1
+ import { join } from 'node:path' ;
2
+
3
+ import { shouldMergeAngularProjects } from '../../adapter/angular-json' ;
4
+ import { PluginConfiguration , readNxJson } from '../../config/nx-json' ;
1
5
import { hashObject } from '../../hasher/file-hasher' ;
2
- import { readNxJson } from '../../config/nx-json' ;
3
- import { LoadedNxPlugin , loadNxPlugins } from './internal-api' ;
6
+ import { IS_WASM } from '../../native' ;
4
7
import { workspaceRoot } from '../../utils/workspace-root' ;
8
+ import { loadNxPluginInIsolation } from './isolation' ;
9
+ import { loadNxPlugin } from './in-process-loader' ;
10
+
11
+ import type { LoadedNxPlugin } from './loaded-nx-plugin' ;
12
+ import {
13
+ cleanupPluginTSTranspiler ,
14
+ pluginTranspilerIsRegistered ,
15
+ } from './transpiler' ;
5
16
17
+ /**
18
+ * Stuff for specified NX Plugins.
19
+ */
6
20
let currentPluginsConfigurationHash : string ;
7
21
let loadedPlugins : LoadedNxPlugin [ ] ;
8
22
let pendingPluginsPromise :
9
23
| Promise < readonly [ LoadedNxPlugin [ ] , ( ) => void ] >
10
24
| undefined ;
11
- let cleanup : ( ) => void ;
25
+ let cleanup : ( ) => void | undefined ;
12
26
13
- export async function getPlugins ( ) {
14
- const pluginsConfiguration = readNxJson ( ) . plugins ?? [ ] ;
27
+ export async function getPlugins (
28
+ root = workspaceRoot
29
+ ) : Promise < LoadedNxPlugin [ ] > {
30
+ const pluginsConfiguration = readNxJson ( root ) . plugins ?? [ ] ;
15
31
const pluginsConfigurationHash = hashObject ( pluginsConfiguration ) ;
16
32
17
33
// If the plugins configuration has not changed, reuse the current plugins
@@ -28,22 +44,29 @@ export async function getPlugins() {
28
44
cleanup ( ) ;
29
45
}
30
46
31
- pendingPluginsPromise ??= loadNxPlugins ( pluginsConfiguration , workspaceRoot ) ;
47
+ pendingPluginsPromise ??= loadSpecifiedNxPlugins ( pluginsConfiguration , root ) ;
32
48
33
49
currentPluginsConfigurationHash = pluginsConfigurationHash ;
34
- const [ result , cleanupFn ] = await pendingPluginsPromise ;
50
+ const [ [ result , cleanupFn ] , defaultPlugins ] = await Promise . all ( [
51
+ pendingPluginsPromise ,
52
+ getOnlyDefaultPlugins ( root ) ,
53
+ ] ) ;
35
54
cleanup = cleanupFn ;
36
- loadedPlugins = result ;
37
- return result ;
55
+ loadedPlugins = result . concat ( defaultPlugins ) ;
56
+ return loadedPlugins ;
38
57
}
39
58
59
+ /**
60
+ * Stuff for default NX Plugins.
61
+ */
62
+
40
63
let loadedDefaultPlugins : LoadedNxPlugin [ ] ;
41
64
let cleanupDefaultPlugins : ( ) => void ;
42
65
let pendingDefaultPluginPromise :
43
66
| Promise < readonly [ LoadedNxPlugin [ ] , ( ) => void ] >
44
67
| undefined ;
45
68
46
- export async function getOnlyDefaultPlugins ( ) {
69
+ export async function getOnlyDefaultPlugins ( root = workspaceRoot ) {
47
70
// If the plugins configuration has not changed, reuse the current plugins
48
71
if ( loadedDefaultPlugins ) {
49
72
return loadedPlugins ;
@@ -55,7 +78,7 @@ export async function getOnlyDefaultPlugins() {
55
78
cleanupDefaultPlugins ( ) ;
56
79
}
57
80
58
- pendingDefaultPluginPromise ??= loadNxPlugins ( [ ] , workspaceRoot ) ;
81
+ pendingDefaultPluginPromise ??= loadDefaultNxPlugins ( workspaceRoot ) ;
59
82
60
83
const [ result , cleanupFn ] = await pendingDefaultPluginPromise ;
61
84
cleanupDefaultPlugins = cleanupFn ;
@@ -66,6 +89,138 @@ export async function getOnlyDefaultPlugins() {
66
89
export function cleanupPlugins ( ) {
67
90
pendingPluginsPromise = undefined ;
68
91
pendingDefaultPluginPromise = undefined ;
69
- cleanup ( ) ;
70
- cleanupDefaultPlugins ( ) ;
92
+ cleanup ?.( ) ;
93
+ cleanupDefaultPlugins ?.( ) ;
94
+ }
95
+
96
+ /**
97
+ * Stuff for generic loading
98
+ */
99
+
100
+ function isIsolationEnabled ( ) {
101
+ // Explicitly enabled, regardless of further conditions
102
+ if ( process . env . NX_ISOLATE_PLUGINS === 'true' ) {
103
+ return true ;
104
+ }
105
+ if (
106
+ // Explicitly disabled
107
+ process . env . NX_ISOLATE_PLUGINS === 'false' ||
108
+ // Isolation is disabled on WASM builds currently.
109
+ IS_WASM
110
+ ) {
111
+ return false ;
112
+ }
113
+ // Default value
114
+ return true ;
115
+ }
116
+
117
+ const loadingMethod = isIsolationEnabled ( )
118
+ ? loadNxPluginInIsolation
119
+ : loadNxPlugin ;
120
+
121
+ async function loadDefaultNxPlugins ( root = workspaceRoot ) {
122
+ performance . mark ( 'loadDefaultNxPlugins:start' ) ;
123
+
124
+ const plugins = getDefaultPlugins ( root ) ;
125
+
126
+ const cleanupFunctions : Array < ( ) => void > = [ ] ;
127
+ const ret = [
128
+ await Promise . all (
129
+ plugins . map ( async ( plugin ) => {
130
+ performance . mark ( `Load Nx Plugin: ${ plugin } - start` ) ;
131
+
132
+ const [ loadedPluginPromise , cleanup ] = await loadingMethod (
133
+ plugin ,
134
+ root
135
+ ) ;
136
+
137
+ cleanupFunctions . push ( cleanup ) ;
138
+ const res = await loadedPluginPromise ;
139
+ performance . mark ( `Load Nx Plugin: ${ plugin } - end` ) ;
140
+ performance . measure (
141
+ `Load Nx Plugin: ${ plugin } ` ,
142
+ `Load Nx Plugin: ${ plugin } - start` ,
143
+ `Load Nx Plugin: ${ plugin } - end`
144
+ ) ;
145
+
146
+ return res ;
147
+ } )
148
+ ) ,
149
+ ( ) => {
150
+ for ( const fn of cleanupFunctions ) {
151
+ fn ( ) ;
152
+ }
153
+ if ( pluginTranspilerIsRegistered ( ) ) {
154
+ cleanupPluginTSTranspiler ( ) ;
155
+ }
156
+ } ,
157
+ ] as const ;
158
+ performance . mark ( 'loadDefaultNxPlugins:end' ) ;
159
+ performance . measure (
160
+ 'loadDefaultNxPlugins' ,
161
+ 'loadDefaultNxPlugins:start' ,
162
+ 'loadDefaultNxPlugins:end'
163
+ ) ;
164
+ return ret ;
165
+ }
166
+
167
+ async function loadSpecifiedNxPlugins (
168
+ plugins : PluginConfiguration [ ] ,
169
+ root = workspaceRoot
170
+ ) : Promise < readonly [ LoadedNxPlugin [ ] , ( ) => void ] > {
171
+ performance . mark ( 'loadSpecifiedNxPlugins:start' ) ;
172
+
173
+ plugins ??= [ ] ;
174
+
175
+ const cleanupFunctions : Array < ( ) => void > = [ ] ;
176
+ const ret = [
177
+ await Promise . all (
178
+ plugins . map ( async ( plugin ) => {
179
+ const pluginPath = typeof plugin === 'string' ? plugin : plugin . plugin ;
180
+ performance . mark ( `Load Nx Plugin: ${ pluginPath } - start` ) ;
181
+
182
+ const [ loadedPluginPromise , cleanup ] = await loadingMethod (
183
+ plugin ,
184
+ root
185
+ ) ;
186
+
187
+ cleanupFunctions . push ( cleanup ) ;
188
+ const res = await loadedPluginPromise ;
189
+ performance . mark ( `Load Nx Plugin: ${ pluginPath } - end` ) ;
190
+ performance . measure (
191
+ `Load Nx Plugin: ${ pluginPath } ` ,
192
+ `Load Nx Plugin: ${ pluginPath } - start` ,
193
+ `Load Nx Plugin: ${ pluginPath } - end`
194
+ ) ;
195
+
196
+ return res ;
197
+ } )
198
+ ) ,
199
+ ( ) => {
200
+ for ( const fn of cleanupFunctions ) {
201
+ fn ( ) ;
202
+ }
203
+ if ( pluginTranspilerIsRegistered ( ) ) {
204
+ cleanupPluginTSTranspiler ( ) ;
205
+ }
206
+ } ,
207
+ ] as const ;
208
+ performance . mark ( 'loadSpecifiedNxPlugins:end' ) ;
209
+ performance . measure (
210
+ 'loadSpecifiedNxPlugins' ,
211
+ 'loadSpecifiedNxPlugins:start' ,
212
+ 'loadSpecifiedNxPlugins:end'
213
+ ) ;
214
+ return ret ;
215
+ }
216
+
217
+ function getDefaultPlugins ( root : string ) {
218
+ return [
219
+ join ( __dirname , '../../plugins/js' ) ,
220
+ ...( shouldMergeAngularProjects ( root , false )
221
+ ? [ join ( __dirname , '../../adapter/angular-json' ) ]
222
+ : [ ] ) ,
223
+ join ( __dirname , '../../plugins/package-json' ) ,
224
+ join ( __dirname , '../../plugins/project-json/build-nodes/project-json' ) ,
225
+ ] ;
71
226
}
0 commit comments