@@ -9,8 +9,16 @@ 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
+ /**
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
14
22
. rollup ( {
15
23
input : opts . input ,
16
24
plugins : ( opts . plugins || [ ] ) . concat ( [
@@ -27,31 +35,37 @@ const build = function (opts) {
27
35
var dest = 'lib/' + ( opts . output || opts . input )
28
36
29
37
console . log ( dest )
30
- bundle . write ( {
38
+ return bundle . write ( {
31
39
format : 'iife' ,
40
+ output : opts . globalName ? { name : opts . globalName } : { } ,
32
41
file : dest ,
33
42
strict : false
34
43
} )
35
44
} )
36
- . catch ( function ( err ) {
37
- console . error ( err )
38
- } )
39
45
}
40
- const buildCore = function ( ) {
41
- build ( {
46
+
47
+ async function buildCore ( ) {
48
+ const promises = [ ]
49
+
50
+ promises . push ( build ( {
42
51
input : 'src/core/index.js' ,
43
- output : 'docsify.js'
44
- } )
52
+ output : 'docsify.js' ,
53
+ globalName : 'DOCSIFY'
54
+ } ) )
45
55
46
56
if ( isProd ) {
47
- build ( {
57
+ promises . push ( build ( {
48
58
input : 'src/core/index.js' ,
49
59
output : 'docsify.min.js' ,
60
+ globalName : 'DOCSIFY' ,
50
61
plugins : [ uglify ( ) ]
51
- } )
62
+ } ) )
52
63
}
64
+
65
+ await Promise . all ( promises )
53
66
}
54
- const buildAllPlugin = function ( ) {
67
+
68
+ async function buildAllPlugin ( ) {
55
69
var plugins = [
56
70
{ name : 'search' , input : 'search/index.js' } ,
57
71
{ name : 'ga' , input : 'ga.js' } ,
@@ -64,56 +78,68 @@ const buildAllPlugin = function () {
64
78
{ name : 'gitalk' , input : 'gitalk.js' }
65
79
]
66
80
67
- plugins . forEach ( item => {
68
- build ( {
81
+ const promises = plugins . map ( item => {
82
+ return build ( {
69
83
input : 'src/plugins/' + item . input ,
70
84
output : 'plugins/' + item . name + '.js'
71
85
} )
72
86
} )
73
87
74
88
if ( isProd ) {
75
89
plugins . forEach ( item => {
76
- build ( {
90
+ promises . push ( build ( {
77
91
input : 'src/plugins/' + item . input ,
78
92
output : 'plugins/' + item . name + '.min.js' ,
79
93
plugins : [ uglify ( ) ]
80
- } )
94
+ } ) )
81
95
} )
82
96
}
97
+
98
+ await Promise . all ( promises )
83
99
}
84
100
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`
104
121
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 ( ) ,
114
136
buildAllPlugin ( )
115
- } )
116
- } else {
117
- buildCore ( )
118
- buildAllPlugin ( )
137
+ ] )
138
+ }
119
139
}
140
+
141
+ main ( ) . catch ( ( e ) => {
142
+ console . error ( e )
143
+ process . exit ( 1 )
144
+ } )
145
+
0 commit comments