directMode -> runInNewContext

This commit is contained in:
Evan You 2017-04-16 20:04:42 +08:00
parent 086ae4469b
commit 745fcd6d78
5 changed files with 26 additions and 23 deletions

View File

@ -34,7 +34,7 @@ export function createBundleRendererCreator (createRenderer: () => Renderer) {
) {
let files, entry, maps
let basedir = rendererOptions.basedir
const direct = rendererOptions.directMode
const runInNewContext = rendererOptions.runInNewContext !== false
// load bundle if given filepath
if (
@ -76,7 +76,7 @@ export function createBundleRendererCreator (createRenderer: () => Renderer) {
const renderer = createRenderer(rendererOptions)
const run = createBundleRunner(entry, files, basedir, direct)
const run = createBundleRunner(entry, files, basedir, runInNewContext)
return {
renderToString: (context?: Object, cb: (err: ?Error, res: ?string) => void) => {

View File

@ -86,10 +86,10 @@ function deepClone (val) {
}
}
export function createBundleRunner (entry, files, basedir, direct) {
export function createBundleRunner (entry, files, basedir, runInNewContext) {
const evaluate = compileModule(files, basedir)
if (!direct) {
// default mode: creates a fresh context and re-evaluate the bundle
if (runInNewContext) {
// new context mode: creates a fresh context and re-evaluate the bundle
// on each render. Ensures entire application state is fresh for each
// render, but incurs extra evaluation cost.
return (userContext = {}) => new Promise(resolve => {
@ -109,7 +109,10 @@ export function createBundleRunner (entry, files, basedir, direct) {
if (!runner) {
runner = evaluate(entry, sharedContext)
if (typeof runner !== 'function') {
throw new Error('direct mode expects bundle export to be a function.')
throw new Error(
'bundle export should be a function when using ' +
'{ runInNewContext: false }.'
)
}
}
sharedContext.__VUE_SSR_CONTEXT__ = userContext

View File

@ -24,9 +24,9 @@ export type RenderOptions = {
cache?: RenderCache;
template?: string;
basedir?: string;
directMode?: boolean;
shouldPreload?: Function;
clientManifest?: ClientManifest;
runInNewContext?: boolean;
};
export function createRenderer ({

View File

@ -37,9 +37,9 @@ describe('SSR: bundle renderer', () => {
createAssertions(false)
})
function createAssertions (directMode) {
function createAssertions (runInNewContext) {
it('renderToString', done => {
createRenderer('app.js', { directMode }, renderer => {
createRenderer('app.js', { runInNewContext }, renderer => {
const context = { url: '/test' }
renderer.renderToString(context, (err, res) => {
expect(err).toBeNull()
@ -51,7 +51,7 @@ function createAssertions (directMode) {
})
it('renderToStream', done => {
createRenderer('app.js', { directMode }, renderer => {
createRenderer('app.js', { runInNewContext }, renderer => {
const context = { url: '/test' }
const stream = renderer.renderToStream(context)
let res = ''
@ -67,7 +67,7 @@ function createAssertions (directMode) {
})
it('renderToString catch error', done => {
createRenderer('error.js', { directMode }, renderer => {
createRenderer('error.js', { runInNewContext }, renderer => {
renderer.renderToString(err => {
expect(err.message).toBe('foo')
done()
@ -76,7 +76,7 @@ function createAssertions (directMode) {
})
it('renderToStream catch error', done => {
createRenderer('error.js', { directMode }, renderer => {
createRenderer('error.js', { runInNewContext }, renderer => {
const stream = renderer.renderToStream()
stream.on('error', err => {
expect(err.message).toBe('foo')
@ -90,7 +90,7 @@ function createAssertions (directMode) {
const get = jasmine.createSpy('get')
const set = jasmine.createSpy('set')
const options = {
directMode,
runInNewContext,
cache: {
// async
get: (key, cb) => {
@ -133,7 +133,7 @@ function createAssertions (directMode) {
const get = jasmine.createSpy('get')
const set = jasmine.createSpy('set')
const options = {
directMode,
runInNewContext,
cache: {
// async
has: (key, cb) => {
@ -181,7 +181,7 @@ function createAssertions (directMode) {
spyOn(cache, 'set').and.callThrough()
const options = {
cache,
directMode
runInNewContext
}
createRenderer('nested-cache.js', options, renderer => {
const expected = '<div data-server-rendered="true">/test</div>'
@ -213,7 +213,7 @@ function createAssertions (directMode) {
})
it('renderToString (bundle format with code split)', done => {
createRenderer('split.js', { directMode, asBundle: true }, renderer => {
createRenderer('split.js', { runInNewContext, asBundle: true }, renderer => {
const context = { url: '/test' }
renderer.renderToString(context, (err, res) => {
expect(err).toBeNull()
@ -224,7 +224,7 @@ function createAssertions (directMode) {
})
it('renderToStream (bundle format with code split)', done => {
createRenderer('split.js', { directMode, asBundle: true }, renderer => {
createRenderer('split.js', { runInNewContext, asBundle: true }, renderer => {
const context = { url: '/test' }
const stream = renderer.renderToStream(context)
let res = ''
@ -239,7 +239,7 @@ function createAssertions (directMode) {
})
it('renderToString catch error (bundle format with source map)', done => {
createRenderer('error.js', { directMode, asBundle: true }, renderer => {
createRenderer('error.js', { runInNewContext, asBundle: true }, renderer => {
renderer.renderToString(err => {
expect(err.stack).toContain('test/ssr/fixtures/error.js:1:6')
expect(err.message).toBe('foo')
@ -249,7 +249,7 @@ function createAssertions (directMode) {
})
it('renderToString catch error (bundle format with source map)', done => {
createRenderer('error.js', { directMode, asBundle: true }, renderer => {
createRenderer('error.js', { runInNewContext, asBundle: true }, renderer => {
const stream = renderer.renderToStream()
stream.on('error', err => {
expect(err.stack).toContain('test/ssr/fixtures/error.js:1:6')

View File

@ -172,9 +172,9 @@ describe('SSR: template option', () => {
createClientManifestAssertions(true)
createClientManifestAssertions(false)
function createClientManifestAssertions (directMode) {
function createClientManifestAssertions (runInNewContext) {
it('bundleRenderer + renderToString + clientManifest', done => {
createRendererWithManifest('split.js', { directMode }, renderer => {
createRendererWithManifest('split.js', { runInNewContext }, renderer => {
renderer.renderToString({}, (err, res) => {
expect(err).toBeNull()
expect(res).toContain(expectedHTMLWithManifest(false))
@ -185,7 +185,7 @@ describe('SSR: template option', () => {
it('bundleRenderer + renderToStream + clientManifest + shouldPreload', done => {
createRendererWithManifest('split.js', {
directMode,
runInNewContext,
shouldPreload: (file, type) => {
if (type === 'image' || type === 'script' || type === 'font') {
return true
@ -206,7 +206,7 @@ describe('SSR: template option', () => {
it('bundleRenderer + renderToString + clientManifest + no template', done => {
createRendererWithManifest('split.js', {
directMode,
runInNewContext,
template: null
}, renderer => {
const context = {}