Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configs to be functions #1189

Merged
merged 21 commits into from Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f72bf4c
allow the user's $docsify config to be a function that receives as in…
trusktr May 22, 2020
b4b8bdc
add tests to make sure the global and plugin APIs are available and t…
trusktr May 22, 2020
732c34d
add editorconfig (tells editors which basic text format to use)
trusktr May 22, 2020
5a5b5aa
update docs regarding configs as functions
trusktr May 22, 2020
b72cb3d
add build error handling so builds don't silently fail
trusktr May 23, 2020
7e002bf
feat: update src/core/index.js to export all global APIs, deprecate o…
trusktr May 23, 2020
b40baae
Merge pull request #1195 from docsifyjs/deprecate-old-globals-and-mak…
trusktr May 23, 2020
1b8a81a
Merge pull request #1192 from docsifyjs/add-build-error-handling
trusktr May 23, 2020
88033b6
remove the DOCSIFY global made by Rollup, and move Docsify into a sep…
trusktr Jun 7, 2020
79fe3d5
remove some unused code and accept eslint changes
trusktr Jun 7, 2020
dca22f3
simplify import
trusktr Jun 7, 2020
41dc2c7
ensure that the test script runs a prod build
trusktr Jun 7, 2020
31f1a64
update outdated comment
trusktr Jun 7, 2020
63f1b88
Revert "ensure that the test script runs a prod build"
trusktr Jun 7, 2020
809fda9
Merge branch 'develop' into allow-config-function
trusktr Jun 7, 2020
72ec71f
Merge branch 'develop' into allow-config-function
trusktr Jun 8, 2020
0438dd6
Merge branch 'develop' into allow-config-function
trusktr Jun 9, 2020
64a231d
use a port for the tests that doesn't collide with common local serve…
trusktr Jun 10, 2020
abaf6f3
Merge branch 'develop' into allow-config-function
trusktr Jun 10, 2020
8f3f263
Merge branch 'develop' into allow-config-function
trusktr Jun 13, 2020
146e4c4
Update build/css.js
trusktr Jun 13, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
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
*/
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
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)
trusktr marked this conversation as resolved.
Show resolved Hide resolved
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)
})

13 changes: 10 additions & 3 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,7 +31,14 @@ 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)
} else {
console.log(message);
}
trusktr marked this conversation as resolved.
Show resolved Hide resolved
});
} else {
return
Expand Down
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>
anikethsaha marked this conversation as resolved.
Show resolved Hide resolved
```

## el

- Type: `String`
Expand Down