@@ -2,56 +2,7 @@ import * as path from 'node:path';
22import * as url from 'node:url' ;
33import type { logging } from '@angular-devkit/core' ;
44
5- const _tsNodeRegister = ( ( ) => {
6- let lastTsConfig : string | undefined ;
7- return ( tsConfig : string , logger : logging . LoggerApi ) => {
8- // Check if the function was previously called with the same tsconfig
9- if ( lastTsConfig && lastTsConfig !== tsConfig ) {
10- logger . warn ( `Trying to register ts-node again with a different tsconfig - skipping the registration.
11- tsconfig 1: ${ lastTsConfig }
12- tsconfig 2: ${ tsConfig } ` ) ;
13- }
14-
15- if ( lastTsConfig ) {
16- return ;
17- }
18-
19- lastTsConfig = tsConfig ;
20-
21- loadTsNode ( ) . register ( {
22- project : tsConfig ,
23- compilerOptions : {
24- module : 'CommonJS' ,
25- types : [
26- 'node' , // NOTE: `node` is added because users scripts can also use pure node's packages as webpack or others
27- ] ,
28- } ,
29- } ) ;
30-
31- const tsConfigPaths = loadTsConfigPaths ( ) ;
32- const result = tsConfigPaths . loadConfig ( tsConfig ) ;
33- // The `loadConfig` returns a `ConfigLoaderResult` which must be guarded with
34- // the `resultType` check.
35- if ( result . resultType === 'success' ) {
36- const { absoluteBaseUrl : baseUrl , paths } = result ;
37- if ( baseUrl && paths ) {
38- tsConfigPaths . register ( { baseUrl, paths } ) ;
39- }
40- }
41- } ;
42- } ) ( ) ;
43-
44- /**
45- * check for TS node registration
46- * @param file: file name or file directory are allowed
47- * @todo tsNodeRegistration: require ts-node if file extension is TypeScript
48- */
49- function tsNodeRegister ( file : string = '' , tsConfig : string , logger : logging . LoggerApi ) {
50- if ( file ?. endsWith ( '.ts' ) ) {
51- // Register TS compiler lazily
52- _tsNodeRegister ( tsConfig , logger ) ;
53- }
54- }
5+ import { registerTsProject } from './register-ts-project' ;
556
567/**
578 * This uses a dynamic import to load a module which may be ESM.
@@ -72,22 +23,20 @@ function loadEsmModule<T>(modulePath: string | URL): Promise<T> {
7223/**
7324 * Loads CJS and ESM modules based on extension
7425 */
75- export async function loadModule < T > (
76- modulePath : string ,
77- tsConfig : string ,
78- logger : logging . LoggerApi
79- ) : Promise < T > {
80- tsNodeRegister ( modulePath , tsConfig , logger ) ;
81-
26+ export async function loadModule < T > ( modulePath : string , tsConfig : string ) : Promise < T > {
8227 switch ( path . extname ( modulePath ) ) {
8328 case '.mjs' :
8429 // Load the ESM configuration file using the TypeScript dynamic import workaround.
8530 // Once TypeScript provides support for keeping the dynamic import this workaround can be
8631 // changed to a direct dynamic import.
8732 return ( await loadEsmModule < { default : T } > ( url . pathToFileURL ( modulePath ) ) ) . default ;
33+
8834 case '.cjs' :
8935 return require ( modulePath ) ;
36+
9037 case '.ts' :
38+ const unregisterTsProject = registerTsProject ( tsConfig ) ;
39+
9140 try {
9241 // If it's a TS file then there are 2 cases for exporing an object.
9342 // The first one is `export blah`, transpiled into `module.exports = { blah} `.
@@ -101,7 +50,10 @@ export async function loadModule<T>(
10150 return ( await loadEsmModule < { default : T } > ( url . pathToFileURL ( modulePath ) ) ) . default ;
10251 }
10352 throw e ;
53+ } finally {
54+ unregisterTsProject ( ) ;
10455 }
56+
10557 //.js
10658 default :
10759 // The file could be either CommonJS or ESM.
@@ -120,19 +72,3 @@ export async function loadModule<T>(
12072 }
12173 }
12274}
123-
124- /**
125- * Loads `ts-node` lazily. Moved to a separate function to declare
126- * a return type, more readable than an inline variant.
127- */
128- function loadTsNode ( ) : typeof import ( 'ts-node' ) {
129- return require ( 'ts-node' ) ;
130- }
131-
132- /**
133- * Loads `tsconfig-paths` lazily. Moved to a separate function to declare
134- * a return type, more readable than an inline variant.
135- */
136- function loadTsConfigPaths ( ) : typeof import ( 'tsconfig-paths' ) {
137- return require ( 'tsconfig-paths' ) ;
138- }
0 commit comments