mirror of
https://gitee.com/vuejs/vue.git
synced 2024-12-02 20:17:52 +08:00
move bundle renderer into src
This commit is contained in:
parent
36e63c234a
commit
5854f0f781
@ -72,8 +72,8 @@ var builds = [
|
||||
{
|
||||
entry: 'src/entries/web-server-renderer.js',
|
||||
format: 'cjs',
|
||||
external: ['stream', 'entities', 'lru-cache'],
|
||||
out: 'packages/vue-server-renderer/create-renderer.js'
|
||||
external: ['stream', 'module', 'vm', 'entities', 'lru-cache'],
|
||||
out: 'packages/vue-server-renderer/index.js'
|
||||
}
|
||||
]
|
||||
|
||||
|
@ -3,10 +3,10 @@ var alias = require('./alias')
|
||||
|
||||
module.exports = {
|
||||
target: 'node',
|
||||
entry: path.resolve(__dirname, 'webpack.ssr.dev.entry.js'),
|
||||
entry: path.resolve(__dirname, '../src/entries/web-server-renderer'),
|
||||
output: {
|
||||
path: path.resolve(__dirname, '../packages/vue-server-renderer'),
|
||||
filename: 'create-renderer.js',
|
||||
filename: 'index.js',
|
||||
libraryTarget: 'commonjs2'
|
||||
},
|
||||
resolve: {
|
||||
@ -17,6 +17,7 @@ module.exports = {
|
||||
'lru-cache': true
|
||||
},
|
||||
module: {
|
||||
noParse: /run-in-vm/,
|
||||
loaders: [
|
||||
{
|
||||
test: /\.js/,
|
||||
|
@ -1 +0,0 @@
|
||||
module.exports = require('../src/entries/web-server-renderer')['default']
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,128 +0,0 @@
|
||||
// thanks to airbnb/hypernova
|
||||
'use strict'
|
||||
|
||||
const NativeModule = require('module')
|
||||
const path = require('path')
|
||||
const assert = require('assert')
|
||||
const vm = require('vm')
|
||||
|
||||
const NativeModules = process.binding('natives')
|
||||
|
||||
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 (context) {
|
||||
const sandbox = {
|
||||
Buffer,
|
||||
clearImmediate,
|
||||
clearInterval,
|
||||
clearTimeout,
|
||||
setImmediate,
|
||||
setInterval,
|
||||
setTimeout,
|
||||
console,
|
||||
process,
|
||||
__VUE_SSR_CONTEXT__: context || {}
|
||||
}
|
||||
sandbox.global = sandbox
|
||||
return sandbox
|
||||
}
|
||||
|
||||
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(context)
|
||||
}
|
||||
|
||||
Module.prototype.load = function (filename) {
|
||||
assert.ok(!this.loaded)
|
||||
this.filename = filename
|
||||
this.paths = NativeModule._nodeModulePaths(path.dirname(filename))
|
||||
}
|
||||
|
||||
Module.prototype.run = function (filename) {
|
||||
const ext = path.extname(filename)
|
||||
const 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) {
|
||||
const r = filePath => this.require(filePath)
|
||||
r.resolve = request => NativeModule._resolveFilename(request, this)
|
||||
r.main = process.mainModule
|
||||
r.extensions = moduleExtensions
|
||||
r.cache = this.cache
|
||||
|
||||
const dirname = path.dirname(filename)
|
||||
|
||||
// create wrapper function
|
||||
const wrapper = NativeModule.wrap(content)
|
||||
|
||||
const options = {
|
||||
filename,
|
||||
displayErrors: true
|
||||
}
|
||||
|
||||
const compiledWrapper = vm.runInNewContext(wrapper, this.context, options)
|
||||
return compiledWrapper.call(this.exports, this.exports, r, this, filename, dirname)
|
||||
}
|
||||
|
||||
Module.load = function (id, filename) {
|
||||
const m = new Module(id)
|
||||
filename = filename || id
|
||||
m.load(filename)
|
||||
m.run(filename)
|
||||
return m
|
||||
}
|
||||
|
||||
Module.loadFile = function (file, parent) {
|
||||
const filename = NativeModule._resolveFilename(file, parent)
|
||||
|
||||
if (parent) {
|
||||
const cachedModule = parent.cache[filename]
|
||||
if (cachedModule) return cachedModule.exports
|
||||
}
|
||||
|
||||
if (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
|
||||
}
|
||||
|
||||
Module.addExtension = function (ext, f) {
|
||||
moduleExtensions[ext] = f
|
||||
}
|
||||
|
||||
module.exports = Module
|
@ -1,21 +1,24 @@
|
||||
/* @flow */
|
||||
|
||||
import { createRenderer } from 'server/create-renderer'
|
||||
import { createRenderer as _createRenderer } from 'server/create-renderer'
|
||||
import { createBundleRendererCreator } from 'server/create-bundle-renderer'
|
||||
import { isUnaryTag } from 'web/util/index'
|
||||
import modules from 'web/server/modules/index'
|
||||
import baseDirectives from 'web/server/directives/index'
|
||||
|
||||
export default function publicCreateRenderer (options?: Object = {}): {
|
||||
export function createRenderer (options?: Object = {}): {
|
||||
renderToString: Function,
|
||||
renderToStream: Function
|
||||
} {
|
||||
// user can provide server-side implementations for custom directives
|
||||
// when creating the renderer.
|
||||
const directives = Object.assign(baseDirectives, options.directives)
|
||||
return createRenderer({
|
||||
return _createRenderer({
|
||||
isUnaryTag,
|
||||
modules,
|
||||
directives,
|
||||
cache: options.cache || {}
|
||||
})
|
||||
}
|
||||
|
||||
export const createBundleRenderer = createBundleRendererCreator(createRenderer)
|
||||
|
22
src/server/create-bundle-renderer.js
Normal file
22
src/server/create-bundle-renderer.js
Normal file
@ -0,0 +1,22 @@
|
||||
import runInVm from './run-in-vm'
|
||||
import { PassThrough } from 'stream'
|
||||
|
||||
export function createBundleRendererCreator (createRenderer) {
|
||||
return (code, rendererOptions) => {
|
||||
const renderer = createRenderer(rendererOptions)
|
||||
return {
|
||||
renderToString: (context, cb) => {
|
||||
runInVm(code, context).then(app => {
|
||||
renderer.renderToString(app, cb)
|
||||
})
|
||||
},
|
||||
renderToStream: (context) => {
|
||||
const res = new PassThrough()
|
||||
runInVm(code, context).then(app => {
|
||||
renderer.renderToStream(app).pipe(res)
|
||||
})
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
37
src/server/run-in-vm.js
Normal file
37
src/server/run-in-vm.js
Normal file
@ -0,0 +1,37 @@
|
||||
import NativeModule from 'module'
|
||||
import vm from 'vm'
|
||||
|
||||
function createContext (context) {
|
||||
const sandbox = {
|
||||
Buffer,
|
||||
clearImmediate,
|
||||
clearInterval,
|
||||
clearTimeout,
|
||||
setImmediate,
|
||||
setInterval,
|
||||
setTimeout,
|
||||
console,
|
||||
process,
|
||||
__VUE_SSR_CONTEXT__: context || {}
|
||||
}
|
||||
sandbox.global = sandbox
|
||||
return sandbox
|
||||
}
|
||||
|
||||
export default function runInVm (code, _context) {
|
||||
const wrapper = NativeModule.wrap(code)
|
||||
const context = createContext(_context)
|
||||
const compiledWrapper = vm.runInNewContext(wrapper, context, {
|
||||
filename: '__vue_ssr_bundle__',
|
||||
displayErrors: true
|
||||
})
|
||||
const m = { exports: {}}
|
||||
compiledWrapper.call(m.exports, m.exports, require, m)
|
||||
const res = Object.prototype.hasOwnProperty.call(m.exports, 'default')
|
||||
? m.exports.default
|
||||
: m
|
||||
if (typeof res.then !== 'function') {
|
||||
throw new Error('SSR bundle should export a Promise.')
|
||||
}
|
||||
return res
|
||||
}
|
Loading…
Reference in New Issue
Block a user