From 745fcd6d789e68f0afc70f1ae926ac158d6a441c Mon Sep 17 00:00:00 2001 From: Evan You Date: Sun, 16 Apr 2017 20:04:42 +0800 Subject: [PATCH] directMode -> runInNewContext --- .../bundle-renderer/create-bundle-renderer.js | 4 ++-- .../bundle-renderer/create-bundle-runner.js | 11 +++++---- src/server/create-renderer.js | 2 +- test/ssr/ssr-bundle-render.spec.js | 24 +++++++++---------- test/ssr/ssr-template.spec.js | 8 +++---- 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/server/bundle-renderer/create-bundle-renderer.js b/src/server/bundle-renderer/create-bundle-renderer.js index 18f60f7e..2b7cf10c 100644 --- a/src/server/bundle-renderer/create-bundle-renderer.js +++ b/src/server/bundle-renderer/create-bundle-renderer.js @@ -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) => { diff --git a/src/server/bundle-renderer/create-bundle-runner.js b/src/server/bundle-renderer/create-bundle-runner.js index a55727ca..ede74183 100644 --- a/src/server/bundle-renderer/create-bundle-runner.js +++ b/src/server/bundle-renderer/create-bundle-runner.js @@ -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 diff --git a/src/server/create-renderer.js b/src/server/create-renderer.js index fb0fbf44..1277fdbe 100644 --- a/src/server/create-renderer.js +++ b/src/server/create-renderer.js @@ -24,9 +24,9 @@ export type RenderOptions = { cache?: RenderCache; template?: string; basedir?: string; - directMode?: boolean; shouldPreload?: Function; clientManifest?: ClientManifest; + runInNewContext?: boolean; }; export function createRenderer ({ diff --git a/test/ssr/ssr-bundle-render.spec.js b/test/ssr/ssr-bundle-render.spec.js index a63ad821..95e22c49 100644 --- a/test/ssr/ssr-bundle-render.spec.js +++ b/test/ssr/ssr-bundle-render.spec.js @@ -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 = '
/test
' @@ -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') diff --git a/test/ssr/ssr-template.spec.js b/test/ssr/ssr-template.spec.js index d5670aa0..453a1f9b 100644 --- a/test/ssr/ssr-template.spec.js +++ b/test/ssr/ssr-template.spec.js @@ -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 = {}