@@ -24,12 +24,7 @@ import { readFile, isDir, isFile, stdout, stderr } from './utils';
24
24
import camelCase from 'camelcase' ;
25
25
26
26
const removeScope = name => name . replace ( / ^ @ .* \/ / , '' ) ;
27
- const safeVariableName = name =>
28
- camelCase (
29
- removeScope ( name )
30
- . toLowerCase ( )
31
- . replace ( / ( ( ^ [ ^ a - z A - Z ] + ) | [ ^ \w . - ] ) | ( [ ^ a - z A - Z 0 - 9 ] + $ ) / g, '' ) ,
32
- ) ;
27
+
33
28
const parseGlobals = globalStrings => {
34
29
const globals = { } ;
35
30
globalStrings . split ( ',' ) . forEach ( globalString => {
@@ -53,105 +48,65 @@ function formatSize(size, filename, type, raw) {
53
48
) } : ${ chalk . white ( basename ( filename ) ) } .${ type } `;
54
49
}
55
50
56
- export default async function microbundle ( options ) {
57
- let cwd = ( options . cwd = resolve ( process . cwd ( ) , options . cwd ) ) ,
58
- hasPackageJson = true ;
51
+ export default async function microbundle ( inputOptions ) {
52
+ let options = { ...inputOptions } ;
59
53
60
- try {
61
- options . pkg = JSON . parse (
62
- await readFile ( resolve ( cwd , 'package.json' ) , 'utf8' ) ,
63
- ) ;
64
- } catch ( err ) {
65
- stderr (
66
- chalk . yellow (
67
- `${ chalk . yellow . inverse (
68
- 'WARN' ,
69
- ) } no package.json found. Assuming a pkg.name of "${ basename (
70
- options . cwd ,
71
- ) } ".`,
72
- ) ,
73
- ) ;
74
- let msg = String ( err . message || err ) ;
75
- if ( ! msg . match ( / E N O E N T / ) ) stderr ( ` ${ chalk . red . dim ( msg ) } ` ) ;
76
- options . pkg = { } ;
77
- hasPackageJson = false ;
78
- }
54
+ options . cwd = resolve ( process . cwd ( ) , inputOptions . cwd ) ;
55
+ const cwd = options . cwd ;
79
56
80
- if ( ! options . pkg . name ) {
81
- options . pkg . name = basename ( options . cwd ) ;
82
- if ( hasPackageJson ) {
83
- stderr (
84
- chalk . yellow (
85
- `${ chalk . yellow . inverse (
86
- 'WARN' ,
87
- ) } missing package.json "name" field. Assuming "${
88
- options . pkg . name
89
- } ".`,
90
- ) ,
91
- ) ;
92
- }
93
- }
57
+ const { hasPackageJson, pkg } = await getConfigFromPkgJson ( cwd ) ;
58
+ options . pkg = pkg ;
94
59
95
- options . name =
96
- options . name || options . pkg . amdName || safeVariableName ( options . pkg . name ) ;
60
+ const { finalName, pkgName } = getName ( {
61
+ name : options . name ,
62
+ pkgName : options . pkg . name ,
63
+ amdName : options . pkg . amdName ,
64
+ hasPackageJson,
65
+ cwd,
66
+ } ) ;
67
+
68
+ options . name = finalName ;
69
+ options . pkg . name = pkgName ;
97
70
98
71
if ( options . sourcemap !== false ) {
99
72
options . sourcemap = true ;
100
73
}
101
74
102
- const jsOrTs = async filename =>
103
- resolve (
104
- cwd ,
105
- `${ filename } ${
106
- ( await isFile ( resolve ( cwd , filename + '.ts' ) ) )
107
- ? '.ts'
108
- : ( await isFile ( resolve ( cwd , filename + '.tsx' ) ) )
109
- ? '.tsx'
110
- : '.js'
111
- } `,
112
- ) ;
113
-
114
- options . input = [ ] ;
115
- [ ]
116
- . concat (
117
- options . entries && options . entries . length
118
- ? options . entries
119
- : ( options . pkg . source && resolve ( cwd , options . pkg . source ) ) ||
120
- ( ( await isDir ( resolve ( cwd , 'src' ) ) ) &&
121
- ( await jsOrTs ( 'src/index' ) ) ) ||
122
- ( await jsOrTs ( 'index' ) ) ||
123
- options . pkg . module ,
124
- )
125
- . map ( file => glob ( file ) )
126
- . forEach ( file => options . input . push ( ...file ) ) ;
127
-
128
- let main = resolve ( cwd , options . output || options . pkg . main || 'dist' ) ;
129
- if ( ! main . match ( / \. [ a - z ] + $ / ) || ( await isDir ( main ) ) ) {
130
- main = resolve ( main , `${ removeScope ( options . pkg . name ) } .js` ) ;
131
- }
132
- options . output = main ;
75
+ options . input = await getInput ( {
76
+ entries : options . entries ,
77
+ cwd,
78
+ source : options . pkg . source ,
79
+ module : options . pkg . module ,
80
+ } ) ;
133
81
134
- let entries = ( await map ( [ ] . concat ( options . input ) , async file => {
135
- file = resolve ( cwd , file ) ;
136
- if ( await isDir ( file ) ) {
137
- file = resolve ( file , 'index.js' ) ;
138
- }
139
- return file ;
140
- } ) ) . filter ( ( item , i , arr ) => arr . indexOf ( item ) === i ) ;
82
+ options . output = await getOutput ( {
83
+ cwd,
84
+ output : options . output ,
85
+ pkgMain : options . pkg . main ,
86
+ pkgName : options . pkg . name ,
87
+ } ) ;
141
88
142
- options . entries = entries ;
89
+ options . entries = await getEntries ( {
90
+ cwd,
91
+ input : options . input ,
92
+ } ) ;
143
93
144
- options . multipleEntries = entries . length > 1 ;
94
+ options . multipleEntries = options . entries . length > 1 ;
145
95
146
96
let formats = ( options . format || options . formats ) . split ( ',' ) ;
147
97
// always compile cjs first if it's there:
148
98
formats . sort ( ( a , b ) => ( a === 'cjs' ? - 1 : a > b ? 1 : 0 ) ) ;
149
99
150
100
let steps = [ ] ;
151
- for ( let i = 0 ; i < entries . length ; i ++ ) {
101
+ for ( let i = 0 ; i < options . entries . length ; i ++ ) {
152
102
for ( let j = 0 ; j < formats . length ; j ++ ) {
153
103
steps . push (
154
- createConfig ( options , entries [ i ] , formats [ j ] , i === 0 && j === 0 ) ,
104
+ createConfig (
105
+ options ,
106
+ options . entries [ i ] ,
107
+ formats [ j ] ,
108
+ i === 0 && j === 0 ,
109
+ ) ,
155
110
) ;
156
111
}
157
112
}
@@ -225,6 +180,105 @@ export default async function microbundle(options) {
225
180
) ;
226
181
}
227
182
183
+ async function getConfigFromPkgJson ( cwd ) {
184
+ try {
185
+ const pkgJSON = await readFile ( resolve ( cwd , 'package.json' ) , 'utf8' ) ;
186
+ const pkg = JSON . parse ( pkgJSON ) ;
187
+
188
+ return {
189
+ hasPackageJson : true ,
190
+ pkg,
191
+ } ;
192
+ } catch ( err ) {
193
+ const pkgName = basename ( cwd ) ;
194
+
195
+ stderr (
196
+ chalk . yellow (
197
+ `${ chalk . yellow . inverse (
198
+ 'WARN' ,
199
+ ) } no package.json found. Assuming a pkg.name of "${ pkgName } ".`,
200
+ ) ,
201
+ ) ;
202
+
203
+ let msg = String ( err . message || err ) ;
204
+ if ( ! msg . match ( / E N O E N T / ) ) stderr ( ` ${ chalk . red . dim ( msg ) } ` ) ;
205
+
206
+ return { hasPackageJson : false , pkg : { name : pkgName } } ;
207
+ }
208
+ }
209
+
210
+ const safeVariableName = name =>
211
+ camelCase (
212
+ removeScope ( name )
213
+ . toLowerCase ( )
214
+ . replace ( / ( ( ^ [ ^ a - z A - Z ] + ) | [ ^ \w . - ] ) | ( [ ^ a - z A - Z 0 - 9 ] + $ ) / g, '' ) ,
215
+ ) ;
216
+
217
+ function getName ( { name, pkgName, amdName, cwd, hasPackageJson } ) {
218
+ if ( ! pkgName ) {
219
+ pkgName = basename ( cwd ) ;
220
+ if ( hasPackageJson ) {
221
+ stderr (
222
+ chalk . yellow (
223
+ `${ chalk . yellow . inverse (
224
+ 'WARN' ,
225
+ ) } missing package.json "name" field. Assuming "${ pkgName } ".`,
226
+ ) ,
227
+ ) ;
228
+ }
229
+ }
230
+
231
+ return { finalName : name || amdName || safeVariableName ( pkgName ) , pkgName } ;
232
+ }
233
+
234
+ async function jsOrTs ( cwd , filename ) {
235
+ const extension = ( await isFile ( resolve ( cwd , filename + '.ts' ) ) )
236
+ ? '.ts'
237
+ : ( await isFile ( resolve ( cwd , filename + '.tsx' ) ) )
238
+ ? '.tsx'
239
+ : '.js' ;
240
+
241
+ return resolve ( cwd , `${ filename } ${ extension } ` ) ;
242
+ }
243
+
244
+ async function getInput ( { entries, cwd, source, module } ) {
245
+ const input = [ ] ;
246
+
247
+ [ ]
248
+ . concat (
249
+ entries && entries . length
250
+ ? entries
251
+ : ( source && resolve ( cwd , source ) ) ||
252
+ ( ( await isDir ( resolve ( cwd , 'src' ) ) ) &&
253
+ ( await jsOrTs ( cwd , 'src/index' ) ) ) ||
254
+ ( await jsOrTs ( cwd , 'index' ) ) ||
255
+ module ,
256
+ )
257
+ . map ( file => glob ( file ) )
258
+ . forEach ( file => input . push ( ...file ) ) ;
259
+
260
+ return input ;
261
+ }
262
+
263
+ async function getOutput ( { cwd, output, pkgMain, pkgName } ) {
264
+ let main = resolve ( cwd , output || pkgMain || 'dist' ) ;
265
+ if ( ! main . match ( / \. [ a - z ] + $ / ) || ( await isDir ( main ) ) ) {
266
+ main = resolve ( main , `${ removeScope ( pkgName ) } .js` ) ;
267
+ }
268
+ return main ;
269
+ }
270
+
271
+ async function getEntries ( { input, cwd } ) {
272
+ let entries = ( await map ( [ ] . concat ( input ) , async file => {
273
+ file = resolve ( cwd , file ) ;
274
+ if ( await isDir ( file ) ) {
275
+ file = resolve ( file , 'index.js' ) ;
276
+ }
277
+ return file ;
278
+ } ) ) . filter ( ( item , i , arr ) => arr . indexOf ( item ) === i ) ;
279
+ return entries ;
280
+ }
281
+
228
282
function createConfig ( options , entry , format , writeMeta ) {
229
283
let { pkg } = options ;
230
284
0 commit comments