@@ -9,8 +9,8 @@ const version = process.env.VERSION || require('../package.json').version
9
9
const chokidar = require ( 'chokidar' )
10
10
const path = require ( 'path' )
11
11
12
- const build = function ( opts ) {
13
- rollup
12
+ async function build ( opts ) {
13
+ await rollup
14
14
. rollup ( {
15
15
input : opts . input ,
16
16
plugins : ( opts . plugins || [ ] ) . concat ( [
@@ -27,31 +27,34 @@ const build = function (opts) {
27
27
var dest = 'lib/' + ( opts . output || opts . input )
28
28
29
29
console . log ( dest )
30
- bundle . write ( {
30
+ return bundle . write ( {
31
31
format : 'iife' ,
32
32
file : dest ,
33
33
strict : false
34
34
} )
35
35
} )
36
- . catch ( function ( err ) {
37
- console . error ( err )
38
- } )
39
36
}
40
- const buildCore = function ( ) {
41
- build ( {
37
+
38
+ async function buildCore ( ) {
39
+ const promises = [ ]
40
+
41
+ promises . push ( build ( {
42
42
input : 'src/core/index.js' ,
43
43
output : 'docsify.js'
44
- } )
44
+ } ) )
45
45
46
46
if ( isProd ) {
47
- build ( {
47
+ promises . push ( build ( {
48
48
input : 'src/core/index.js' ,
49
49
output : 'docsify.min.js' ,
50
50
plugins : [ uglify ( ) ]
51
- } )
51
+ } ) )
52
52
}
53
+
54
+ await Promise . all ( promises )
53
55
}
54
- const buildAllPlugin = function ( ) {
56
+
57
+ async function buildAllPlugin ( ) {
55
58
var plugins = [
56
59
{ name : 'search' , input : 'search/index.js' } ,
57
60
{ name : 'ga' , input : 'ga.js' } ,
@@ -64,56 +67,68 @@ const buildAllPlugin = function () {
64
67
{ name : 'gitalk' , input : 'gitalk.js' }
65
68
]
66
69
67
- plugins . forEach ( item => {
68
- build ( {
70
+ const promises = plugins . map ( item => {
71
+ return build ( {
69
72
input : 'src/plugins/' + item . input ,
70
73
output : 'plugins/' + item . name + '.js'
71
74
} )
72
75
} )
73
76
74
77
if ( isProd ) {
75
78
plugins . forEach ( item => {
76
- build ( {
79
+ promises . push ( build ( {
77
80
input : 'src/plugins/' + item . input ,
78
81
output : 'plugins/' + item . name + '.min.js' ,
79
82
plugins : [ uglify ( ) ]
80
- } )
83
+ } ) )
81
84
} )
82
85
}
86
+
87
+ await Promise . all ( promises )
83
88
}
84
89
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
- / \. j s / . test ( dirs [ 2 ] ) ? '' : '/index'
103
- } .js`
90
+ async function main ( ) {
91
+ if ( ! isProd ) {
92
+ chokidar
93
+ . watch ( [ 'src/core' , 'src/plugins' ] , {
94
+ atomic : true ,
95
+ awaitWriteFinish : {
96
+ stabilityThreshold : 1000 ,
97
+ pollInterval : 100
98
+ }
99
+ } )
100
+ . on ( 'change' , p => {
101
+ console . log ( '[watch] ' , p )
102
+ const dirs = p . split ( path . sep )
103
+ if ( dirs [ 1 ] === 'core' ) {
104
+ buildCore ( )
105
+ } else if ( dirs [ 2 ] ) {
106
+ const name = path . basename ( dirs [ 2 ] , '.js' )
107
+ const input = `src/plugins/${ name } ${
108
+ / \. j s / . test ( dirs [ 2 ] ) ? '' : '/index'
109
+ } .js`
104
110
105
- build ( {
106
- input,
107
- output : 'plugins/' + name + '.js'
108
- } )
109
- }
110
- } )
111
- . on ( 'ready' , ( ) => {
112
- console . log ( '[start]' )
113
- buildCore ( )
111
+ build ( {
112
+ input,
113
+ output : 'plugins/' + name + '.js'
114
+ } )
115
+ }
116
+ } )
117
+ . on ( 'ready' , ( ) => {
118
+ console . log ( '[start]' )
119
+ buildCore ( )
120
+ buildAllPlugin ( )
121
+ } )
122
+ } else {
123
+ await Promise . all ( [
124
+ buildCore ( ) ,
114
125
buildAllPlugin ( )
115
- } )
116
- } else {
117
- buildCore ( )
118
- buildAllPlugin ( )
126
+ ] )
127
+ }
119
128
}
129
+
130
+ main ( ) . catch ( ( e ) => {
131
+ console . error ( e )
132
+ process . exit ( 1 )
133
+ } )
134
+
0 commit comments