Skip to content

Commit 7589cd0

Browse files
committed
refactor: Format outputs are named entirely separately
1 parent 22187fb commit 7589cd0

File tree

1 file changed

+27
-35
lines changed

1 file changed

+27
-35
lines changed

src/index.js

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -134,10 +133,9 @@ export default async function microbundle(inputOptions) {
134133
}),
135134
);
136135

137-
const targetDir = relative(cwd, dirname(options.output)) || '.';
138136
const sourceExist = options.input.length > 0;
139137
const banner = sourceExist
140-
? blue(`Build "${options.pkg.name}" to ${targetDir}:`)
138+
? blue(`Built "${options.pkg.name}":`)
141139
: red(`Error: No entry module found for "${options.pkg.name}"`);
142140
return {
143141
output: `${banner}\n${out.join('\n')}`,
@@ -223,8 +221,8 @@ async function getInput({ entries, cwd, source, module }) {
223221
return input;
224222
}
225223

226-
async function getOutput({ cwd, output, pkgMain, pkgName }) {
227-
let main = resolve(cwd, output || pkgMain || 'dist');
224+
async function getOutput({ cwd, output, pkgName }) {
225+
let main = resolve(cwd, output || 'dist');
228226
if (!main.match(/\.[a-z]+$/) || (await isDir(main))) {
229227
main = resolve(main, `${removeScope(pkgName)}.js`);
230228
}
@@ -259,13 +257,6 @@ async function getEntries({ input, cwd }) {
259257
return entries;
260258
}
261259

262-
function replaceName(filename, name) {
263-
return resolve(
264-
dirname(filename),
265-
name + basename(filename).replace(/^[^.]+/, ''),
266-
);
267-
}
268-
269260
function walk(exports, includeDefault) {
270261
if (!exports) return null;
271262
if (typeof exports === 'string') return exports;
@@ -278,43 +269,44 @@ function getMain({ options, entry, format }) {
278269
const { pkg } = options;
279270
const pkgMain = options['pkg-main'];
280271
const pkgTypeModule = pkg.type === 'module';
272+
let multipleEntries = options.multipleEntries;
281273

282274
if (!pkgMain) {
283275
return options.output;
284276
}
285277

286-
let mainNoExtension = options.output;
287-
if (options.multipleEntries) {
278+
let defaultOutput = options.output;
279+
if (multipleEntries) {
288280
let name = entry.match(new RegExp(/([\\/])index/.source + EXTENSION.source))
289-
? mainNoExtension
281+
? defaultOutput
290282
: entry;
291-
mainNoExtension = resolve(dirname(mainNoExtension), basename(name));
283+
defaultOutput = resolve(dirname(defaultOutput), basename(name));
292284
}
293-
mainNoExtension = mainNoExtension.replace(EXTENSION, '');
285+
const defaultOutputNoExtension = defaultOutput.replace(EXTENSION, '');
294286

295287
const mainsByFormat = {};
296288

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

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

0 commit comments

Comments
 (0)