Browse Source

include compiler in vue-server-renderer

dev
Evan You 9 years ago
parent
commit
5c6c8c7ed0
  1. 23
      benchmarks/ssr/common.js
  2. 2
      build/build.js
  3. 3
      build/webpack.ssr.dev.config.js
  4. 2
      package.json
  5. 3
      packages/vue-server-renderer/package.json
  6. 22
      src/server/render.js
  7. 12
      test/ssr/ssr-stream.spec.js
  8. 16
      test/ssr/ssr-string.spec.js

23
benchmarks/ssr/common.js

@ -1,6 +1,5 @@
'use strict' 'use strict'
const compiler = require('../../packages/vue-template-compiler')
const self = (global || root) const self = (global || root)
self.performance = { self.performance = {
@ -26,7 +25,7 @@ function generateGrid (rowCount, columnCount) {
const gridData = generateGrid(1000, 10) const gridData = generateGrid(1000, 10)
var gridComponent = { module.exports = {
template: '<div><h1>{{ Math.random() }}</h1><my-table></my-table></div>', template: '<div><h1>{{ Math.random() }}</h1><my-table></my-table></div>',
components: { components: {
myTable: { myTable: {
@ -58,23 +57,3 @@ var gridComponent = {
} }
} }
} }
function createCompiledOptions (options) {
const res = compiler.compileToFunctions(options.template, {
preserveWhitespace: false
})
Object.assign(options, res)
delete options.template
if (options.components) {
const keys = Object.keys(options.components)
let total = keys.length
while (total) {
const name = keys[total - 1]
options.components[name] = createCompiledOptions(options.components[name])
total--
}
}
return options
}
module.exports = createCompiledOptions(gridComponent)

2
build/build.js

@ -72,7 +72,7 @@ var builds = [
{ {
entry: 'src/entries/web-server-renderer.js', entry: 'src/entries/web-server-renderer.js',
format: 'cjs', format: 'cjs',
external: ['stream', 'module', 'vm', 'entities'], external: ['stream', 'module', 'vm', 'entities', 'de-indent'],
out: 'packages/vue-server-renderer/index.js' out: 'packages/vue-server-renderer/index.js'
} }
] ]

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

@ -13,7 +13,8 @@ module.exports = {
alias: alias alias: alias
}, },
externals: { externals: {
'entities': true 'entities': true,
'de-indent': true
}, },
module: { module: {
noParse: /run-in-vm/, noParse: /run-in-vm/,

2
package.json

@ -15,7 +15,7 @@
"dev:ssr": "webpack --watch --config build/webpack.ssr.dev.config.js", "dev:ssr": "webpack --watch --config build/webpack.ssr.dev.config.js",
"dev:compiler": "webpack --watch --config build/webpack.compiler.dev.config.js", "dev:compiler": "webpack --watch --config build/webpack.compiler.dev.config.js",
"build": "NODE_ENV=production node build/build.js", "build": "NODE_ENV=production node build/build.js",
"build:ssr": "npm run build -- vue.common.js,vue-template-compiler,vue-server-renderer", "build:ssr": "npm run build -- vue.common.js,vue-server-renderer",
"test": "npm run lint && flow check && npm run test:cover && npm run test:e2e -- --env phantomjs && npm run test:ssr", "test": "npm run lint && flow check && npm run test:cover && npm run test:e2e -- --env phantomjs && npm run test:ssr",
"test:unit": "NODE_ENV=development karma start build/karma.unit.config.js", "test:unit": "NODE_ENV=development karma start build/karma.unit.config.js",
"test:cover": "NODE_ENV=development karma start build/karma.cover.config.js", "test:cover": "NODE_ENV=development karma start build/karma.cover.config.js",

3
packages/vue-server-renderer/package.json

@ -18,7 +18,8 @@
"url": "https://github.com/vuejs/vue/issues" "url": "https://github.com/vuejs/vue/issues"
}, },
"dependencies": { "dependencies": {
"entities": "^1.1.1" "entities": "^1.1.1",
"de-indent": "^1.0.2"
}, },
"homepage": "https://github.com/vuejs/vue#readme" "homepage": "https://github.com/vuejs/vue#readme"
} }

22
src/server/render.js

@ -1,6 +1,7 @@
/* @flow */ /* @flow */
import { encodeHTML } from 'entities' import { encodeHTML } from 'entities'
import { compileToFunctions } from 'web/compiler/index'
import { createComponentInstanceForVnode } from 'core/vdom/create-component' import { createComponentInstanceForVnode } from 'core/vdom/create-component'
const normalizeAsync = (cache, method) => { const normalizeAsync = (cache, method) => {
@ -14,6 +15,18 @@ const normalizeAsync = (cache, method) => {
} }
} }
const compilationCache = Object.create(null)
const normalizeRender = vm => {
const { render, template } = vm.$options
if (!render && template) {
const renderFns = (
compilationCache[template] ||
(compilationCache[template] = compileToFunctions(template))
)
Object.assign(vm.$options, renderFns)
}
}
export function createRenderFunction ( export function createRenderFunction (
modules: Array<Function>, modules: Array<Function>,
directives: Object, directives: Object,
@ -77,9 +90,11 @@ export function createRenderFunction (
} }
function renderComponent (node, write, next, isRoot) { function renderComponent (node, write, next, isRoot) {
const child = createComponentInstanceForVnode(node)._render() const child = createComponentInstanceForVnode(node)
child.parent = node normalizeRender(child)
renderNode(child, write, next, isRoot) const childNode = child._render()
childNode.parent = node
renderNode(childNode, write, next, isRoot)
} }
function renderComponentWithCache (node, write, next, isRoot, cache, key) { function renderComponentWithCache (node, write, next, isRoot, cache, key) {
@ -179,6 +194,7 @@ export function createRenderFunction (
write: (text: string, next: Function) => void, write: (text: string, next: Function) => void,
done: Function done: Function
) { ) {
normalizeRender(component)
renderNode(component._render(), write, done, true) renderNode(component._render(), write, done, true)
} }
} }

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

@ -1,11 +1,10 @@
import Vue from '../../dist/vue.common.js' 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() const { renderToStream } = createRenderer()
describe('SSR: renderToStream', () => { describe('SSR: renderToStream', () => {
it('should render to a stream', done => { it('should render to a stream', done => {
const stream = renderVmWithOptions({ const stream = renderToStream(new Vue({
template: ` template: `
<div> <div>
<p class="hi">yoyo</p> <p class="hi">yoyo</p>
@ -45,7 +44,7 @@ describe('SSR: renderToStream', () => {
} }
} }
} }
}) }))
let res = '' let res = ''
stream.on('data', chunk => { stream.on('data', chunk => {
res += chunk res += chunk
@ -78,10 +77,3 @@ describe('SSR: renderToStream', () => {
stream.on('data', _ => _) stream.on('data', _ => _)
}) })
}) })
function renderVmWithOptions (options) {
const res = compileToFunctions(options.template)
Object.assign(options, res)
delete options.template
return renderToStream(new Vue(options))
}

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

@ -1,5 +1,4 @@
import Vue from '../../dist/vue.common.js' 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() const { renderToString } = createRenderer()
@ -223,11 +222,13 @@ describe('SSR: renderToString', () => {
`, `,
components: { components: {
testAsync (resolve) { testAsync (resolve) {
const options = compileToFunctions(` const options = {
<span class="b"> template: `
<test-sub-async></test-sub-async> <span class="b">
</span> <test-sub-async></test-sub-async>
`, { preserveWhitespace: false }) </span>
`
}
options.components = { options.components = {
testSubAsync (resolve) { testSubAsync (resolve) {
@ -475,9 +476,6 @@ describe('SSR: renderToString', () => {
}) })
function renderVmWithOptions (options, cb) { function renderVmWithOptions (options, cb) {
const res = compileToFunctions(options.template)
Object.assign(options, res)
delete options.template
renderToString(new Vue(options), (err, res) => { renderToString(new Vue(options), (err, res) => {
expect(err).toBeNull() expect(err).toBeNull()
cb(res) cb(res)

Loading…
Cancel
Save