Skip to content

Commit d026801

Browse files
mdaffinyyx990803
authored andcommitted
feat: theme index enhancment support (#154)
1 parent f322105 commit d026801

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

docs/guide/custom-themes.md

+14
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,20 @@ The compiled content of the current `.md` file being rendered will be available
6666
</template>
6767
```
6868

69+
## Theme Level Enhancements
70+
71+
Themes can extend the Vue app that VuePress uses by exposing an `index.js` file at the root of the theme. The file should `export default` a hook function which will receive an object containing some app level values. You can use this hook to install additional Vue plugins, register global components, or add additional router hooks:
72+
73+
``` js
74+
export default ({
75+
Vue, // the version of Vue being used in the VuePress app
76+
options, // the options for the root Vue instance
77+
router // the router instance for the app
78+
}) => {
79+
// ...apply enhancements to the app
80+
}
81+
```
82+
6983
## Using Theme from a Dependency
7084

7185
Themes can be published on npm in raw Vue SFC format as `vuepress-theme-xxx`.

lib/app/app.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import NotFound from '@notFound'
77
import { routes } from '@temp/routes'
88
import { siteData } from '@temp/siteData'
99
import enhanceApp from '@temp/enhanceApp'
10+
import themeEnhanceApp from '@temp/themeEnhanceApp'
1011

1112
// suggest dev server restart on base change
1213
if (module.hot) {
@@ -75,7 +76,8 @@ export function createApp () {
7576
})
7677

7778
const options = {}
78-
79+
80+
themeEnhanceApp({ Vue, options, router })
7981
enhanceApp({ Vue, options, router })
8082

8183
const app = new Vue(

lib/prepare.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,21 @@ if (!Object.assign) Object.assign = require('object-assign')`
5353
await writeTemp(`override.styl`, hasUserOverride ? `@import(${JSON.stringify(overridePath)})` : ``)
5454
}
5555

56+
async function writeEnhanceTemp (destName, srcPath, isEnhanceExist) {
57+
await writeTemp(
58+
destName,
59+
isEnhanceExist
60+
? `export { default } from ${JSON.stringify(srcPath)}`
61+
: `export default function () {}`
62+
)
63+
}
64+
5665
// 6. handle enhanceApp.js
57-
const enhancePath = path.resolve(sourceDir, '.vuepress/enhanceApp.js')
58-
const hasEnhancePath = fs.existsSync(enhancePath)
59-
await writeTemp(
60-
'enhanceApp.js',
61-
hasEnhancePath
62-
? `export { default } from ${JSON.stringify(enhancePath)}`
63-
: `export default function () {}`
64-
)
66+
const enhanceAppPath = path.resolve(sourceDir, '.vuepress/enhanceApp.js')
67+
writeEnhanceTemp('enhanceApp.js', enhanceAppPath, fs.existsSync(enhanceAppPath))
68+
69+
// 7. handle the theme index.js
70+
writeEnhanceTemp('themeEnhanceApp.js', options.themeApp, fs.existsSync(options.themeApp))
6571

6672
return options
6773
}
@@ -153,6 +159,11 @@ async function resolveOptions (sourceDir) {
153159
} else {
154160
options.notFoundPath = path.resolve(__dirname, 'default-theme/NotFound.vue')
155161
}
162+
163+
const themeApp = path.resolve(themeDir, 'index.js')
164+
if (fs.existsSync(themeApp)) {
165+
options.themeApp = themeApp
166+
}
156167
}
157168

158169
// resolve pages

0 commit comments

Comments
 (0)