-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.node.ts
64 lines (45 loc) · 1.3 KB
/
node.node.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
interface $node {
[key:string]: any
}
var $node = new Proxy( { require } as any , {
get( target , name : string , wrapper ) {
if( target[ name ] ) return target[ name ]
const mod = target.require/****/( 'module' ) as typeof import/****/( 'module' )
if( mod.builtinModules.indexOf( name ) >= 0 ) return target.require ( name )
if( name[0] === '.' ) return target.require( name )
try {
target.require.resolve( name )
} catch {
const $$ = ( $ as any )
$$.$mol_exec( '.' , 'npm' , 'install' , '--omit=dev', name )
try {
$$.$mol_exec( '.' , 'npm' , 'install' , '--omit=dev', '@types/' + name )
} catch (e) {
if ($$.$mol_fail_catch(e)) {
$$.$mol_fail_log(e)
}
}
}
try {
return target.require( name )
} catch( error ) {
if($.$mol_fail_catch(error) && ( error as any ).code === 'ERR_REQUIRE_ESM' ) {
const module = cache.get( name )
if( module ) return module
throw import( name ).then( module => cache.set( name, module ) )
}
$.$mol_fail_log( error )
return null
}
},
set( target , name : string , value ) {
target[ name ] = value
return true
},
} ) as $node
const cache = new Map< string, any >()
require = ( req =>
Object.assign( function require( name : string ) {
return $node[ name ]
} , req )
)( require )