|
|
@@ -1,21 +1,22 @@
|
|
|
// thanks to airbnb/hypernova
|
|
|
+'use strict'
|
|
|
|
|
|
-var NativeModule = require('module')
|
|
|
-var path = require('path')
|
|
|
-var assert = require('assert')
|
|
|
-var vm = require('vm')
|
|
|
+const NativeModule = require('module')
|
|
|
+const path = require('path')
|
|
|
+const assert = require('assert')
|
|
|
+const vm = require('vm')
|
|
|
|
|
|
-var NativeModules = process.binding('natives')
|
|
|
+const NativeModules = process.binding('natives')
|
|
|
|
|
|
-var moduleExtensions = Object.assign({}, NativeModule._extensions)
|
|
|
+const moduleExtensions = Object.assign({}, NativeModule._extensions)
|
|
|
|
|
|
function isNativeModule (id) {
|
|
|
return Object.prototype.hasOwnProperty.call(NativeModules, id)
|
|
|
}
|
|
|
|
|
|
// Creates a sandbox so we don't share globals across different runs.
|
|
|
-function createContext () {
|
|
|
- var sandbox = {
|
|
|
+function createContext (context) {
|
|
|
+ const sandbox = {
|
|
|
Buffer,
|
|
|
clearImmediate,
|
|
|
clearInterval,
|
|
|
@@ -24,22 +25,22 @@ function createContext () {
|
|
|
setInterval,
|
|
|
setTimeout,
|
|
|
console,
|
|
|
- process
|
|
|
+ process,
|
|
|
+ __VUE_SSR_CONTEXT__: context || {}
|
|
|
}
|
|
|
sandbox.global = sandbox
|
|
|
return sandbox
|
|
|
}
|
|
|
|
|
|
-function Module (id, parent, isBundle) {
|
|
|
- var cache = parent ? parent.cache : null
|
|
|
+function Module (id, parent, context) {
|
|
|
+ const cache = parent ? parent.cache : null
|
|
|
this.id = id
|
|
|
this.exports = {}
|
|
|
this.cache = cache || {}
|
|
|
this.parent = parent
|
|
|
this.filename = null
|
|
|
this.loaded = false
|
|
|
- this.context = parent ? parent.context : createContext()
|
|
|
- this.isBundle = isBundle
|
|
|
+ this.context = parent ? parent.context : createContext(context)
|
|
|
}
|
|
|
|
|
|
Module.prototype.load = function (filename) {
|
|
|
@@ -49,8 +50,8 @@ Module.prototype.load = function (filename) {
|
|
|
}
|
|
|
|
|
|
Module.prototype.run = function (filename) {
|
|
|
- var ext = path.extname(filename)
|
|
|
- var extension = moduleExtensions[ext] ? ext : '.js'
|
|
|
+ const ext = path.extname(filename)
|
|
|
+ const extension = moduleExtensions[ext] ? ext : '.js'
|
|
|
moduleExtensions[extension](this, filename)
|
|
|
this.loaded = true
|
|
|
}
|
|
|
@@ -61,32 +62,28 @@ Module.prototype.require = function (filePath) {
|
|
|
}
|
|
|
|
|
|
Module.prototype._compile = function (content, filename) {
|
|
|
- var self = this
|
|
|
-
|
|
|
- function r (filePath) {
|
|
|
- return self.require(filePath)
|
|
|
- }
|
|
|
+ const r = filePath => this.require(filePath)
|
|
|
r.resolve = request => NativeModule._resolveFilename(request, this)
|
|
|
r.main = process.mainModule
|
|
|
r.extensions = moduleExtensions
|
|
|
r.cache = this.cache
|
|
|
|
|
|
- var dirname = path.dirname(filename)
|
|
|
+ const dirname = path.dirname(filename)
|
|
|
|
|
|
// create wrapper function
|
|
|
- var wrapper = NativeModule.wrap(content)
|
|
|
+ const wrapper = NativeModule.wrap(content)
|
|
|
|
|
|
- var options = {
|
|
|
+ const options = {
|
|
|
filename,
|
|
|
displayErrors: true
|
|
|
}
|
|
|
|
|
|
- var compiledWrapper = vm.runInNewContext(wrapper, this.context, options)
|
|
|
+ const compiledWrapper = vm.runInNewContext(wrapper, this.context, options)
|
|
|
return compiledWrapper.call(this.exports, this.exports, r, this, filename, dirname)
|
|
|
}
|
|
|
|
|
|
Module.load = function (id, filename) {
|
|
|
- var m = new Module(id)
|
|
|
+ const m = new Module(id)
|
|
|
filename = filename || id
|
|
|
m.load(filename)
|
|
|
m.run(filename)
|
|
|
@@ -94,22 +91,22 @@ Module.load = function (id, filename) {
|
|
|
}
|
|
|
|
|
|
Module.loadFile = function (file, parent) {
|
|
|
- var filename = NativeModule._resolveFilename(file, parent)
|
|
|
+ const filename = NativeModule._resolveFilename(file, parent)
|
|
|
|
|
|
if (parent) {
|
|
|
- var cachedModule = parent.cache[filename]
|
|
|
+ const cachedModule = parent.cache[filename]
|
|
|
if (cachedModule) return cachedModule.exports
|
|
|
}
|
|
|
|
|
|
- if (parent.isBundle || isNativeModule(filename)) {
|
|
|
+ if (isNativeModule(filename)) {
|
|
|
return require(filename)
|
|
|
}
|
|
|
|
|
|
- var m = new Module(filename, parent)
|
|
|
+ const m = new Module(filename, parent)
|
|
|
|
|
|
m.cache[filename] = m
|
|
|
|
|
|
- var hadException = true
|
|
|
+ let hadException = true
|
|
|
|
|
|
try {
|
|
|
m.load(filename)
|