Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allows naming of outputs individually #896

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/fair-adults-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'microbundle': minor
---

Microbundle's output formats are now entirely independendly named and respect output paths provided to them.

You can now export different formats in different directories if you wish.
62 changes: 27 additions & 35 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export default async function microbundle(inputOptions) {
options.output = await getOutput({
cwd,
output: options.output,
pkgMain: options.pkg.main,
pkgName: options.pkg.name,
});

Expand Down Expand Up @@ -134,10 +133,9 @@ export default async function microbundle(inputOptions) {
}),
);

const targetDir = relative(cwd, dirname(options.output)) || '.';
const sourceExist = options.input.length > 0;
const banner = sourceExist
? blue(`Build "${options.pkg.name}" to ${targetDir}:`)
? blue(`Built "${options.pkg.name}":`)
Comment on lines -140 to +138
Copy link
Collaborator Author

@rschristian rschristian Mar 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

targetDir is a bit unknowable now (each output format could theoretically have a different output directory, if the user so desired), so it's been removed.

: red(`Error: No entry module found for "${options.pkg.name}"`);
return {
output: `${banner}\n${out.join('\n')}`,
Expand Down Expand Up @@ -223,8 +221,8 @@ async function getInput({ entries, cwd, source, module }) {
return input;
}

async function getOutput({ cwd, output, pkgMain, pkgName }) {
let main = resolve(cwd, output || pkgMain || 'dist');
async function getOutput({ cwd, output, pkgName }) {
let main = resolve(cwd, output || 'dist');
if (!main.match(/\.[a-z]+$/) || (await isDir(main))) {
main = resolve(main, `${removeScope(pkgName)}.js`);
}
Expand Down Expand Up @@ -259,13 +257,6 @@ async function getEntries({ input, cwd }) {
return entries;
}

function replaceName(filename, name) {
return resolve(
dirname(filename),
name + basename(filename).replace(/^[^.]+/, ''),
);
}

function walk(exports, includeDefault) {
if (!exports) return null;
if (typeof exports === 'string') return exports;
Expand All @@ -278,43 +269,44 @@ function getMain({ options, entry, format }) {
const { pkg } = options;
const pkgMain = options['pkg-main'];
const pkgTypeModule = pkg.type === 'module';
let multipleEntries = options.multipleEntries;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multipleEntries needs to act as an escape hatch to our name resolution logic to ensure no collisions.

This can be seen with Microbundle itself; when we create a cli.js and microbundle.js, we need to ensure the assigned name of cli does not get replaced by the "main" field ("microbundle").


if (!pkgMain) {
return options.output;
}

let mainNoExtension = options.output;
if (options.multipleEntries) {
let defaultOutput = options.output;
if (multipleEntries) {
let name = entry.match(new RegExp(/([\\/])index/.source + EXTENSION.source))
? mainNoExtension
? defaultOutput
: entry;
mainNoExtension = resolve(dirname(mainNoExtension), basename(name));
defaultOutput = resolve(dirname(defaultOutput), basename(name));
}
mainNoExtension = mainNoExtension.replace(EXTENSION, '');
const defaultOutputNoExtension = defaultOutput.replace(EXTENSION, '');

const mainsByFormat = {};

mainsByFormat.es = replaceName(
pkg.module && !pkg.module.match(/src\//)
? pkg.module
: pkg['jsnext:main'] || (pkgTypeModule ? 'x.esm.js' : 'x.esm.mjs'),
mainNoExtension,
mainsByFormat.es = resolve(
(!multipleEntries &&
(pkg.module && !pkg.module.match(/src\//)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Random q: What's this match against src/ about? Goes back to repo init afaict so couldn't find any context there.

? pkg.module
: pkg['jsnext:main'])) ||
`${defaultOutputNoExtension}.esm.${pkgTypeModule ? 'js' : 'mjs'}`,
);

mainsByFormat.modern = replaceName(
(pkg.exports && walk(pkg.exports, pkgTypeModule)) ||
(pkg.syntax && pkg.syntax.esmodules) ||
pkg.esmodule ||
(pkgTypeModule ? 'x.modern.js' : 'x.modern.mjs'),
mainNoExtension,
mainsByFormat.modern = resolve(
(!multipleEntries &&
((pkg.exports && walk(pkg.exports, pkgTypeModule)) ||
(pkg.syntax && pkg.syntax.esmodules) ||
pkg.esmodule)) ||
`${defaultOutputNoExtension}.modern.${pkgTypeModule ? 'js' : 'mjs'}`,
);
mainsByFormat.cjs = replaceName(
pkg['cjs:main'] || (pkgTypeModule ? 'x.cjs' : 'x.js'),
mainNoExtension,
mainsByFormat.cjs = resolve(
(!multipleEntries && (pkg['cjs:main'] || pkg.main)) ||
`${defaultOutputNoExtension}.${pkgTypeModule ? 'cjs' : 'js'}`,
);
mainsByFormat.umd = replaceName(
pkg['umd:main'] || pkg.unpkg || 'x.umd.js',
mainNoExtension,
mainsByFormat.umd = resolve(
(!multipleEntries && (pkg['umd:main'] || pkg.unpkg)) ||
`${defaultOutputNoExtension}.umd.js`,
);

return mainsByFormat[format] || mainsByFormat.cjs;
Expand Down
Loading