Skip to content

Commit 1b8a81a

Browse files
authored
Merge pull request #1192 from docsifyjs/add-build-error-handling
Add build error handling
2 parents 5a5b5aa + b40baae commit 1b8a81a

File tree

10 files changed

+556
-268
lines changed

10 files changed

+556
-268
lines changed

build/build.js

Lines changed: 75 additions & 49 deletions
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,37 @@ 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+
globalName: 'DOCSIFY'
54+
}))
4555

4656
if (isProd) {
47-
build({
57+
promises.push(build({
4858
input: 'src/core/index.js',
4959
output: 'docsify.min.js',
60+
globalName: 'DOCSIFY',
5061
plugins: [uglify()]
51-
})
62+
}))
5263
}
64+
65+
await Promise.all(promises)
5366
}
54-
const buildAllPlugin = function () {
67+
68+
async function buildAllPlugin() {
5569
var plugins = [
5670
{name: 'search', input: 'search/index.js'},
5771
{name: 'ga', input: 'ga.js'},
@@ -64,56 +78,68 @@ const buildAllPlugin = function () {
6478
{name: 'gitalk', input: 'gitalk.js'}
6579
]
6680

67-
plugins.forEach(item => {
68-
build({
81+
const promises = plugins.map(item => {
82+
return build({
6983
input: 'src/plugins/' + item.input,
7084
output: 'plugins/' + item.name + '.js'
7185
})
7286
})
7387

7488
if (isProd) {
7589
plugins.forEach(item => {
76-
build({
90+
promises.push(build({
7791
input: 'src/plugins/' + item.input,
7892
output: 'plugins/' + item.name + '.min.js',
7993
plugins: [uglify()]
80-
})
94+
}))
8195
})
8296
}
97+
98+
await Promise.all(promises)
8399
}
84100

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`
101+
async function main() {
102+
if (!isProd) {
103+
chokidar
104+
.watch(['src/core', 'src/plugins'], {
105+
atomic: true,
106+
awaitWriteFinish: {
107+
stabilityThreshold: 1000,
108+
pollInterval: 100
109+
}
110+
})
111+
.on('change', p => {
112+
console.log('[watch] ', p)
113+
const dirs = p.split(path.sep)
114+
if (dirs[1] === 'core') {
115+
buildCore()
116+
} else if (dirs[2]) {
117+
const name = path.basename(dirs[2], '.js')
118+
const input = `src/plugins/${name}${
119+
/\.js/.test(dirs[2]) ? '' : '/index'
120+
}.js`
104121

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

build/css.js

Lines changed: 10 additions & 3 deletions
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,7 +31,14 @@ 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+
} else {
40+
console.log(message);
41+
}
3542
});
3643
} else {
3744
return

build/mincss.js

Lines changed: 3 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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
})

0 commit comments

Comments
 (0)