@@ -90,7 +90,6 @@ export default async function microbundle(inputOptions) {
9090 options . output = await getOutput ( {
9191 cwd,
9292 output : options . output ,
93- pkgMain : options . pkg . main ,
9493 pkgName : options . pkg . name ,
9594 } ) ;
9695
@@ -139,10 +138,9 @@ export default async function microbundle(inputOptions) {
139138 } ) ,
140139 ) ;
141140
142- const targetDir = relative ( cwd , dirname ( options . output ) ) || '.' ;
143141 const sourceExist = options . input . length > 0 ;
144142 const banner = sourceExist
145- ? blue ( `Build "${ options . pkg . name } " to ${ targetDir } :` )
143+ ? blue ( `Built "${ options . pkg . name } ":` )
146144 : red ( `Error: No entry module found for "${ options . pkg . name } "` ) ;
147145 return {
148146 output : `${ banner } \n${ out . join ( '\n' ) } ` ,
@@ -228,8 +226,8 @@ async function getInput({ entries, cwd, source, module }) {
228226 return input ;
229227}
230228
231- async function getOutput ( { cwd, output, pkgMain , pkgName } ) {
232- let main = resolve ( cwd , output || pkgMain || 'dist' ) ;
229+ async function getOutput ( { cwd, output, pkgName } ) {
230+ let main = resolve ( cwd , output || 'dist' ) ;
233231 if ( ! main . match ( / \. [ a - z ] + $ / ) || ( await isDir ( main ) ) ) {
234232 main = resolve ( main , `${ removeScope ( pkgName ) } .js` ) ;
235233 }
@@ -264,13 +262,6 @@ async function getEntries({ input, cwd }) {
264262 return entries ;
265263}
266264
267- function replaceName ( filename , name ) {
268- return resolve (
269- dirname ( filename ) ,
270- name + basename ( filename ) . replace ( / ^ [ ^ . ] + / , '' ) ,
271- ) ;
272- }
273-
274265function walk ( exports , includeDefault ) {
275266 if ( ! exports ) return null ;
276267 if ( typeof exports === 'string' ) return exports ;
@@ -283,47 +274,44 @@ function getMain({ options, entry, format }) {
283274 const { pkg } = options ;
284275 const pkgMain = options [ 'pkg-main' ] ;
285276 const pkgTypeModule = pkg . type === 'module' ;
277+ let multipleEntries = options . multipleEntries ;
286278
287279 if ( ! pkgMain ) {
288280 return options . output ;
289281 }
290282
291- let mainNoExtension = options . output ;
292- if ( options . multipleEntries ) {
283+ let defaultOutput = options . output ;
284+ if ( multipleEntries ) {
293285 let name = entry . match ( new RegExp ( / ( [ \\ / ] ) i n d e x / . source + EXTENSION . source ) )
294- ? mainNoExtension
286+ ? defaultOutput
295287 : entry ;
296- mainNoExtension = resolve ( dirname ( mainNoExtension ) , basename ( name ) ) ;
288+ defaultOutput = resolve ( dirname ( defaultOutput ) , basename ( name ) ) ;
297289 }
298- mainNoExtension = mainNoExtension . replace ( EXTENSION , '' ) ;
290+ const defaultOutputNoExtension = defaultOutput . replace ( EXTENSION , '' ) ;
299291
300292 const mainsByFormat = { } ;
301293
302- mainsByFormat . es = replaceName (
303- pkg . module && ! pkg . module . match ( / s r c \/ / )
304- ? pkg . module
305- : pkg [ 'jsnext:main' ] || pkgTypeModule
306- ? 'x.esm.js'
307- : 'x.esm.mjs' ,
308- mainNoExtension ,
294+ mainsByFormat . es = resolve (
295+ ( ! multipleEntries &&
296+ ( pkg . module && ! pkg . module . match ( / s r c \/ / )
297+ ? pkg . module
298+ : pkg [ 'jsnext:main' ] ) ) ||
299+ `${ defaultOutputNoExtension } .esm.${ pkgTypeModule ? 'js' : 'mjs' } ` ,
309300 ) ;
310-
311- mainsByFormat . modern = replaceName (
312- ( pkg . exports && walk ( pkg . exports , pkgTypeModule ) ) ||
313- ( pkg . syntax && pkg . syntax . esmodules ) ||
314- pkg . esmodule ||
315- pkgTypeModule
316- ? 'x.modern.js'
317- : 'x.modern.mjs' ,
318- mainNoExtension ,
301+ mainsByFormat . modern = resolve (
302+ ( ! multipleEntries &&
303+ ( ( pkg . exports && walk ( pkg . exports , pkgTypeModule ) ) ||
304+ ( pkg . syntax && pkg . syntax . esmodules ) ||
305+ pkg . esmodule ) ) ||
306+ `${ defaultOutputNoExtension } .modern.${ pkgTypeModule ? 'js' : 'mjs' } ` ,
319307 ) ;
320- mainsByFormat . cjs = replaceName (
321- pkg [ 'cjs:main' ] || ( pkgTypeModule ? 'x.cjs' : 'x.js' ) ,
322- mainNoExtension ,
308+ mainsByFormat . cjs = resolve (
309+ ( ! multipleEntries && ( pkg [ 'cjs:main' ] || pkg . main ) ) ||
310+ ` ${ defaultOutputNoExtension } . ${ pkgTypeModule ? 'cjs' : 'js' } ` ,
323311 ) ;
324- mainsByFormat . umd = replaceName (
325- pkg [ 'umd:main' ] || pkg . unpkg || 'x.umd.js' ,
326- mainNoExtension ,
312+ mainsByFormat . umd = resolve (
313+ ( ! multipleEntries && ( pkg [ 'umd:main' ] || pkg . unpkg ) ) ||
314+ ` ${ defaultOutputNoExtension } .umd.js` ,
327315 ) ;
328316
329317 return mainsByFormat [ format ] || mainsByFormat . cjs ;
0 commit comments