@@ -9,8 +9,8 @@ const version = process.env.VERSION || require('../package.json').version
99const chokidar = require ( 'chokidar' )
1010const path = require ( 'path' )
1111
12- const build = function ( opts ) {
13- rollup
12+ async function build ( opts ) {
13+ await rollup
1414 . rollup ( {
1515 input : opts . input ,
1616 plugins : ( opts . plugins || [ ] ) . concat ( [
@@ -27,31 +27,34 @@ const build = function (opts) {
2727 var dest = 'lib/' + ( opts . output || opts . input )
2828
2929 console . log ( dest )
30- bundle . write ( {
30+ return bundle . write ( {
3131 format : 'iife' ,
3232 file : dest ,
3333 strict : false
3434 } )
3535 } )
36- . catch ( function ( err ) {
37- console . error ( err )
38- } )
3936}
40- const buildCore = function ( ) {
41- build ( {
37+
38+ async function buildCore ( ) {
39+ const promises = [ ]
40+
41+ promises . push ( build ( {
4242 input : 'src/core/index.js' ,
4343 output : 'docsify.js'
44- } )
44+ } ) )
4545
4646 if ( isProd ) {
47- build ( {
47+ promises . push ( build ( {
4848 input : 'src/core/index.js' ,
4949 output : 'docsify.min.js' ,
5050 plugins : [ uglify ( ) ]
51- } )
51+ } ) )
5252 }
53+
54+ await Promise . all ( promises )
5355}
54- const buildAllPlugin = function ( ) {
56+
57+ async function buildAllPlugin ( ) {
5558 var plugins = [
5659 { name : 'search' , input : 'search/index.js' } ,
5760 { name : 'ga' , input : 'ga.js' } ,
@@ -64,56 +67,68 @@ const buildAllPlugin = function () {
6467 { name : 'gitalk' , input : 'gitalk.js' }
6568 ]
6669
67- plugins . forEach ( item => {
68- build ( {
70+ const promises = plugins . map ( item => {
71+ return build ( {
6972 input : 'src/plugins/' + item . input ,
7073 output : 'plugins/' + item . name + '.js'
7174 } )
7275 } )
7376
7477 if ( isProd ) {
7578 plugins . forEach ( item => {
76- build ( {
79+ promises . push ( build ( {
7780 input : 'src/plugins/' + item . input ,
7881 output : 'plugins/' + item . name + '.min.js' ,
7982 plugins : [ uglify ( ) ]
80- } )
83+ } ) )
8184 } )
8285 }
86+
87+ await Promise . all ( promises )
8388}
8489
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`
104110
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 ( ) ,
114125 buildAllPlugin ( )
115- } )
116- } else {
117- buildCore ( )
118- buildAllPlugin ( )
126+ ] )
127+ }
119128}
129+
130+ main ( ) . catch ( ( e ) => {
131+ console . error ( e )
132+ process . exit ( 1 )
133+ } )
134+
0 commit comments