Evan You
8 years ago
8 changed files with 174 additions and 11 deletions
@ -0,0 +1,13 @@ |
|||
/* @flow */ |
|||
|
|||
import modules from './server/modules/index' |
|||
import directives from './server/directives/index' |
|||
import { isUnaryTag, canBeLeftOpenTag } from './compiler/util' |
|||
import { createBasicRenderer } from 'server/create-basic-renderer' |
|||
|
|||
export default createBasicRenderer({ |
|||
modules, |
|||
directives, |
|||
isUnaryTag, |
|||
canBeLeftOpenTag |
|||
}) |
@ -0,0 +1,37 @@ |
|||
/* @flow */ |
|||
|
|||
import { createWriteFunction } from './write' |
|||
import { createRenderFunction } from './render' |
|||
import type { RenderOptions } from './create-renderer' |
|||
|
|||
export function createBasicRenderer ({ |
|||
modules = [], |
|||
directives = {}, |
|||
isUnaryTag = (() => false), |
|||
cache |
|||
}: RenderOptions = {}) { |
|||
const render = createRenderFunction(modules, directives, isUnaryTag, cache) |
|||
|
|||
return function renderToString ( |
|||
component: Component, |
|||
context: any, |
|||
done: any |
|||
): void { |
|||
if (typeof context === 'function') { |
|||
done = context |
|||
context = {} |
|||
} |
|||
let result = '' |
|||
const write = createWriteFunction(text => { |
|||
result += text |
|||
return false |
|||
}, done) |
|||
try { |
|||
render(component, write, context, () => { |
|||
done(null, result) |
|||
}) |
|||
} catch (e) { |
|||
done(e) |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,53 @@ |
|||
import Vue from '../../dist/vue.runtime.common.js' |
|||
import renderToString from '../../packages/vue-server-renderer/basic' |
|||
|
|||
describe('SSR: basicRenderer', () => { |
|||
it('should work', done => { |
|||
renderToString(new Vue({ |
|||
template: ` |
|||
<div> |
|||
<p class="hi">yoyo</p> |
|||
<div id="ho" :class="{ red: isRed }"></div> |
|||
<span>{{ test }}</span> |
|||
<input :value="test"> |
|||
<img :src="imageUrl"> |
|||
<test></test> |
|||
<test-async></test-async> |
|||
</div> |
|||
`,
|
|||
data: { |
|||
test: 'hi', |
|||
isRed: true, |
|||
imageUrl: 'https://vuejs.org/images/logo.png' |
|||
}, |
|||
components: { |
|||
test: { |
|||
render () { |
|||
return this.$createElement('div', { class: ['a'] }, 'test') |
|||
} |
|||
}, |
|||
testAsync (resolve) { |
|||
resolve({ |
|||
render () { |
|||
return this.$createElement('span', { class: ['b'] }, 'testAsync') |
|||
} |
|||
}) |
|||
} |
|||
} |
|||
}), (err, result) => { |
|||
expect(err).toBeNull() |
|||
expect(result).toContain( |
|||
'<div data-server-rendered="true">' + |
|||
'<p class="hi">yoyo</p> ' + |
|||
'<div id="ho" class="red"></div> ' + |
|||
'<span>hi</span> ' + |
|||
'<input value="hi"> ' + |
|||
'<img src="https://vuejs.org/images/logo.png"> ' + |
|||
'<div class="a">test</div> ' + |
|||
'<span class="b">testAsync</span>' + |
|||
'</div>' |
|||
) |
|||
done() |
|||
}) |
|||
}) |
|||
}) |
Loading…
Reference in new issue