1
1
// thanks to airbnb/hypernova
2
2
3
- const NativeModule = require ( 'module' )
4
- const path = require ( 'path' )
5
- const { ok } = require ( 'assert' )
6
- const { runInNewContext } = require ( 'vm' )
3
+ var NativeModule = require ( 'module' )
4
+ var path = require ( 'path' )
5
+ var assert = require ( 'assert' )
6
+ var vm = require ( 'vm' )
7
7
8
- const NativeModules = process . binding ( 'natives' )
8
+ var NativeModules = process . binding ( 'natives' )
9
9
10
- const moduleExtensions = Object . assign ( { } , NativeModule . _extensions )
10
+ var moduleExtensions = Object . assign ( { } , NativeModule . _extensions )
11
11
12
12
function isNativeModule ( id ) {
13
13
return Object . prototype . hasOwnProperty . call ( NativeModules , id )
14
14
}
15
15
16
16
// Creates a sandbox so we don't share globals across different runs.
17
17
function createContext ( ) {
18
- const sandbox = {
18
+ var sandbox = {
19
19
Buffer,
20
20
clearImmediate,
21
21
clearInterval,
@@ -30,105 +30,102 @@ function createContext () {
30
30
return sandbox
31
31
}
32
32
33
- // This class should satisfy the Module interface that NodeJS defines in their native m.js
34
- // implementation.
35
- class Module {
36
- constructor ( id , parent , isBundle ) {
37
- const cache = parent ? parent . cache : null
38
- this . id = id
39
- this . exports = { }
40
- this . cache = cache || { }
41
- this . parent = parent
42
- this . filename = null
43
- this . loaded = false
44
- this . context = parent ? parent . context : createContext ( )
45
- this . isBundle = isBundle
46
- }
47
-
48
- load ( filename ) {
49
- ok ( ! this . loaded )
50
- this . filename = filename
51
- this . paths = NativeModule . _nodeModulePaths ( path . dirname ( filename ) )
52
- }
33
+ function Module ( id , parent , isBundle ) {
34
+ var cache = parent ? parent . cache : null
35
+ this . id = id
36
+ this . exports = { }
37
+ this . cache = cache || { }
38
+ this . parent = parent
39
+ this . filename = null
40
+ this . loaded = false
41
+ this . context = parent ? parent . context : createContext ( )
42
+ this . isBundle = isBundle
43
+ }
53
44
54
- run ( filename ) {
55
- const ext = path . extname ( filename )
56
- const extension = moduleExtensions [ ext ] ? ext : '.js'
57
- moduleExtensions [ extension ] ( this , filename )
58
- this . loaded = true
59
- }
45
+ Module . prototype . load = function ( filename ) {
46
+ assert . ok ( ! this . loaded )
47
+ this . filename = filename
48
+ this . paths = NativeModule . _nodeModulePaths ( path . dirname ( filename ) )
49
+ }
60
50
61
- require ( filePath ) {
62
- ok ( typeof filePath === 'string' , 'path must be a string' )
63
- return Module . loadFile ( filePath , this )
64
- }
51
+ Module . prototype . run = function ( filename ) {
52
+ var ext = path . extname ( filename )
53
+ var extension = moduleExtensions [ ext ] ? ext : '.js'
54
+ moduleExtensions [ extension ] ( this , filename )
55
+ this . loaded = true
56
+ }
65
57
66
- _compile ( content , filename ) {
67
- const self = this
58
+ Module . prototype . require = function ( filePath ) {
59
+ assert . ok ( typeof filePath === 'string' , 'path must be a string' )
60
+ return Module . loadFile ( filePath , this )
61
+ }
68
62
69
- function r ( filePath ) {
70
- return self . require ( filePath )
71
- }
72
- r . resolve = request => NativeModule . _resolveFilename ( request , this )
73
- r . main = process . mainModule
74
- r . extensions = moduleExtensions
75
- r . cache = this . cache
63
+ Module . prototype . _compile = function ( content , filename ) {
64
+ var self = this
76
65
77
- const dirname = path . dirname ( filename )
66
+ function r ( filePath ) {
67
+ return self . require ( filePath )
68
+ }
69
+ r . resolve = request => NativeModule . _resolveFilename ( request , this )
70
+ r . main = process . mainModule
71
+ r . extensions = moduleExtensions
72
+ r . cache = this . cache
78
73
79
- // create wrapper function
80
- const wrapper = NativeModule . wrap ( content )
74
+ var dirname = path . dirname ( filename )
81
75
82
- const options = {
83
- filename,
84
- displayErrors : true
85
- }
76
+ // create wrapper function
77
+ var wrapper = NativeModule . wrap ( content )
86
78
87
- const compiledWrapper = runInNewContext ( wrapper , this . context , options )
88
- return compiledWrapper . call ( this . exports , this . exports , r , this , filename , dirname )
79
+ var options = {
80
+ filename,
81
+ displayErrors : true
89
82
}
90
83
91
- static load ( id , filename = id ) {
92
- const m = new Module ( id )
93
- m . load ( filename )
94
- m . run ( filename )
95
- return m
96
- }
84
+ var compiledWrapper = vm . runInNewContext ( wrapper , this . context , options )
85
+ return compiledWrapper . call ( this . exports , this . exports , r , this , filename , dirname )
86
+ }
97
87
98
- static loadFile ( file , parent ) {
99
- const filename = NativeModule . _resolveFilename ( file , parent )
88
+ Module . load = function ( id , filename ) {
89
+ var m = new Module ( id )
90
+ filename = filename || id
91
+ m . load ( filename )
92
+ m . run ( filename )
93
+ return m
94
+ }
100
95
101
- if ( parent ) {
102
- const cachedModule = parent . cache [ filename ]
103
- if ( cachedModule ) return cachedModule . exports
104
- }
96
+ Module . loadFile = function ( file , parent ) {
97
+ var filename = NativeModule . _resolveFilename ( file , parent )
105
98
106
- if ( parent . isBundle || isNativeModule ( filename ) ) {
107
- return require ( filename )
108
- }
99
+ if ( parent ) {
100
+ var cachedModule = parent . cache [ filename ]
101
+ if ( cachedModule ) return cachedModule . exports
102
+ }
103
+
104
+ if ( parent . isBundle || isNativeModule ( filename ) ) {
105
+ return require ( filename )
106
+ }
109
107
110
- const m = new Module ( filename , parent )
108
+ var m = new Module ( filename , parent )
111
109
112
- m . cache [ filename ] = m
110
+ m . cache [ filename ] = m
113
111
114
- let hadException = true
112
+ var hadException = true
115
113
116
- try {
117
- m . load ( filename )
118
- m . run ( filename )
119
- hadException = false
120
- } finally {
121
- if ( hadException ) {
122
- delete m . cache [ filename ]
123
- }
114
+ try {
115
+ m . load ( filename )
116
+ m . run ( filename )
117
+ hadException = false
118
+ } finally {
119
+ if ( hadException ) {
120
+ delete m . cache [ filename ]
124
121
}
125
-
126
- return m . exports
127
122
}
128
123
129
- static addExtension ( ext , f ) {
130
- moduleExtensions [ ext ] = f
131
- }
124
+ return m . exports
125
+ }
126
+
127
+ Module . addExtension = function ( ext , f ) {
128
+ moduleExtensions [ ext ] = f
132
129
}
133
130
134
131
module . exports = Module
0 commit comments