Skip to content

Commit 4cbb6b6

Browse files
committed
progress
1 parent dc643ca commit 4cbb6b6

File tree

15 files changed

+1863
-356
lines changed

15 files changed

+1863
-356
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
node_modules
33
*.log
44
.temp
5+
docs/_dist
6+
TODOs.md

bin/vuepress.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const path = require('path')
22
const { build } = require('../lib')
33

4-
build(path.resolve(__dirname, '../docs'))
4+
build(path.resolve(__dirname, '../docs')).catch(err => {
5+
console.log(err)
6+
})

docs/_components/demo-1.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
export default {
99
data () {
1010
return {
11-
msg: 'hello'
11+
msg: 'hello this is a dynamic demo'
1212
}
1313
}
1414
}
File renamed without changes.

docs/layout.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Custom Layout
22

3-
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:
3+
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:
44

5-
```
5+
``` bash
66
- docs
7-
- _layout
8-
- Layout.vue
7+
- _theme
8+
- App.vue
99
```
1010

1111
From there it's the same as developing a normal Vue application. There are only a few special things to note:

lib/app/app.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ import routes from './.temp/routes'
1616

1717
const router = new Router({
1818
mode: 'history',
19-
scrollBehavior: () => ({ y: 0 }),
2019
routes
2120
})
2221

2322
// expose Vue Press data
2423
const g = typeof window !== 'undefined' ? window : global
25-
const $site = Vue.prototype.$site = g.VUE_PRESS_DATA
24+
const $site = Vue.prototype.$site = g.VUEPRESS_DATA
2625

2726
Vue.mixin({
2827
computed: {
@@ -32,9 +31,18 @@ Vue.mixin({
3231
}
3332
})
3433

34+
Vue.component('Content', {
35+
functional: true,
36+
render (h, { parent }) {
37+
return h('page-' + parent.$page.name)
38+
}
39+
})
40+
3541
const app = new Vue({
3642
router,
37-
render: h => h(Layout)
43+
render (h) {
44+
return h('router-view')
45+
}
3846
})
3947

4048
export { app, router }

lib/default-layout/Layout.vue

-15
This file was deleted.

lib/default-theme/App.vue

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<template>
2+
<div id="container">
3+
<ul>
4+
<li v-for="page in $site.pages">
5+
<router-link :to="page.path">{{ page.name }}</router-link>
6+
</li>
7+
</ul>
8+
<Index v-if="$page.isIndex" />
9+
<Page v-else />
10+
</div>
11+
</template>
12+
13+
<script>
14+
import nprogress from 'nprogress'
15+
import Index from './Index.vue'
16+
import Page from './Page.vue'
17+
18+
export default {
19+
components: { Index, Page },
20+
mounted () {
21+
nprogress.configure({ showSpinner: false })
22+
23+
this.$router.beforeEach((to, from, next) => {
24+
nprogress.start()
25+
next()
26+
})
27+
28+
this.$router.afterEach(() => {
29+
nprogress.done()
30+
})
31+
}
32+
}
33+
</script>
34+
35+
<style>
36+
/*
37+
Copied from nprogress since it doens't
38+
allow programmatic configuration of color
39+
*/
40+
41+
/* Make clicks pass-through */
42+
#nprogress {
43+
pointer-events: none;
44+
}
45+
46+
#nprogress .bar {
47+
background: #41b883;
48+
49+
position: fixed;
50+
z-index: 1031;
51+
top: 0;
52+
left: 0;
53+
54+
width: 100%;
55+
height: 2px;
56+
}
57+
58+
/* Fancy blur effect */
59+
#nprogress .peg {
60+
display: block;
61+
position: absolute;
62+
right: 0px;
63+
width: 100px;
64+
height: 100%;
65+
box-shadow: 0 0 10px #41b883, 0 0 5px #41b883;
66+
opacity: 1.0;
67+
68+
-webkit-transform: rotate(3deg) translate(0px, -4px);
69+
-ms-transform: rotate(3deg) translate(0px, -4px);
70+
transform: rotate(3deg) translate(0px, -4px);
71+
}
72+
73+
/* Remove these to get rid of the spinner */
74+
#nprogress .spinner {
75+
display: block;
76+
position: fixed;
77+
z-index: 1031;
78+
top: 15px;
79+
right: 15px;
80+
}
81+
82+
#nprogress .spinner-icon {
83+
width: 18px;
84+
height: 18px;
85+
box-sizing: border-box;
86+
87+
border: solid 2px transparent;
88+
border-top-color: #41b883;
89+
border-left-color: #41b883;
90+
border-radius: 50%;
91+
92+
-webkit-animation: nprogress-spinner 400ms linear infinite;
93+
animation: nprogress-spinner 400ms linear infinite;
94+
}
95+
96+
.nprogress-custom-parent {
97+
overflow: hidden;
98+
position: relative;
99+
}
100+
101+
.nprogress-custom-parent #nprogress .spinner,
102+
.nprogress-custom-parent #nprogress .bar {
103+
position: absolute;
104+
}
105+
106+
@-webkit-keyframes nprogress-spinner {
107+
0% { -webkit-transform: rotate(0deg); }
108+
100% { -webkit-transform: rotate(360deg); }
109+
}
110+
@keyframes nprogress-spinner {
111+
0% { transform: rotate(0deg); }
112+
100% { transform: rotate(360deg); }
113+
}
114+
</style>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<template>
22
<div class="index">
3+
<h1>This is the Index</h1>
34
<Content/>
45
</div>
56
</template>
File renamed without changes.
File renamed without changes.

lib/index.js

+51-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
const fs = require('fs')
22
const path = require('path')
3+
const rimraf = require('rimraf')
34
const globby = require('globby')
45
const webpack = require('webpack')
56
const tempPath = path.resolve(__dirname, 'app/.temp')
7+
const createClientConfig = require('./webpack/clientConfig')
8+
const createServerConfig = require('./webpack/serverConfig')
69

710
exports.build = async function build (sourceDir) {
811
// 1. loadConfig
@@ -14,30 +17,63 @@ exports.build = async function build (sourceDir) {
1417
// 3. generate routes
1518
await genRoutesFile(sourceDir)
1619

17-
// 4. build
20+
// 4. client build
21+
const clientConfig = createClientConfig({ sourceDir }).toConfig()
22+
return new Promise((resolve, reject) => {
23+
rimraf.sync(clientConfig.output.path)
24+
webpack(clientConfig, (err, stats) => {
25+
if (err) {
26+
return reject(err)
27+
}
28+
if (stats.hasErrors()) {
29+
return reject(stats.toJson().errors)
30+
}
31+
resolve()
32+
})
33+
})
1834
}
1935

2036
async function genComponentRegistrationFile (sourceDir) {
21-
const componentDir = path.resolve(sourceDir, '_components')
22-
if (!fs.existsSync(componentDir)) {
23-
return
24-
}
37+
const pages = await globby(['**/*.md'], { cwd: sourceDir })
38+
const components = (await resolveComponents(sourceDir)) || []
2539

26-
const components = await globby(['*.vue'], { cwd: componentDir })
27-
if (!components.length) {
28-
return
29-
}
30-
31-
const file = `import Vue from 'vue'\n` + components.map(file => {
32-
const name = file.replace(/\.vue$/, '')
33-
const absolutePath = path.resolve(componentDir, file)
40+
function genImport (file) {
41+
const isPage = /\.md$/.test(file)
42+
const name = (isPage ? `page-` : ``) + file.replace(/\.(vue|md)$/, '').replace(/\/|\\/, '-')
43+
const baseDir = isPage ? sourceDir : path.resolve(sourceDir, '_components')
44+
const absolutePath = path.resolve(baseDir, file)
3445
const code = `Vue.component(${JSON.stringify(name)}, () => import(${JSON.stringify(absolutePath)}))`
3546
return code
36-
}).join('\n')
47+
}
3748

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

41-
async function genRoutesFile () {
54+
async function resolveComponents (sourceDir) {
55+
const componentDir = path.resolve(sourceDir, '_components')
56+
if (!fs.existsSync(componentDir)) {
57+
return
58+
}
59+
return await globby(['*.vue'], { cwd: componentDir })
60+
}
61+
62+
async function genRoutesFile (sourceDir) {
63+
const pages = await globby(['**/*.md'], { cwd: sourceDir })
64+
65+
function genRoute (file) {
66+
const name = file.replace(/\.md$/, '')
67+
const code = `
68+
{
69+
path: ${JSON.stringify('/' + (name === 'index' ? '' : name))},
70+
component: Layout
71+
}`
72+
return code
73+
}
4274

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

0 commit comments

Comments
 (0)