Skip to content

Commit

Permalink
Merge branch 'develop' into delete-package-lock
Browse files Browse the repository at this point in the history
* develop:
  Update build/css.js
  use a port for the tests that doesn't collide with common local server ports
  Revert "ensure that the test script runs a prod build"
  update outdated comment
  ensure that the test script runs a prod build
  simplify import
  remove some unused code and accept eslint changes
  remove the DOCSIFY global made by Rollup, and move Docsify into a separate file where we can import it from tests while leaving the entry point for the bundle without any exports so that Rollup will not create a global variable from it
  feat: update src/core/index.js to export all global APIs, deprecate old globals in favor of a single global DOCSIFY, and add tests for this
  add build error handling so builds don't silently fail
  update docs regarding configs as functions
  add editorconfig (tells editors which basic text format to use)
  add tests to make sure the global and plugin APIs are available and that configs can be functions
  allow the user's $docsify config to be a function that receives as input the Docsify instance
  • Loading branch information
trusktr committed Jun 13, 2020
2 parents 3dd37ea + da9af18 commit 170bc87
Show file tree
Hide file tree
Showing 15 changed files with 321 additions and 106 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"eslint-plugin-prettier": "^3.1.2",
"esm": "^3.1.4",
"husky": "^3.1.0",
"jsdom": "^15.1.1",
"jsdom": "^16.2.2",
"lerna": "^3.17.0",
"lint-staged": "^10.1.2",
"live-server": "^1.2.1",
Expand All @@ -97,6 +97,7 @@
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-uglify": "^6.0.4",
"serve-handler": "^6.1.2",
"start-server-and-test": "^1.10.6",
"stylus": "^0.54.5"
},
Expand Down
23 changes: 23 additions & 0 deletions src/core/Docsify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { initMixin } from './init';
import { routerMixin } from './router';
import { renderMixin } from './render';
import { fetchMixin } from './fetch';
import { eventMixin } from './event';
import initGlobalAPI from './global-api';

export function Docsify() {
this._init();
}

const proto = Docsify.prototype;

initMixin(proto);
routerMixin(proto);
renderMixin(proto);
fetchMixin(proto);
eventMixin(proto);

/**
* Global API
*/
initGlobalAPI();
6 changes: 4 additions & 2 deletions src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { merge, hyphenate, isPrimitive, hasOwn } from './util/core';

const currentScript = document.currentScript;

export default function() {
export default function(vm) {
const config = merge(
{
el: '#app',
Expand Down Expand Up @@ -36,7 +36,9 @@ export default function() {
relativePath: false,
topMargin: 0,
},
window.$docsify
typeof window.$docsify === 'function'
? window.$docsify(vm)
: window.$docsify
);

const script =
Expand Down
4 changes: 4 additions & 0 deletions src/core/global-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { Compiler } from './render/compiler';
import { slugify } from './render/slugify';
import { get } from './fetch/ajax';

// TODO This is deprecated, kept for backwards compatibility. Remove in next
// major release. We'll tell people to get everything from the DOCSIFY global
// when using the global build, but we'll highly recommend for them to import
// from the ESM build (f.e. lib/docsify.esm.js and lib/docsify.min.esm.js).
export default function() {
window.Docsify = {
util,
Expand Down
43 changes: 3 additions & 40 deletions src/core/index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,8 @@
import { initMixin } from './init';
import { routerMixin } from './router';
import { renderMixin } from './render';
import { fetchMixin } from './fetch';
import { eventMixin } from './event';
import initGlobalAPI from './global-api';

/**
* Fork https://github.com/bendrucker/document-ready/blob/master/index.js
* @param {Function} callback The callbacack to be called when the page is loaded
* @returns {Number|void} If the page is already laoded returns the result of the setTimeout callback,
* otherwise it only attaches the callback to the DOMContentLoaded event
*/
function ready(callback) {
const state = document.readyState;

if (state === 'complete' || state === 'interactive') {
return setTimeout(callback, 0);
}

document.addEventListener('DOMContentLoaded', callback);
}

function Docsify() {
this._init();
}

const proto = Docsify.prototype;

initMixin(proto);
routerMixin(proto);
renderMixin(proto);
fetchMixin(proto);
eventMixin(proto);

/**
* Global API
*/
initGlobalAPI();
import { documentReady } from './util/dom';
import { Docsify } from './Docsify';

/**
* Run Docsify
*/
// eslint-disable-next-line no-unused-vars
ready(_ => new Docsify());
documentReady(_ => new Docsify());
Loading

0 comments on commit 170bc87

Please sign in to comment.