Skip to content

Commit

Permalink
feat: theme index enhancment support (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaffin authored and yyx990803 committed Apr 23, 2018
1 parent f322105 commit d026801
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
14 changes: 14 additions & 0 deletions docs/guide/custom-themes.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ The compiled content of the current `.md` file being rendered will be available
</template>
```

## Theme Level Enhancements

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:

``` js
export default ({
Vue, // the version of Vue being used in the VuePress app
options, // the options for the root Vue instance
router // the router instance for the app
}) => {
// ...apply enhancements to the app
}
```

## Using Theme from a Dependency

Themes can be published on npm in raw Vue SFC format as `vuepress-theme-xxx`.
Expand Down
4 changes: 3 additions & 1 deletion lib/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import NotFound from '@notFound'
import { routes } from '@temp/routes'
import { siteData } from '@temp/siteData'
import enhanceApp from '@temp/enhanceApp'
import themeEnhanceApp from '@temp/themeEnhanceApp'

// suggest dev server restart on base change
if (module.hot) {
Expand Down Expand Up @@ -75,7 +76,8 @@ export function createApp () {
})

const options = {}


themeEnhanceApp({ Vue, options, router })
enhanceApp({ Vue, options, router })

const app = new Vue(
Expand Down
27 changes: 19 additions & 8 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,21 @@ if (!Object.assign) Object.assign = require('object-assign')`
await writeTemp(`override.styl`, hasUserOverride ? `@import(${JSON.stringify(overridePath)})` : ``)
}

async function writeEnhanceTemp (destName, srcPath, isEnhanceExist) {
await writeTemp(
destName,
isEnhanceExist
? `export { default } from ${JSON.stringify(srcPath)}`
: `export default function () {}`
)
}

// 6. handle enhanceApp.js
const enhancePath = path.resolve(sourceDir, '.vuepress/enhanceApp.js')
const hasEnhancePath = fs.existsSync(enhancePath)
await writeTemp(
'enhanceApp.js',
hasEnhancePath
? `export { default } from ${JSON.stringify(enhancePath)}`
: `export default function () {}`
)
const enhanceAppPath = path.resolve(sourceDir, '.vuepress/enhanceApp.js')
writeEnhanceTemp('enhanceApp.js', enhanceAppPath, fs.existsSync(enhanceAppPath))

// 7. handle the theme index.js
writeEnhanceTemp('themeEnhanceApp.js', options.themeApp, fs.existsSync(options.themeApp))

return options
}
Expand Down Expand Up @@ -153,6 +159,11 @@ async function resolveOptions (sourceDir) {
} else {
options.notFoundPath = path.resolve(__dirname, 'default-theme/NotFound.vue')
}

const themeApp = path.resolve(themeDir, 'index.js')
if (fs.existsSync(themeApp)) {
options.themeApp = themeApp
}
}

// resolve pages
Expand Down

0 comments on commit d026801

Please sign in to comment.