mirror of
https://gitee.com/vuejs/vue.git
synced 2024-12-02 12:07:39 +08:00
make ssr files compatible with older node versions
This commit is contained in:
parent
4cf710e56d
commit
175282ea05
@ -1,10 +1,9 @@
|
||||
const { readFileSync } = require('fs')
|
||||
const createRenderer = require('./create-renderer')
|
||||
const Module = require('./module')
|
||||
var createRenderer = require('./create-renderer')
|
||||
var Module = require('./module')
|
||||
|
||||
function runAsNewModule (code) {
|
||||
const path = '__app__'
|
||||
const m = new Module(path, null, true /* isBundle */)
|
||||
var path = '__app__'
|
||||
var m = new Module(path, null, true /* isBundle */)
|
||||
m.load(path)
|
||||
m._compile(code, path)
|
||||
return Object.prototype.hasOwnProperty.call(m.exports, 'default')
|
||||
@ -15,14 +14,14 @@ function runAsNewModule (code) {
|
||||
exports.createRenderer = createRenderer
|
||||
|
||||
exports.createBundleRenderer = function (code, options) {
|
||||
const renderer = createRenderer(options)
|
||||
var renderer = createRenderer(options)
|
||||
return {
|
||||
renderToString (cb) {
|
||||
const app = runAsNewModule(code)
|
||||
renderToString: function (cb) {
|
||||
var app = runAsNewModule(code)
|
||||
renderer.renderToString(app, cb)
|
||||
},
|
||||
renderToStream () {
|
||||
const app = runAsNewModule(code)
|
||||
renderToStream: function () {
|
||||
var app = runAsNewModule(code)
|
||||
return renderer.renderToStream(app)
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
// thanks to airbnb/hypernova
|
||||
|
||||
const NativeModule = require('module')
|
||||
const path = require('path')
|
||||
const { ok } = require('assert')
|
||||
const { runInNewContext } = require('vm')
|
||||
var NativeModule = require('module')
|
||||
var path = require('path')
|
||||
var assert = require('assert')
|
||||
var vm = require('vm')
|
||||
|
||||
const NativeModules = process.binding('natives')
|
||||
var NativeModules = process.binding('natives')
|
||||
|
||||
const moduleExtensions = Object.assign({}, NativeModule._extensions)
|
||||
var moduleExtensions = Object.assign({}, NativeModule._extensions)
|
||||
|
||||
function isNativeModule (id) {
|
||||
return Object.prototype.hasOwnProperty.call(NativeModules, id)
|
||||
@ -15,7 +15,7 @@ function isNativeModule (id) {
|
||||
|
||||
// Creates a sandbox so we don't share globals across different runs.
|
||||
function createContext () {
|
||||
const sandbox = {
|
||||
var sandbox = {
|
||||
Buffer,
|
||||
clearImmediate,
|
||||
clearInterval,
|
||||
@ -30,105 +30,102 @@ function createContext () {
|
||||
return sandbox
|
||||
}
|
||||
|
||||
// This class should satisfy the Module interface that NodeJS defines in their native m.js
|
||||
// implementation.
|
||||
class Module {
|
||||
constructor (id, parent, isBundle) {
|
||||
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
|
||||
function Module (id, parent, isBundle) {
|
||||
var 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
|
||||
}
|
||||
|
||||
Module.prototype.load = function (filename) {
|
||||
assert.ok(!this.loaded)
|
||||
this.filename = filename
|
||||
this.paths = NativeModule._nodeModulePaths(path.dirname(filename))
|
||||
}
|
||||
|
||||
Module.prototype.run = function (filename) {
|
||||
var ext = path.extname(filename)
|
||||
var extension = moduleExtensions[ext] ? ext : '.js'
|
||||
moduleExtensions[extension](this, filename)
|
||||
this.loaded = true
|
||||
}
|
||||
|
||||
Module.prototype.require = function (filePath) {
|
||||
assert.ok(typeof filePath === 'string', 'path must be a string')
|
||||
return Module.loadFile(filePath, this)
|
||||
}
|
||||
|
||||
Module.prototype._compile = function (content, filename) {
|
||||
var self = this
|
||||
|
||||
function r (filePath) {
|
||||
return self.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)
|
||||
|
||||
// create wrapper function
|
||||
var wrapper = NativeModule.wrap(content)
|
||||
|
||||
var options = {
|
||||
filename,
|
||||
displayErrors: true
|
||||
}
|
||||
|
||||
load (filename) {
|
||||
ok(!this.loaded)
|
||||
this.filename = filename
|
||||
this.paths = NativeModule._nodeModulePaths(path.dirname(filename))
|
||||
var 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)
|
||||
filename = filename || id
|
||||
m.load(filename)
|
||||
m.run(filename)
|
||||
return m
|
||||
}
|
||||
|
||||
Module.loadFile = function (file, parent) {
|
||||
var filename = NativeModule._resolveFilename(file, parent)
|
||||
|
||||
if (parent) {
|
||||
var cachedModule = parent.cache[filename]
|
||||
if (cachedModule) return cachedModule.exports
|
||||
}
|
||||
|
||||
run (filename) {
|
||||
const ext = path.extname(filename)
|
||||
const extension = moduleExtensions[ext] ? ext : '.js'
|
||||
moduleExtensions[extension](this, filename)
|
||||
this.loaded = true
|
||||
if (parent.isBundle || isNativeModule(filename)) {
|
||||
return require(filename)
|
||||
}
|
||||
|
||||
require (filePath) {
|
||||
ok(typeof filePath === 'string', 'path must be a string')
|
||||
return Module.loadFile(filePath, this)
|
||||
}
|
||||
var m = new Module(filename, parent)
|
||||
|
||||
_compile (content, filename) {
|
||||
const self = this
|
||||
m.cache[filename] = m
|
||||
|
||||
function r (filePath) {
|
||||
return self.require(filePath)
|
||||
}
|
||||
r.resolve = request => NativeModule._resolveFilename(request, this)
|
||||
r.main = process.mainModule
|
||||
r.extensions = moduleExtensions
|
||||
r.cache = this.cache
|
||||
var hadException = true
|
||||
|
||||
const dirname = path.dirname(filename)
|
||||
|
||||
// create wrapper function
|
||||
const wrapper = NativeModule.wrap(content)
|
||||
|
||||
const options = {
|
||||
filename,
|
||||
displayErrors: true
|
||||
}
|
||||
|
||||
const compiledWrapper = runInNewContext(wrapper, this.context, options)
|
||||
return compiledWrapper.call(this.exports, this.exports, r, this, filename, dirname)
|
||||
}
|
||||
|
||||
static load (id, filename = id) {
|
||||
const m = new Module(id)
|
||||
try {
|
||||
m.load(filename)
|
||||
m.run(filename)
|
||||
return m
|
||||
hadException = false
|
||||
} finally {
|
||||
if (hadException) {
|
||||
delete m.cache[filename]
|
||||
}
|
||||
}
|
||||
|
||||
static loadFile (file, parent) {
|
||||
const filename = NativeModule._resolveFilename(file, parent)
|
||||
return m.exports
|
||||
}
|
||||
|
||||
if (parent) {
|
||||
const cachedModule = parent.cache[filename]
|
||||
if (cachedModule) return cachedModule.exports
|
||||
}
|
||||
|
||||
if (parent.isBundle || isNativeModule(filename)) {
|
||||
return require(filename)
|
||||
}
|
||||
|
||||
const m = new Module(filename, parent)
|
||||
|
||||
m.cache[filename] = m
|
||||
|
||||
let hadException = true
|
||||
|
||||
try {
|
||||
m.load(filename)
|
||||
m.run(filename)
|
||||
hadException = false
|
||||
} finally {
|
||||
if (hadException) {
|
||||
delete m.cache[filename]
|
||||
}
|
||||
}
|
||||
|
||||
return m.exports
|
||||
}
|
||||
|
||||
static addExtension (ext, f) {
|
||||
moduleExtensions[ext] = f
|
||||
}
|
||||
Module.addExtension = function (ext, f) {
|
||||
moduleExtensions[ext] = f
|
||||
}
|
||||
|
||||
module.exports = Module
|
||||
|
Loading…
Reference in New Issue
Block a user