From 1e9c7abdef6c15b2de0c24db75fe467c399de93d Mon Sep 17 00:00:00 2001 From: fjc0k Date: Mon, 16 Apr 2018 19:53:34 +0800 Subject: [PATCH 1/3] feat: allow injecting Vue, app, router --- lib/app/app.js | 4 ++++ lib/prepare.js | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/app/app.js b/lib/app/app.js index 1caf8bab12..537a19e60d 100644 --- a/lib/app/app.js +++ b/lib/app/app.js @@ -6,6 +6,7 @@ import dataMixin from './dataMixin' import NotFound from '@notFound' import { routes } from '@temp/routes' import { siteData } from '@temp/siteData' +import inject from '@temp/inject' // suggest dev server restart on base change if (module.hot) { @@ -73,5 +74,8 @@ export function createApp () { } }) + // user inject + inject({ Vue, app, router }) + return { ...{ app, router }} } diff --git a/lib/prepare.js b/lib/prepare.js index 3fd7b45376..bcf29b2c9d 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -50,6 +50,16 @@ if (!Object.assign) Object.assign = require('object-assign')` await writeTemp(`override.styl`, hasUserOverride ? `@import(${JSON.stringify(overridePath)})` : ``) } + // 6. handle user inject + const injectPath = path.resolve(sourceDir, '.vuepress/inject.js') + const hasUserInject = fs.existsSync(injectPath) + await writeTemp( + 'inject.js', + hasUserInject + ? `export { default } from ${JSON.stringify(injectPath)}` + : `export default function () {}` + ) + return options } From 6be5350b0d6416891dfc46140009781e01b6217f Mon Sep 17 00:00:00 2001 From: fjc0k Date: Tue, 17 Apr 2018 01:50:29 +0800 Subject: [PATCH 2/3] refactor: rename to enhanceApp.js --- lib/app/app.js | 25 ++++++++++++++----------- lib/prepare.js | 12 ++++++------ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/app/app.js b/lib/app/app.js index 537a19e60d..85eaaf7aa1 100644 --- a/lib/app/app.js +++ b/lib/app/app.js @@ -6,7 +6,7 @@ import dataMixin from './dataMixin' import NotFound from '@notFound' import { routes } from '@temp/routes' import { siteData } from '@temp/siteData' -import inject from '@temp/inject' +import enhanceApp from '@temp/enhanceApp' // suggest dev server restart on base change if (module.hot) { @@ -65,17 +65,20 @@ export function createApp () { } }) - const app = new Vue({ - router, - render (h) { - return h('div', { attrs: { id: 'app' }}, [ - h('router-view', { ref: 'layout' }) - ]) - } - }) + const options = {} - // user inject - inject({ Vue, app, router }) + enhanceApp({ Vue, options }) + + const app = new Vue( + Object.assign(options, { + router, + render (h) { + return h('div', { attrs: { id: 'app' }}, [ + h('router-view', { ref: 'layout' }) + ]) + } + }) + ) return { ...{ app, router }} } diff --git a/lib/prepare.js b/lib/prepare.js index bcf29b2c9d..c57b5cce20 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -50,13 +50,13 @@ if (!Object.assign) Object.assign = require('object-assign')` await writeTemp(`override.styl`, hasUserOverride ? `@import(${JSON.stringify(overridePath)})` : ``) } - // 6. handle user inject - const injectPath = path.resolve(sourceDir, '.vuepress/inject.js') - const hasUserInject = fs.existsSync(injectPath) + // 6. handle enhanceApp.js + const enhancePath = path.resolve(sourceDir, '.vuepress/enhanceApp.js') + const hasEnhancePath = fs.existsSync(enhancePath) await writeTemp( - 'inject.js', - hasUserInject - ? `export { default } from ${JSON.stringify(injectPath)}` + 'enhanceApp.js', + hasEnhancePath + ? `export { default } from ${JSON.stringify(enhancePath)}` : `export default function () {}` ) From bfcc62d613e5c6183778653b91858ec43b13ec50 Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 16 Apr 2018 14:00:52 -0400 Subject: [PATCH 3/3] Update app.js --- lib/app/app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/app/app.js b/lib/app/app.js index 025bbd55ef..ad7fb7675d 100644 --- a/lib/app/app.js +++ b/lib/app/app.js @@ -67,7 +67,7 @@ export function createApp () { const options = {} - enhanceApp({ Vue, options }) + enhanceApp({ Vue, options, router }) const app = new Vue( Object.assign(options, {