@@ -9,8 +9,16 @@ 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+ /**
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- / \. j s / . 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+ / \. j s / . 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+
0 commit comments