-
Notifications
You must be signed in to change notification settings - Fork 361
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
}); | ||
|
||
|
@@ -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}":`) | ||
: red(`Error: No entry module found for "${options.pkg.name}"`); | ||
return { | ||
output: `${banner}\n${out.join('\n')}`, | ||
|
@@ -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`); | ||
} | ||
|
@@ -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; | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This can be seen with Microbundle itself; when we create a |
||
|
||
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\//) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Random q: What's this match against |
||
? 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; | ||
|
There was a problem hiding this comment.
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.