11// thanks to airbnb/hypernova
2+ 'use strict'
23
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' )
78
8- var NativeModules = process . binding ( 'natives' )
9+ const NativeModules = process . binding ( 'natives' )
910
10- var moduleExtensions = Object . assign ( { } , NativeModule . _extensions )
11+ const moduleExtensions = Object . assign ( { } , NativeModule . _extensions )
1112
1213function isNativeModule ( id ) {
1314 return Object . prototype . hasOwnProperty . call ( NativeModules , id )
1415}
1516
1617// 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 = {
1920 Buffer,
2021 clearImmediate,
2122 clearInterval,
@@ -24,22 +25,22 @@ function createContext () {
2425 setInterval,
2526 setTimeout,
2627 console,
27- process
28+ process,
29+ __VUE_SSR_CONTEXT__ : context || { }
2830 }
2931 sandbox . global = sandbox
3032 return sandbox
3133}
3234
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
3537 this . id = id
3638 this . exports = { }
3739 this . cache = cache || { }
3840 this . parent = parent
3941 this . filename = null
4042 this . loaded = false
41- this . context = parent ? parent . context : createContext ( )
42- this . isBundle = isBundle
43+ this . context = parent ? parent . context : createContext ( context )
4344}
4445
4546Module . prototype . load = function ( filename ) {
@@ -49,8 +50,8 @@ Module.prototype.load = function (filename) {
4950}
5051
5152Module . 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'
5455 moduleExtensions [ extension ] ( this , filename )
5556 this . loaded = true
5657}
@@ -61,55 +62,51 @@ Module.prototype.require = function (filePath) {
6162}
6263
6364Module . 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 )
6966 r . resolve = request => NativeModule . _resolveFilename ( request , this )
7067 r . main = process . mainModule
7168 r . extensions = moduleExtensions
7269 r . cache = this . cache
7370
74- var dirname = path . dirname ( filename )
71+ const dirname = path . dirname ( filename )
7572
7673 // create wrapper function
77- var wrapper = NativeModule . wrap ( content )
74+ const wrapper = NativeModule . wrap ( content )
7875
79- var options = {
76+ const options = {
8077 filename,
8178 displayErrors : true
8279 }
8380
84- var compiledWrapper = vm . runInNewContext ( wrapper , this . context , options )
81+ const compiledWrapper = vm . runInNewContext ( wrapper , this . context , options )
8582 return compiledWrapper . call ( this . exports , this . exports , r , this , filename , dirname )
8683}
8784
8885Module . load = function ( id , filename ) {
89- var m = new Module ( id )
86+ const m = new Module ( id )
9087 filename = filename || id
9188 m . load ( filename )
9289 m . run ( filename )
9390 return m
9491}
9592
9693Module . loadFile = function ( file , parent ) {
97- var filename = NativeModule . _resolveFilename ( file , parent )
94+ const filename = NativeModule . _resolveFilename ( file , parent )
9895
9996 if ( parent ) {
100- var cachedModule = parent . cache [ filename ]
97+ const cachedModule = parent . cache [ filename ]
10198 if ( cachedModule ) return cachedModule . exports
10299 }
103100
104- if ( parent . isBundle || isNativeModule ( filename ) ) {
101+ if ( isNativeModule ( filename ) ) {
105102 return require ( filename )
106103 }
107104
108- var m = new Module ( filename , parent )
105+ const m = new Module ( filename , parent )
109106
110107 m . cache [ filename ] = m
111108
112- var hadException = true
109+ let hadException = true
113110
114111 try {
115112 m . load ( filename )
0 commit comments