update ssr package

This commit is contained in:
Evan You 2016-06-25 00:05:39 -04:00
parent 646061fda5
commit 4cf710e56d
10 changed files with 3195 additions and 3010 deletions

View File

@ -1,7 +1,7 @@
'use strict'
const Vue = require('../../dist/vue.common.js')
const createRenderer = require('../../packages/vue-server-renderer')
const createRenderer = require('../../packages/vue-server-renderer').createRenderer
const renderToStream = createRenderer().renderToStream
const gridComponent = require('./common.js')

View File

@ -1,7 +1,7 @@
'use strict'
const Vue = require('../../dist/vue.common.js')
const createRenderer = require('../../packages/vue-server-renderer')
const createRenderer = require('../../packages/vue-server-renderer').createRenderer
const renderToString = createRenderer().renderToString
const gridComponent = require('./common.js')

View File

@ -73,7 +73,7 @@ var builds = [
entry: 'src/entries/web-server-renderer.js',
format: 'cjs',
external: ['stream', 'entities'],
out: 'packages/vue-server-renderer/index.js'
out: 'packages/vue-server-renderer/create-renderer.js'
}
]

View File

@ -2,11 +2,12 @@ var path = require('path')
var alias = require('./alias')
module.exports = {
target: 'node',
entry: path.resolve(__dirname, 'webpack.ssr.dev.entry.js'),
target: 'node',
output: {
path: path.resolve(__dirname, '../packages/vue-server-renderer'),
filename: 'index.js',
filename: 'create-renderer.js',
libraryTarget: 'commonjs2'
},
resolve: {

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,134 @@
// thanks to airbnb/hypernova
const NativeModule = require('module')
const path = require('path')
const { ok } = require('assert')
const { runInNewContext } = 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 () {
const sandbox = {
Buffer,
clearImmediate,
clearInterval,
clearTimeout,
setImmediate,
setInterval,
setTimeout,
console,
process
}
sandbox.global = sandbox
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
}
load (filename) {
ok(!this.loaded)
this.filename = filename
this.paths = NativeModule._nodeModulePaths(path.dirname(filename))
}
run (filename) {
const ext = path.extname(filename)
const extension = moduleExtensions[ext] ? ext : '.js'
moduleExtensions[extension](this, filename)
this.loaded = true
}
require (filePath) {
ok(typeof filePath === 'string', 'path must be a string')
return Module.loadFile(filePath, this)
}
_compile (content, filename) {
const 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
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)
m.load(filename)
m.run(filename)
return m
}
static loadFile (file, parent) {
const filename = NativeModule._resolveFilename(file, parent)
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.exports = Module

View File

@ -1,5 +1,5 @@
import Vue from '../../dist/vue.common.js'
import createRenderer from '../../packages/vue-server-renderer'
import { createRenderer } from '../../packages/vue-server-renderer'
import '../helpers/to-have-been-warned.js'
describe('SSR: VUE_ENV=server', () => {

View File

@ -1,6 +1,6 @@
import Vue from '../../dist/vue.common.js'
import { compileToFunctions } from '../../packages/vue-template-compiler'
import createRenderer from '../../packages/vue-server-renderer'
import { createRenderer } from '../../packages/vue-server-renderer'
const { renderToStream } = createRenderer()
describe('SSR: renderToStream', () => {

View File

@ -1,6 +1,6 @@
import Vue from '../../dist/vue.common.js'
import { compileToFunctions } from '../../packages/vue-template-compiler'
import createRenderer from '../../packages/vue-server-renderer'
import { createRenderer } from '../../packages/vue-server-renderer'
const { renderToString } = createRenderer()
describe('SSR: renderToString', () => {