Skip to content

Commit da9af18

Browse files
authored
Merge pull request #1189 from docsifyjs/allow-config-function
Allow configs to be functions
2 parents a1ddb3c + 146e4c4 commit da9af18

16 files changed

+1181
-1033
lines changed

.editorconfig

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# http://EditorConfig.org
2+
3+
root = true
4+
5+
[*]
6+
charset = utf-8
7+
indent_style = space
8+
indent_size = 2
9+
end_of_line = lf
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true

build/build.js

+73-49
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@ const version = process.env.VERSION || require('../package.json').version
99
const chokidar = require('chokidar')
1010
const path = require('path')
1111

12-
const build = function (opts) {
13-
rollup
12+
/**
13+
* @param {{
14+
* input: string,
15+
* output?: string,
16+
* globalName?: string,
17+
* plugins?: Array<import('rollup').Plugin>
18+
* }} opts
19+
*/
20+
async function build(opts) {
21+
await rollup
1422
.rollup({
1523
input: opts.input,
1624
plugins: (opts.plugins || []).concat([
@@ -27,31 +35,35 @@ const build = function (opts) {
2735
var dest = 'lib/' + (opts.output || opts.input)
2836

2937
console.log(dest)
30-
bundle.write({
38+
return bundle.write({
3139
format: 'iife',
40+
output: opts.globalName ? {name: opts.globalName} : {},
3241
file: dest,
3342
strict: false
3443
})
3544
})
36-
.catch(function (err) {
37-
console.error(err)
38-
})
3945
}
40-
const buildCore = function () {
41-
build({
46+
47+
async function buildCore() {
48+
const promises = []
49+
50+
promises.push(build({
4251
input: 'src/core/index.js',
43-
output: 'docsify.js'
44-
})
52+
output: 'docsify.js',
53+
}))
4554

4655
if (isProd) {
47-
build({
56+
promises.push(build({
4857
input: 'src/core/index.js',
4958
output: 'docsify.min.js',
5059
plugins: [uglify()]
51-
})
60+
}))
5261
}
62+
63+
await Promise.all(promises)
5364
}
54-
const buildAllPlugin = function () {
65+
66+
async function buildAllPlugin() {
5567
var plugins = [
5668
{name: 'search', input: 'search/index.js'},
5769
{name: 'ga', input: 'ga.js'},
@@ -64,56 +76,68 @@ const buildAllPlugin = function () {
6476
{name: 'gitalk', input: 'gitalk.js'}
6577
]
6678

67-
plugins.forEach(item => {
68-
build({
79+
const promises = plugins.map(item => {
80+
return build({
6981
input: 'src/plugins/' + item.input,
7082
output: 'plugins/' + item.name + '.js'
7183
})
7284
})
7385

7486
if (isProd) {
7587
plugins.forEach(item => {
76-
build({
88+
promises.push(build({
7789
input: 'src/plugins/' + item.input,
7890
output: 'plugins/' + item.name + '.min.js',
7991
plugins: [uglify()]
80-
})
92+
}))
8193
})
8294
}
95+
96+
await Promise.all(promises)
8397
}
8498

85-
if (!isProd) {
86-
chokidar
87-
.watch(['src/core', 'src/plugins'], {
88-
atomic: true,
89-
awaitWriteFinish: {
90-
stabilityThreshold: 1000,
91-
pollInterval: 100
92-
}
93-
})
94-
.on('change', p => {
95-
console.log('[watch] ', p)
96-
const dirs = p.split(path.sep)
97-
if (dirs[1] === 'core') {
98-
buildCore()
99-
} else if (dirs[2]) {
100-
const name = path.basename(dirs[2], '.js')
101-
const input = `src/plugins/${name}${
102-
/\.js/.test(dirs[2]) ? '' : '/index'
103-
}.js`
99+
async function main() {
100+
if (!isProd) {
101+
chokidar
102+
.watch(['src/core', 'src/plugins'], {
103+
atomic: true,
104+
awaitWriteFinish: {
105+
stabilityThreshold: 1000,
106+
pollInterval: 100
107+
}
108+
})
109+
.on('change', p => {
110+
console.log('[watch] ', p)
111+
const dirs = p.split(path.sep)
112+
if (dirs[1] === 'core') {
113+
buildCore()
114+
} else if (dirs[2]) {
115+
const name = path.basename(dirs[2], '.js')
116+
const input = `src/plugins/${name}${
117+
/\.js/.test(dirs[2]) ? '' : '/index'
118+
}.js`
104119

105-
build({
106-
input,
107-
output: 'plugins/' + name + '.js'
108-
})
109-
}
110-
})
111-
.on('ready', () => {
112-
console.log('[start]')
113-
buildCore()
120+
build({
121+
input,
122+
output: 'plugins/' + name + '.js'
123+
})
124+
}
125+
})
126+
.on('ready', () => {
127+
console.log('[start]')
128+
buildCore()
129+
buildAllPlugin()
130+
})
131+
} else {
132+
await Promise.all([
133+
buildCore(),
114134
buildAllPlugin()
115-
})
116-
} else {
117-
buildCore()
118-
buildAllPlugin()
135+
])
136+
}
119137
}
138+
139+
main().catch((e) => {
140+
console.error(e)
141+
process.exit(1)
142+
})
143+

build/css.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const {spawn} = require('child_process')
55
const args = process.argv.slice(2)
66
fs.readdir(path.join(__dirname, '../src/themes'), (err, files) => {
77
if (err) {
8-
console.log('err', err)
9-
return
8+
console.error('err', err)
9+
process.exit(1)
1010
}
1111
files.map(async (file) => {
1212
if (/\.styl/g.test(file)) {
@@ -31,11 +31,17 @@ fs.readdir(path.join(__dirname, '../src/themes'), (err, files) => {
3131
});
3232

3333
stylusCMD.on('close', (code) => {
34-
console.log(`[Stylus Build ] child process exited with code ${code}`);
34+
const message = `[Stylus Build ] child process exited with code ${code}`
35+
36+
if (code !== 0) {
37+
console.error(message);
38+
process.exit(code)
39+
}
40+
console.log(message);
3541
});
3642
} else {
3743
return
3844
}
3945

4046
})
41-
})
47+
})

build/mincss.js

+3
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ files.forEach(file => {
88
file = path.resolve('lib/themes', file)
99
cssnano(fs.readFileSync(file)).then(result => {
1010
fs.writeFileSync(file, result.css)
11+
}).catch(e => {
12+
console.error(e)
13+
process.exit(1)
1114
})
1215
})

build/ssr.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ rollup
2424
var dest = 'packages/docsify-server-renderer/build.js'
2525

2626
console.log(dest)
27-
bundle.write({
27+
return bundle.write({
2828
format: 'cjs',
2929
file: dest
3030
})
3131
})
3232
.catch(function (err) {
3333
console.error(err)
34+
process.exit(1)
3435
})

docs/configuration.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Configuration
22

3-
You can configure the `window.$docsify`.
3+
You can configure Docsify by defining `window.$docsify` as an object:
44

55
```html
66
<script>
@@ -12,6 +12,24 @@ You can configure the `window.$docsify`.
1212
</script>
1313
```
1414

15+
The config can also be defined as a function, in which case the first arg is the Docsify `vm` instance. The function should return a config object. This can be useful for referencing `vm` in places like the markdown configuration:
16+
17+
```html
18+
<script>
19+
window.$docsify = function(vm) {
20+
return {
21+
markdown: {
22+
renderer: {
23+
code(code, lang) {
24+
// ... use `vm` ...
25+
},
26+
},
27+
},
28+
};
29+
};
30+
</script>
31+
```
32+
1533
## el
1634

1735
- Type: `String`

0 commit comments

Comments
 (0)