1
1
// thanks to airbnb/hypernova
2
+ 'use strict'
2
3
3
- var NativeModule = require ( 'module' )
4
- var path = require ( 'path' )
5
- var assert = require ( 'assert' )
6
- var vm = require ( 'vm' )
4
+ const NativeModule = require ( 'module' )
5
+ const path = require ( 'path' )
6
+ const assert = require ( 'assert' )
7
+ const vm = require ( 'vm' )
7
8
8
- var NativeModules = process . binding ( 'natives' )
9
+ const NativeModules = process . binding ( 'natives' )
9
10
10
- var moduleExtensions = Object . assign ( { } , NativeModule . _extensions )
11
+ const moduleExtensions = Object . assign ( { } , NativeModule . _extensions )
11
12
12
13
function isNativeModule ( id ) {
13
14
return Object . prototype . hasOwnProperty . call ( NativeModules , id )
14
15
}
15
16
16
17
// Creates a sandbox so we don't share globals across different runs.
17
- function createContext ( ) {
18
- var sandbox = {
18
+ function createContext ( context ) {
19
+ const sandbox = {
19
20
Buffer,
20
21
clearImmediate,
21
22
clearInterval,
@@ -24,22 +25,22 @@ function createContext () {
24
25
setInterval,
25
26
setTimeout,
26
27
console,
27
- process
28
+ process,
29
+ __VUE_SSR_CONTEXT__ : context || { }
28
30
}
29
31
sandbox . global = sandbox
30
32
return sandbox
31
33
}
32
34
33
- function Module ( id , parent , isBundle ) {
34
- var cache = parent ? parent . cache : null
35
+ function Module ( id , parent , context ) {
36
+ const cache = parent ? parent . cache : null
35
37
this . id = id
36
38
this . exports = { }
37
39
this . cache = cache || { }
38
40
this . parent = parent
39
41
this . filename = null
40
42
this . loaded = false
41
- this . context = parent ? parent . context : createContext ( )
42
- this . isBundle = isBundle
43
+ this . context = parent ? parent . context : createContext ( context )
43
44
}
44
45
45
46
Module . prototype . load = function ( filename ) {
@@ -49,8 +50,8 @@ Module.prototype.load = function (filename) {
49
50
}
50
51
51
52
Module . prototype . run = function ( filename ) {
52
- var ext = path . extname ( filename )
53
- var extension = moduleExtensions [ ext ] ? ext : '.js'
53
+ const ext = path . extname ( filename )
54
+ const extension = moduleExtensions [ ext ] ? ext : '.js'
54
55
moduleExtensions [ extension ] ( this , filename )
55
56
this . loaded = true
56
57
}
@@ -61,55 +62,51 @@ Module.prototype.require = function (filePath) {
61
62
}
62
63
63
64
Module . prototype . _compile = function ( content , filename ) {
64
- var self = this
65
-
66
- function r ( filePath ) {
67
- return self . require ( filePath )
68
- }
65
+ const r = filePath => this . require ( filePath )
69
66
r . resolve = request => NativeModule . _resolveFilename ( request , this )
70
67
r . main = process . mainModule
71
68
r . extensions = moduleExtensions
72
69
r . cache = this . cache
73
70
74
- var dirname = path . dirname ( filename )
71
+ const dirname = path . dirname ( filename )
75
72
76
73
// create wrapper function
77
- var wrapper = NativeModule . wrap ( content )
74
+ const wrapper = NativeModule . wrap ( content )
78
75
79
- var options = {
76
+ const options = {
80
77
filename,
81
78
displayErrors : true
82
79
}
83
80
84
- var compiledWrapper = vm . runInNewContext ( wrapper , this . context , options )
81
+ const compiledWrapper = vm . runInNewContext ( wrapper , this . context , options )
85
82
return compiledWrapper . call ( this . exports , this . exports , r , this , filename , dirname )
86
83
}
87
84
88
85
Module . load = function ( id , filename ) {
89
- var m = new Module ( id )
86
+ const m = new Module ( id )
90
87
filename = filename || id
91
88
m . load ( filename )
92
89
m . run ( filename )
93
90
return m
94
91
}
95
92
96
93
Module . loadFile = function ( file , parent ) {
97
- var filename = NativeModule . _resolveFilename ( file , parent )
94
+ const filename = NativeModule . _resolveFilename ( file , parent )
98
95
99
96
if ( parent ) {
100
- var cachedModule = parent . cache [ filename ]
97
+ const cachedModule = parent . cache [ filename ]
101
98
if ( cachedModule ) return cachedModule . exports
102
99
}
103
100
104
- if ( parent . isBundle || isNativeModule ( filename ) ) {
101
+ if ( isNativeModule ( filename ) ) {
105
102
return require ( filename )
106
103
}
107
104
108
- var m = new Module ( filename , parent )
105
+ const m = new Module ( filename , parent )
109
106
110
107
m . cache [ filename ] = m
111
108
112
- var hadException = true
109
+ let hadException = true
113
110
114
111
try {
115
112
m . load ( filename )
0 commit comments