Skip to content

Commit 679da9f

Browse files
author
Ryan Christian
committed
refactor: Format outputs are named entirely separately
1 parent fb0a437 commit 679da9f

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

src/index.js

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ export default async function microbundle(inputOptions) {
8989
options.output = await getOutput({
9090
cwd,
9191
output: options.output,
92-
pkgMain: options.pkg.main,
9392
pkgName: options.pkg.name,
9493
});
9594

@@ -138,10 +137,9 @@ export default async function microbundle(inputOptions) {
138137
}),
139138
);
140139

141-
const targetDir = relative(cwd, dirname(options.output)) || '.';
142140
const sourceExist = options.input.length > 0;
143141
const banner = sourceExist
144-
? blue(`Build "${options.pkg.name}" to ${targetDir}:`)
142+
? blue(`Built "${options.pkg.name}":`)
145143
: red(`Error: No entry module found for "${options.pkg.name}"`);
146144
return {
147145
output: `${banner}\n ${out.join('\n ')}`,
@@ -225,8 +223,8 @@ async function getInput({ entries, cwd, source, module }) {
225223
return input;
226224
}
227225

228-
async function getOutput({ cwd, output, pkgMain, pkgName }) {
229-
let main = resolve(cwd, output || pkgMain || 'dist');
226+
async function getOutput({ cwd, output, pkgName }) {
227+
let main = resolve(cwd, output || 'dist');
230228
if (!main.match(/\.[a-z]+$/) || (await isDir(main))) {
231229
main = resolve(main, `${removeScope(pkgName)}.js`);
232230
}
@@ -261,13 +259,6 @@ async function getEntries({ input, cwd }) {
261259
return entries;
262260
}
263261

264-
function replaceName(filename, name) {
265-
return resolve(
266-
dirname(filename),
267-
name + basename(filename).replace(/^[^.]+/, ''),
268-
);
269-
}
270-
271262
function walk(exports, includeDefault) {
272263
if (!exports) return null;
273264
if (typeof exports === 'string') return exports;
@@ -279,42 +270,45 @@ function walk(exports, includeDefault) {
279270
function getMain({ options, entry, format }) {
280271
const { pkg } = options;
281272
const pkgMain = options['pkg-main'];
273+
const pkgTypeModule = pkg.type === 'module';
274+
let multipleEntries = options.multipleEntries;
282275

283276
if (!pkgMain) {
284277
return options.output;
285278
}
286279

287-
let mainNoExtension = options.output;
288-
if (options.multipleEntries) {
280+
let defaultOutput = options.output;
281+
if (multipleEntries) {
289282
let name = entry.match(new RegExp(/([\\/])index/.source + EXTENSION.source))
290-
? mainNoExtension
283+
? defaultOutput
291284
: entry;
292-
mainNoExtension = resolve(dirname(mainNoExtension), basename(name));
285+
defaultOutput = resolve(dirname(defaultOutput), basename(name));
293286
}
294-
mainNoExtension = mainNoExtension.replace(EXTENSION, '');
287+
const defaultOutputNoExtension = defaultOutput.replace(EXTENSION, '');
295288

296289
const mainsByFormat = {};
297290

298-
mainsByFormat.es = replaceName(
299-
pkg.module && !pkg.module.match(/src\//)
300-
? pkg.module
301-
: pkg['jsnext:main'] || 'x.esm.js',
302-
mainNoExtension,
291+
mainsByFormat.es = resolve(
292+
(!multipleEntries &&
293+
(pkg.module && !pkg.module.match(/src\//)
294+
? pkg.module
295+
: pkg['jsnext:main'])) ||
296+
`${defaultOutputNoExtension}.esm.js`,
303297
);
304-
mainsByFormat.modern = replaceName(
305-
(pkg.exports && walk(pkg.exports, pkg.type === 'module')) ||
306-
(pkg.syntax && pkg.syntax.esmodules) ||
307-
pkg.esmodule ||
308-
'x.modern.js',
309-
mainNoExtension,
298+
mainsByFormat.modern = resolve(
299+
(!multipleEntries &&
300+
((pkg.exports && walk(pkg.exports, pkgTypeModule)) ||
301+
(pkg.syntax && pkg.syntax.esmodules) ||
302+
pkg.esmodule)) ||
303+
`${defaultOutputNoExtension}.modern.${pkgTypeModule ? 'js' : 'mjs'}`,
310304
);
311-
mainsByFormat.cjs = replaceName(
312-
pkg['cjs:main'] || (pkg.type && pkg.type === 'module' ? 'x.cjs' : 'x.js'),
313-
mainNoExtension,
305+
mainsByFormat.cjs = resolve(
306+
(!multipleEntries && (pkg['cjs:main'] || pkg.main)) ||
307+
`${defaultOutputNoExtension}.${pkgTypeModule ? 'cjs' : 'js'}`,
314308
);
315-
mainsByFormat.umd = replaceName(
316-
pkg['umd:main'] || pkg.unpkg || 'x.umd.js',
317-
mainNoExtension,
309+
mainsByFormat.umd = resolve(
310+
(!multipleEntries && (pkg['umd:main'] || pkg.unpkg)) ||
311+
`${defaultOutputNoExtension}.umd.js`,
318312
);
319313

320314
return mainsByFormat[format] || mainsByFormat.cjs;

0 commit comments

Comments
 (0)