Skip to content

Commit

Permalink
Merge pull request #1189 from docsifyjs/allow-config-function
Browse files Browse the repository at this point in the history
Allow configs to be functions
  • Loading branch information
trusktr committed Jun 13, 2020
2 parents a1ddb3c + 146e4c4 commit da9af18
Show file tree
Hide file tree
Showing 16 changed files with 1,181 additions and 1,033 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
@@ -0,0 +1,11 @@
# http://EditorConfig.org

root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
122 changes: 73 additions & 49 deletions build/build.js
Expand Up @@ -9,8 +9,16 @@ const version = process.env.VERSION || require('../package.json').version
const chokidar = require('chokidar')
const path = require('path')

const build = function (opts) {
rollup
/**
* @param {{
* input: string,
* output?: string,
* globalName?: string,
* plugins?: Array<import('rollup').Plugin>
* }} opts
*/
async function build(opts) {
await rollup
.rollup({
input: opts.input,
plugins: (opts.plugins || []).concat([
Expand All @@ -27,31 +35,35 @@ const build = function (opts) {
var dest = 'lib/' + (opts.output || opts.input)

console.log(dest)
bundle.write({
return bundle.write({
format: 'iife',
output: opts.globalName ? {name: opts.globalName} : {},
file: dest,
strict: false
})
})
.catch(function (err) {
console.error(err)
})
}
const buildCore = function () {
build({

async function buildCore() {
const promises = []

promises.push(build({
input: 'src/core/index.js',
output: 'docsify.js'
})
output: 'docsify.js',
}))

if (isProd) {
build({
promises.push(build({
input: 'src/core/index.js',
output: 'docsify.min.js',
plugins: [uglify()]
})
}))
}

await Promise.all(promises)
}
const buildAllPlugin = function () {

async function buildAllPlugin() {
var plugins = [
{name: 'search', input: 'search/index.js'},
{name: 'ga', input: 'ga.js'},
Expand All @@ -64,56 +76,68 @@ const buildAllPlugin = function () {
{name: 'gitalk', input: 'gitalk.js'}
]

plugins.forEach(item => {
build({
const promises = plugins.map(item => {
return build({
input: 'src/plugins/' + item.input,
output: 'plugins/' + item.name + '.js'
})
})

if (isProd) {
plugins.forEach(item => {
build({
promises.push(build({
input: 'src/plugins/' + item.input,
output: 'plugins/' + item.name + '.min.js',
plugins: [uglify()]
})
}))
})
}

await Promise.all(promises)
}

if (!isProd) {
chokidar
.watch(['src/core', 'src/plugins'], {
atomic: true,
awaitWriteFinish: {
stabilityThreshold: 1000,
pollInterval: 100
}
})
.on('change', p => {
console.log('[watch] ', p)
const dirs = p.split(path.sep)
if (dirs[1] === 'core') {
buildCore()
} else if (dirs[2]) {
const name = path.basename(dirs[2], '.js')
const input = `src/plugins/${name}${
/\.js/.test(dirs[2]) ? '' : '/index'
}.js`
async function main() {
if (!isProd) {
chokidar
.watch(['src/core', 'src/plugins'], {
atomic: true,
awaitWriteFinish: {
stabilityThreshold: 1000,
pollInterval: 100
}
})
.on('change', p => {
console.log('[watch] ', p)
const dirs = p.split(path.sep)
if (dirs[1] === 'core') {
buildCore()
} else if (dirs[2]) {
const name = path.basename(dirs[2], '.js')
const input = `src/plugins/${name}${
/\.js/.test(dirs[2]) ? '' : '/index'
}.js`

build({
input,
output: 'plugins/' + name + '.js'
})
}
})
.on('ready', () => {
console.log('[start]')
buildCore()
build({
input,
output: 'plugins/' + name + '.js'
})
}
})
.on('ready', () => {
console.log('[start]')
buildCore()
buildAllPlugin()
})
} else {
await Promise.all([
buildCore(),
buildAllPlugin()
})
} else {
buildCore()
buildAllPlugin()
])
}
}

main().catch((e) => {
console.error(e)
process.exit(1)
})

14 changes: 10 additions & 4 deletions build/css.js
Expand Up @@ -5,8 +5,8 @@ const {spawn} = require('child_process')
const args = process.argv.slice(2)
fs.readdir(path.join(__dirname, '../src/themes'), (err, files) => {
if (err) {
console.log('err', err)
return
console.error('err', err)
process.exit(1)
}
files.map(async (file) => {
if (/\.styl/g.test(file)) {
Expand All @@ -31,11 +31,17 @@ fs.readdir(path.join(__dirname, '../src/themes'), (err, files) => {
});

stylusCMD.on('close', (code) => {
console.log(`[Stylus Build ] child process exited with code ${code}`);
const message = `[Stylus Build ] child process exited with code ${code}`

if (code !== 0) {
console.error(message);
process.exit(code)
}
console.log(message);
});
} else {
return
}

})
})
})
3 changes: 3 additions & 0 deletions build/mincss.js
Expand Up @@ -8,5 +8,8 @@ files.forEach(file => {
file = path.resolve('lib/themes', file)
cssnano(fs.readFileSync(file)).then(result => {
fs.writeFileSync(file, result.css)
}).catch(e => {
console.error(e)
process.exit(1)
})
})
3 changes: 2 additions & 1 deletion build/ssr.js
Expand Up @@ -24,11 +24,12 @@ rollup
var dest = 'packages/docsify-server-renderer/build.js'

console.log(dest)
bundle.write({
return bundle.write({
format: 'cjs',
file: dest
})
})
.catch(function (err) {
console.error(err)
process.exit(1)
})
20 changes: 19 additions & 1 deletion docs/configuration.md
@@ -1,6 +1,6 @@
# Configuration

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

```html
<script>
Expand All @@ -12,6 +12,24 @@ You can configure the `window.$docsify`.
</script>
```

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:

```html
<script>
window.$docsify = function(vm) {
return {
markdown: {
renderer: {
code(code, lang) {
// ... use `vm` ...
},
},
},
};
};
</script>
```

## el

- Type: `String`
Expand Down

1 comment on commit da9af18

@vercel
Copy link

@vercel vercel bot commented on da9af18 Jun 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.