Browse Source

update ssr package

dev
Evan You 9 years ago
parent
commit
4cf710e56d
  1. 2
      benchmarks/ssr/renderToStream.js
  2. 2
      benchmarks/ssr/renderToString.js
  3. 2
      build/build.js
  4. 3
      build/webpack.ssr.dev.config.js
  5. 3031
      packages/vue-server-renderer/create-renderer.js
  6. 3021
      packages/vue-server-renderer/index.js
  7. 134
      packages/vue-server-renderer/module.js
  8. 2
      test/ssr/ssr-env.spec.js
  9. 2
      test/ssr/ssr-stream.spec.js
  10. 2
      test/ssr/ssr-string.spec.js

2
benchmarks/ssr/renderToStream.js

@ -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')

2
benchmarks/ssr/renderToString.js

@ -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')

2
build/build.js

@ -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'
}
]

3
build/webpack.ssr.dev.config.js

@ -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: {

3031
packages/vue-server-renderer/create-renderer.js

File diff suppressed because it is too large

3021
packages/vue-server-renderer/index.js

File diff suppressed because it is too large

134
packages/vue-server-renderer/module.js

@ -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

2
test/ssr/ssr-env.spec.js

@ -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', () => {

2
test/ssr/ssr-stream.spec.js

@ -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', () => {

2
test/ssr/ssr-string.spec.js

@ -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', () => {

Loading…
Cancel
Save