@@ -10,9 +10,8 @@ import { RuleFactory, SchematicsException, Tree } from '@angular-devkit/schemati
10
10
import { FileSystemCollectionDesc , NodeModulesEngineHost } from '@angular-devkit/schematics/tools' ;
11
11
import { readFileSync } from 'fs' ;
12
12
import { parse as parseJson } from 'jsonc-parser' ;
13
- import { createRequire } from 'module' ;
13
+ import { Module , createRequire } from 'module' ;
14
14
import { dirname , resolve } from 'path' ;
15
- import { TextEncoder } from 'util' ;
16
15
import { Script } from 'vm' ;
17
16
import { assertIsError } from '../../utilities/error' ;
18
17
@@ -204,38 +203,24 @@ function wrap(
204
203
205
204
// Setup a wrapper function to capture the module's exports
206
205
const schematicCode = readFileSync ( schematicFile , 'utf8' ) ;
207
- // `module` is required due to @angular/localize ng-add being in UMD format
208
- const headerCode = '(function() {\nvar exports = {};\nvar module = { exports };\n' ;
209
- const footerCode = exportName
210
- ? `\nreturn module.exports['${ exportName } '];});`
211
- : '\nreturn module.exports;});' ;
212
-
213
- const script = new Script ( headerCode + schematicCode + footerCode , {
206
+ const script = new Script ( Module . wrap ( schematicCode ) , {
214
207
filename : schematicFile ,
215
- lineOffset : 3 ,
208
+ lineOffset : 1 ,
216
209
} ) ;
217
-
218
- const context = {
219
- __dirname : schematicDirectory ,
220
- __filename : schematicFile ,
221
- Buffer,
222
- // TextEncoder is used by the compiler to generate i18n message IDs. See:
223
- // https://github.com/angular/angular/blob/main/packages/compiler/src/i18n/digest.ts#L17
224
- // It is referenced globally, because it may be run either on the browser or the server.
225
- // Usually Node exposes it globally, but in order for it to work, our custom context
226
- // has to expose it too. Issue context: https://github.com/angular/angular/issues/48940.
227
- TextEncoder,
228
- console,
229
- process,
230
- get global ( ) {
231
- return this ;
232
- } ,
233
- require : customRequire ,
210
+ const schematicModule = new Module ( schematicFile ) ;
211
+ const moduleFactory = script . runInThisContext ( ) ;
212
+
213
+ return ( ) => {
214
+ moduleFactory (
215
+ schematicModule . exports ,
216
+ customRequire ,
217
+ schematicModule ,
218
+ schematicFile ,
219
+ schematicDirectory ,
220
+ ) ;
221
+
222
+ return exportName ? schematicModule . exports [ exportName ] : schematicModule . exports ;
234
223
} ;
235
-
236
- const exportsFactory = script . runInNewContext ( context ) ;
237
-
238
- return exportsFactory ;
239
224
}
240
225
241
226
function loadBuiltinModule ( id : string ) : unknown {
0 commit comments