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 ;
1120
1221/**
1322 * @param {{
@@ -24,89 +33,96 @@ async function build(opts) {
2433 plugins : ( opts . plugins || [ ] ) . concat ( [
2534 buble ( {
2635 transforms : {
27- dangerousForOf : true
28- } } ) ,
36+ dangerousForOf : true ,
37+ } ,
38+ } ) ,
2939 commonjs ( ) ,
3040 nodeResolve ( ) ,
3141 replace ( {
3242 __VERSION__ : version ,
33- 'process.env.SSR' : false
34- } )
43+ 'process.env.SSR' : false ,
44+ } ) ,
3545 ] ) ,
36- onwarn : function ( message ) {
46+ onwarn : function ( message ) {
3747 if ( message . code === 'UNRESOLVED_IMPORT' ) {
3848 throw new Error (
3949 `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+ ) ;
4454 }
45- }
55+ } ,
4656 } )
47- . then ( function ( bundle ) {
48- var dest = 'lib/' + ( opts . output || opts . input )
57+ . then ( function ( bundle ) {
58+ var dest = 'lib/' + ( opts . output || opts . input ) ;
4959
50- console . log ( dest )
60+ console . log ( dest ) ;
5161 return bundle . write ( {
5262 format : 'iife' ,
53- output : opts . globalName ? { name : opts . globalName } : { } ,
63+ output : opts . globalName ? { name : opts . globalName } : { } ,
5464 file : dest ,
55- strict : false
56- } )
57- } )
65+ strict : false ,
66+ } ) ;
67+ } ) ;
5868}
5969
6070async function buildCore ( ) {
61- const promises = [ ]
71+ const promises = [ ] ;
6272
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+ ) ;
6779
6880 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+ ) ;
7488 }
7589
76- await Promise . all ( promises )
90+ await Promise . all ( promises ) ;
7791}
7892
7993async function buildAllPlugin ( ) {
8094 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+ ] ;
91105
92106 const promises = plugins . map ( item => {
93107 return build ( {
94108 input : 'src/plugins/' + item . input ,
95- output : 'plugins/' + item . name + '.js'
96- } )
97- } )
109+ output : 'plugins/' + item . name + '.js' ,
110+ } ) ;
111+ } ) ;
98112
99113 if ( isProd ) {
100114 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+ } ) ;
107123 }
108124
109- await Promise . all ( promises )
125+ await Promise . all ( promises ) ;
110126}
111127
112128async function main ( ) {
@@ -116,41 +132,37 @@ async function main() {
116132 atomic : true ,
117133 awaitWriteFinish : {
118134 stabilityThreshold : 1000 ,
119- pollInterval : 100
120- }
135+ pollInterval : 100 ,
136+ } ,
121137 } )
122138 . 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 ) ;
125141 if ( dirs [ 1 ] === 'core' ) {
126- buildCore ( )
142+ buildCore ( ) ;
127143 } else if ( dirs [ 2 ] ) {
128- const name = path . basename ( dirs [ 2 ] , '.js' )
144+ const name = path . basename ( dirs [ 2 ] , '.js' ) ;
129145 const input = `src/plugins/${ name } ${
130146 / \. j s / . test ( dirs [ 2 ] ) ? '' : '/index'
131- } .js`
147+ } .js`;
132148
133149 build ( {
134150 input,
135- output : 'plugins/' + name + '.js'
136- } )
151+ output : 'plugins/' + name + '.js' ,
152+ } ) ;
137153 }
138154 } )
139155 . on ( 'ready' , ( ) => {
140- console . log ( '[start]' )
141- buildCore ( )
142- buildAllPlugin ( )
143- } )
156+ console . log ( '[start]' ) ;
157+ buildCore ( ) ;
158+ buildAllPlugin ( ) ;
159+ } ) ;
144160 } else {
145- await Promise . all ( [
146- buildCore ( ) ,
147- buildAllPlugin ( )
148- ] )
161+ await Promise . all ( [ buildCore ( ) , buildAllPlugin ( ) ] ) ;
149162 }
150163}
151164
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