Skip to content

Commit

Permalink
include compiler in vue-server-renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jul 5, 2016
1 parent 93342d7 commit 5c6c8c7
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 48 deletions.
23 changes: 1 addition & 22 deletions benchmarks/ssr/common.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const compiler = require('../../packages/vue-template-compiler')
const self = (global || root)

self.performance = {
Expand All @@ -26,7 +25,7 @@ function generateGrid (rowCount, columnCount) {

const gridData = generateGrid(1000, 10)

var gridComponent = {
module.exports = {
template: '<div><h1>{{ Math.random() }}</h1><my-table></my-table></div>',
components: {
myTable: {
Expand Down Expand Up @@ -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 changes: 1 addition & 1 deletion build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var builds = [
{
entry: 'src/entries/web-server-renderer.js',
format: 'cjs',
external: ['stream', 'module', 'vm', 'entities'],
external: ['stream', 'module', 'vm', 'entities', 'de-indent'],
out: 'packages/vue-server-renderer/index.js'
}
]
Expand Down
3 changes: 2 additions & 1 deletion build/webpack.ssr.dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = {
alias: alias
},
externals: {
'entities': true
'entities': true,
'de-indent': true
},
module: {
noParse: /run-in-vm/,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"dev:ssr": "webpack --watch --config build/webpack.ssr.dev.config.js",
"dev:compiler": "webpack --watch --config build/webpack.compiler.dev.config.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:unit": "NODE_ENV=development karma start build/karma.unit.config.js",
"test:cover": "NODE_ENV=development karma start build/karma.cover.config.js",
Expand Down
3 changes: 2 additions & 1 deletion packages/vue-server-renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"url": "https://github.com/vuejs/vue/issues"
},
"dependencies": {
"entities": "^1.1.1"
"entities": "^1.1.1",
"de-indent": "^1.0.2"
},
"homepage": "https://github.com/vuejs/vue#readme"
}
22 changes: 19 additions & 3 deletions src/server/render.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow */

import { encodeHTML } from 'entities'
import { compileToFunctions } from 'web/compiler/index'
import { createComponentInstanceForVnode } from 'core/vdom/create-component'

const normalizeAsync = (cache, method) => {
Expand All @@ -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 (
modules: Array<Function>,
directives: Object,
Expand Down Expand Up @@ -77,9 +90,11 @@ export function createRenderFunction (
}

function renderComponent (node, write, next, isRoot) {
const child = createComponentInstanceForVnode(node)._render()
child.parent = node
renderNode(child, write, next, isRoot)
const child = createComponentInstanceForVnode(node)
normalizeRender(child)
const childNode = child._render()
childNode.parent = node
renderNode(childNode, write, next, isRoot)
}

function renderComponentWithCache (node, write, next, isRoot, cache, key) {
Expand Down Expand Up @@ -179,6 +194,7 @@ export function createRenderFunction (
write: (text: string, next: Function) => void,
done: Function
) {
normalizeRender(component)
renderNode(component._render(), write, done, true)
}
}
12 changes: 2 additions & 10 deletions test/ssr/ssr-stream.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Vue from '../../dist/vue.common.js'
import { compileToFunctions } from '../../packages/vue-template-compiler'
import { createRenderer } from '../../packages/vue-server-renderer'
const { renderToStream } = createRenderer()

describe('SSR: renderToStream', () => {
it('should render to a stream', done => {
const stream = renderVmWithOptions({
const stream = renderToStream(new Vue({
template: `
<div>
<p class="hi">yoyo</p>
Expand Down Expand Up @@ -45,7 +44,7 @@ describe('SSR: renderToStream', () => {
}
}
}
})
}))
let res = ''
stream.on('data', chunk => {
res += chunk
Expand Down Expand Up @@ -78,10 +77,3 @@ describe('SSR: renderToStream', () => {
stream.on('data', _ => _)
})
})

function renderVmWithOptions (options) {
const res = compileToFunctions(options.template)
Object.assign(options, res)
delete options.template
return renderToStream(new Vue(options))
}
16 changes: 7 additions & 9 deletions test/ssr/ssr-string.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Vue from '../../dist/vue.common.js'
import { compileToFunctions } from '../../packages/vue-template-compiler'
import { createRenderer } from '../../packages/vue-server-renderer'
const { renderToString } = createRenderer()

Expand Down Expand Up @@ -223,11 +222,13 @@ describe('SSR: renderToString', () => {
`,
components: {
testAsync (resolve) {
const options = compileToFunctions(`
<span class="b">
<test-sub-async></test-sub-async>
</span>
`, { preserveWhitespace: false })
const options = {
template: `
<span class="b">
<test-sub-async></test-sub-async>
</span>
`
}

options.components = {
testSubAsync (resolve) {
Expand Down Expand Up @@ -475,9 +476,6 @@ describe('SSR: renderToString', () => {
})

function renderVmWithOptions (options, cb) {
const res = compileToFunctions(options.template)
Object.assign(options, res)
delete options.template
renderToString(new Vue(options), (err, res) => {
expect(err).toBeNull()
cb(res)
Expand Down

0 comments on commit 5c6c8c7

Please sign in to comment.