11import ts from 'typescript' ;
2- import vm from 'vm ' ;
2+ import path from 'path ' ;
33import { Module } from 'module' ;
44import type { Config , TypeToken } from './typeshot' ;
55
@@ -26,6 +26,7 @@ const CURRENT_TYPESHOT_CONTEXT = { current: null as null | TypeshotContext };
2626
2727export const getCurrentContext = ( ) => CURRENT_TYPESHOT_CONTEXT . current ;
2828
29+ const parent = module ;
2930const runSourceWithContext = (
3031 source : ts . SourceFile ,
3132 sourceText : string ,
@@ -34,17 +35,15 @@ const runSourceWithContext = (
3435) : TypeshotContext => {
3536 const prev = CURRENT_TYPESHOT_CONTEXT . current ;
3637 try {
37- const executableCode = ts . transpile ( sourceText , options ) ;
38- const { fileName } = source ;
39-
40- const MODULE = new Module ( fileName , module ) ;
41- const pure = MODULE . require ;
42- MODULE . filename = fileName ;
43- MODULE . require = ( ( id ) => ( id === 'typeshot' ? require ( './typeshot' ) : pure . call ( MODULE , id ) ) ) as typeof require ;
44- Object . assign ( MODULE . require , pure ) ;
45-
38+ const { fileName : filename } = source ;
39+ const script = ts . transpile ( sourceText , options ) ;
4640 CURRENT_TYPESHOT_CONTEXT . current = context ;
47- vm . runInNewContext ( executableCode , MODULE ) ;
41+
42+ // https://github.com/nodejs/node/blob/6add5b31fcc4ae45a8603f886477c544a99e0188/lib/internal/bootstrap_node.js#L415
43+ const module = new Module ( filename , parent ) ;
44+ module . filename = filename ;
45+ module . paths = ( Module as any ) . _nodeModulePaths ( path . dirname ( filename ) ) ;
46+ ( module as any ) . _compile ( script , filename ) ;
4847 } catch ( e ) {
4948 // eslint-disable-next-line no-console
5049 console . log ( e ) ;
0 commit comments