1
- const rollup = require ( 'rollup' )
2
- const buble = require ( 'rollup-plugin-buble' )
3
- const commonjs = require ( 'rollup-plugin-commonjs' )
4
- const nodeResolve = require ( 'rollup-plugin-node-resolve' )
5
- const { uglify } = require ( 'rollup-plugin-uglify' )
6
- const replace = require ( 'rollup-plugin-replace' )
7
- const isProd = process . env . NODE_ENV === 'production'
8
- const version = process . env . VERSION || require ( '../package.json' ) . version
9
- const chokidar = require ( 'chokidar' )
10
- const path = require ( 'path' )
1
+ // @ts -check
2
+
3
+ import rollup from 'rollup' ;
4
+ import buble from 'rollup-plugin-buble' ;
5
+ import commonjs from 'rollup-plugin-commonjs' ;
6
+ import nodeResolve from 'rollup-plugin-node-resolve' ;
7
+ import replace from 'rollup-plugin-replace' ;
8
+ import chokidar from 'chokidar' ;
9
+ import path from 'path' ;
10
+ import fs from 'fs' ;
11
+ import _uglify from 'rollup-plugin-uglify' ;
12
+
13
+ const { uglify } = _uglify ;
14
+ const isProd = process . env . NODE_ENV === 'production' ;
15
+ const dirname = path . dirname ( import . meta. url . replace ( 'file://' , '' ) ) ;
16
+ const version =
17
+ process . env . VERSION ||
18
+ JSON . parse ( fs . readFileSync ( path . resolve ( dirname , '..' , 'package.json' ) ) . toString ( ) )
19
+ . version ;
11
20
12
21
/**
13
22
* @param {{
@@ -24,89 +33,96 @@ async function build(opts) {
24
33
plugins : ( opts . plugins || [ ] ) . concat ( [
25
34
buble ( {
26
35
transforms : {
27
- dangerousForOf : true
28
- } } ) ,
36
+ dangerousForOf : true ,
37
+ } ,
38
+ } ) ,
29
39
commonjs ( ) ,
30
40
nodeResolve ( ) ,
31
41
replace ( {
32
42
__VERSION__ : version ,
33
- 'process.env.SSR' : false
34
- } )
43
+ 'process.env.SSR' : false ,
44
+ } ) ,
35
45
] ) ,
36
- onwarn : function ( message ) {
46
+ onwarn : function ( message ) {
37
47
if ( message . code === 'UNRESOLVED_IMPORT' ) {
38
48
throw new Error (
39
49
`Could not resolve module ` +
40
- message . source +
41
- `. Try running 'npm install' or using rollup's 'external' option if this is an external dependency. ` +
42
- `Module ${ message . source } is imported in ${ message . importer } `
43
- )
50
+ message . source +
51
+ `. Try running 'npm install' or using rollup's 'external' option if this is an external dependency. ` +
52
+ `Module ${ message . source } is imported in ${ message . importer } `
53
+ ) ;
44
54
}
45
- }
55
+ } ,
46
56
} )
47
- . then ( function ( bundle ) {
48
- var dest = 'lib/' + ( opts . output || opts . input )
57
+ . then ( function ( bundle ) {
58
+ var dest = 'lib/' + ( opts . output || opts . input ) ;
49
59
50
- console . log ( dest )
60
+ console . log ( dest ) ;
51
61
return bundle . write ( {
52
62
format : 'iife' ,
53
- output : opts . globalName ? { name : opts . globalName } : { } ,
63
+ output : opts . globalName ? { name : opts . globalName } : { } ,
54
64
file : dest ,
55
- strict : false
56
- } )
57
- } )
65
+ strict : false ,
66
+ } ) ;
67
+ } ) ;
58
68
}
59
69
60
70
async function buildCore ( ) {
61
- const promises = [ ]
71
+ const promises = [ ] ;
62
72
63
- promises . push ( build ( {
64
- input : 'src/core/index.js' ,
65
- output : 'docsify.js' ,
66
- } ) )
73
+ promises . push (
74
+ build ( {
75
+ input : 'src/core/index.js' ,
76
+ output : 'docsify.js' ,
77
+ } )
78
+ ) ;
67
79
68
80
if ( isProd ) {
69
- promises . push ( build ( {
70
- input : 'src/core/index.js' ,
71
- output : 'docsify.min.js' ,
72
- plugins : [ uglify ( ) ]
73
- } ) )
81
+ promises . push (
82
+ build ( {
83
+ input : 'src/core/index.js' ,
84
+ output : 'docsify.min.js' ,
85
+ plugins : [ uglify ( ) ] ,
86
+ } )
87
+ ) ;
74
88
}
75
89
76
- await Promise . all ( promises )
90
+ await Promise . all ( promises ) ;
77
91
}
78
92
79
93
async function buildAllPlugin ( ) {
80
94
var plugins = [
81
- { name : 'search' , input : 'search/index.js' } ,
82
- { name : 'ga' , input : 'ga.js' } ,
83
- { name : 'matomo' , input : 'matomo.js' } ,
84
- { name : 'emoji' , input : 'emoji.js' } ,
85
- { name : 'external-script' , input : 'external-script.js' } ,
86
- { name : 'front-matter' , input : 'front-matter/index.js' } ,
87
- { name : 'zoom-image' , input : 'zoom-image.js' } ,
88
- { name : 'disqus' , input : 'disqus.js' } ,
89
- { name : 'gitalk' , input : 'gitalk.js' }
90
- ]
95
+ { name : 'search' , input : 'search/index.js' } ,
96
+ { name : 'ga' , input : 'ga.js' } ,
97
+ { name : 'matomo' , input : 'matomo.js' } ,
98
+ { name : 'emoji' , input : 'emoji.js' } ,
99
+ { name : 'external-script' , input : 'external-script.js' } ,
100
+ { name : 'front-matter' , input : 'front-matter/index.js' } ,
101
+ { name : 'zoom-image' , input : 'zoom-image.js' } ,
102
+ { name : 'disqus' , input : 'disqus.js' } ,
103
+ { name : 'gitalk' , input : 'gitalk.js' } ,
104
+ ] ;
91
105
92
106
const promises = plugins . map ( item => {
93
107
return build ( {
94
108
input : 'src/plugins/' + item . input ,
95
- output : 'plugins/' + item . name + '.js'
96
- } )
97
- } )
109
+ output : 'plugins/' + item . name + '.js' ,
110
+ } ) ;
111
+ } ) ;
98
112
99
113
if ( isProd ) {
100
114
plugins . forEach ( item => {
101
- promises . push ( build ( {
102
- input : 'src/plugins/' + item . input ,
103
- output : 'plugins/' + item . name + '.min.js' ,
104
- plugins : [ uglify ( ) ]
105
- } ) )
106
- } )
115
+ promises . push (
116
+ build ( {
117
+ input : 'src/plugins/' + item . input ,
118
+ output : 'plugins/' + item . name + '.min.js' ,
119
+ plugins : [ uglify ( ) ] ,
120
+ } )
121
+ ) ;
122
+ } ) ;
107
123
}
108
124
109
- await Promise . all ( promises )
125
+ await Promise . all ( promises ) ;
110
126
}
111
127
112
128
async function main ( ) {
@@ -116,41 +132,37 @@ async function main() {
116
132
atomic : true ,
117
133
awaitWriteFinish : {
118
134
stabilityThreshold : 1000 ,
119
- pollInterval : 100
120
- }
135
+ pollInterval : 100 ,
136
+ } ,
121
137
} )
122
138
. on ( 'change' , p => {
123
- console . log ( '[watch] ' , p )
124
- const dirs = p . split ( path . sep )
139
+ console . log ( '[watch] ' , p ) ;
140
+ const dirs = p . split ( path . sep ) ;
125
141
if ( dirs [ 1 ] === 'core' ) {
126
- buildCore ( )
142
+ buildCore ( ) ;
127
143
} else if ( dirs [ 2 ] ) {
128
- const name = path . basename ( dirs [ 2 ] , '.js' )
144
+ const name = path . basename ( dirs [ 2 ] , '.js' ) ;
129
145
const input = `src/plugins/${ name } ${
130
146
/ \. j s / . test ( dirs [ 2 ] ) ? '' : '/index'
131
- } .js`
147
+ } .js`;
132
148
133
149
build ( {
134
150
input,
135
- output : 'plugins/' + name + '.js'
136
- } )
151
+ output : 'plugins/' + name + '.js' ,
152
+ } ) ;
137
153
}
138
154
} )
139
155
. on ( 'ready' , ( ) => {
140
- console . log ( '[start]' )
141
- buildCore ( )
142
- buildAllPlugin ( )
143
- } )
156
+ console . log ( '[start]' ) ;
157
+ buildCore ( ) ;
158
+ buildAllPlugin ( ) ;
159
+ } ) ;
144
160
} else {
145
- await Promise . all ( [
146
- buildCore ( ) ,
147
- buildAllPlugin ( )
148
- ] )
161
+ await Promise . all ( [ buildCore ( ) , buildAllPlugin ( ) ] ) ;
149
162
}
150
163
}
151
164
152
- main ( ) . catch ( ( e ) => {
153
- console . error ( e )
154
- process . exit ( 1 )
155
- } )
156
-
165
+ main ( ) . catch ( e => {
166
+ console . error ( e ) ;
167
+ process . exit ( 1 ) ;
168
+ } ) ;
0 commit comments