Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 4, 2018
1 parent dc643ca commit 4cbb6b6
Show file tree
Hide file tree
Showing 15 changed files with 1,863 additions and 356 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
node_modules
*.log
.temp
docs/_dist
TODOs.md
4 changes: 3 additions & 1 deletion bin/vuepress.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const path = require('path')
const { build } = require('../lib')

build(path.resolve(__dirname, '../docs'))
build(path.resolve(__dirname, '../docs')).catch(err => {
console.log(err)
})
2 changes: 1 addition & 1 deletion docs/_components/demo-1.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export default {
data () {
return {
msg: 'hello'
msg: 'hello this is a dynamic demo'
}
}
}
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions docs/layout.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Custom Layout

VuePress uses Vue single file components for custom layouts. To use a custom layout, create a `_layout` directory under `docs`, and then create a `Layout.vue` file:
VuePress uses Vue single file components for custom layouts. To use a custom layout, create a `_theme` directory under `docs`, and then create a `App.vue` file:

```
``` bash
- docs
- _layout
- Layout.vue
- _theme
- App.vue
```

From there it's the same as developing a normal Vue application. There are only a few special things to note:
Expand Down
14 changes: 11 additions & 3 deletions lib/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ import routes from './.temp/routes'

const router = new Router({
mode: 'history',
scrollBehavior: () => ({ y: 0 }),
routes
})

// expose Vue Press data
const g = typeof window !== 'undefined' ? window : global
const $site = Vue.prototype.$site = g.VUE_PRESS_DATA
const $site = Vue.prototype.$site = g.VUEPRESS_DATA

Vue.mixin({
computed: {
Expand All @@ -32,9 +31,18 @@ Vue.mixin({
}
})

Vue.component('Content', {
functional: true,
render (h, { parent }) {
return h('page-' + parent.$page.name)
}
})

const app = new Vue({
router,
render: h => h(Layout)
render (h) {
return h('router-view')
}
})

export { app, router }
15 changes: 0 additions & 15 deletions lib/default-layout/Layout.vue

This file was deleted.

114 changes: 114 additions & 0 deletions lib/default-theme/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<template>
<div id="container">
<ul>
<li v-for="page in $site.pages">
<router-link :to="page.path">{{ page.name }}</router-link>
</li>
</ul>
<Index v-if="$page.isIndex" />
<Page v-else />
</div>
</template>

<script>
import nprogress from 'nprogress'
import Index from './Index.vue'
import Page from './Page.vue'
export default {
components: { Index, Page },
mounted () {
nprogress.configure({ showSpinner: false })
this.$router.beforeEach((to, from, next) => {
nprogress.start()
next()
})
this.$router.afterEach(() => {
nprogress.done()
})
}
}
</script>

<style>
/*
Copied from nprogress since it doens't
allow programmatic configuration of color
*/
/* Make clicks pass-through */
#nprogress {
pointer-events: none;
}
#nprogress .bar {
background: #41b883;
position: fixed;
z-index: 1031;
top: 0;
left: 0;
width: 100%;
height: 2px;
}
/* Fancy blur effect */
#nprogress .peg {
display: block;
position: absolute;
right: 0px;
width: 100px;
height: 100%;
box-shadow: 0 0 10px #41b883, 0 0 5px #41b883;
opacity: 1.0;
-webkit-transform: rotate(3deg) translate(0px, -4px);
-ms-transform: rotate(3deg) translate(0px, -4px);
transform: rotate(3deg) translate(0px, -4px);
}
/* Remove these to get rid of the spinner */
#nprogress .spinner {
display: block;
position: fixed;
z-index: 1031;
top: 15px;
right: 15px;
}
#nprogress .spinner-icon {
width: 18px;
height: 18px;
box-sizing: border-box;
border: solid 2px transparent;
border-top-color: #41b883;
border-left-color: #41b883;
border-radius: 50%;
-webkit-animation: nprogress-spinner 400ms linear infinite;
animation: nprogress-spinner 400ms linear infinite;
}
.nprogress-custom-parent {
overflow: hidden;
position: relative;
}
.nprogress-custom-parent #nprogress .spinner,
.nprogress-custom-parent #nprogress .bar {
position: absolute;
}
@-webkit-keyframes nprogress-spinner {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
@keyframes nprogress-spinner {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<div class="index">
<h1>This is the Index</h1>
<Content/>
</div>
</template>
File renamed without changes.
File renamed without changes.
66 changes: 51 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const fs = require('fs')
const path = require('path')
const rimraf = require('rimraf')
const globby = require('globby')
const webpack = require('webpack')
const tempPath = path.resolve(__dirname, 'app/.temp')
const createClientConfig = require('./webpack/clientConfig')
const createServerConfig = require('./webpack/serverConfig')

exports.build = async function build (sourceDir) {
// 1. loadConfig
Expand All @@ -14,30 +17,63 @@ exports.build = async function build (sourceDir) {
// 3. generate routes
await genRoutesFile(sourceDir)

// 4. build
// 4. client build
const clientConfig = createClientConfig({ sourceDir }).toConfig()
return new Promise((resolve, reject) => {
rimraf.sync(clientConfig.output.path)
webpack(clientConfig, (err, stats) => {
if (err) {
return reject(err)
}
if (stats.hasErrors()) {
return reject(stats.toJson().errors)
}
resolve()
})
})
}

async function genComponentRegistrationFile (sourceDir) {
const componentDir = path.resolve(sourceDir, '_components')
if (!fs.existsSync(componentDir)) {
return
}
const pages = await globby(['**/*.md'], { cwd: sourceDir })
const components = (await resolveComponents(sourceDir)) || []

const components = await globby(['*.vue'], { cwd: componentDir })
if (!components.length) {
return
}

const file = `import Vue from 'vue'\n` + components.map(file => {
const name = file.replace(/\.vue$/, '')
const absolutePath = path.resolve(componentDir, file)
function genImport (file) {
const isPage = /\.md$/.test(file)
const name = (isPage ? `page-` : ``) + file.replace(/\.(vue|md)$/, '').replace(/\/|\\/, '-')
const baseDir = isPage ? sourceDir : path.resolve(sourceDir, '_components')
const absolutePath = path.resolve(baseDir, file)
const code = `Vue.component(${JSON.stringify(name)}, () => import(${JSON.stringify(absolutePath)}))`
return code
}).join('\n')
}

const all = [...pages, ...components]
const file = `import Vue from 'vue'\n` + all.map(genImport).join('\n')
fs.writeFileSync(path.join(tempPath, 'register-components.js'), file)
}

async function genRoutesFile () {
async function resolveComponents (sourceDir) {
const componentDir = path.resolve(sourceDir, '_components')
if (!fs.existsSync(componentDir)) {
return
}
return await globby(['*.vue'], { cwd: componentDir })
}

async function genRoutesFile (sourceDir) {
const pages = await globby(['**/*.md'], { cwd: sourceDir })

function genRoute (file) {
const name = file.replace(/\.md$/, '')
const code = `
{
path: ${JSON.stringify('/' + (name === 'index' ? '' : name))},
component: Layout
}`
return code
}

const file =
`import Layout from '~layout'\n` +
`export default [${pages.map(genRoute).join(',')}\n]`
fs.writeFileSync(path.join(tempPath, 'routes.js'), file)
}
Loading

0 comments on commit 4cbb6b6

Please sign in to comment.