Skip to content

Commit

Permalink
refac: dts and else outputs as one
Browse files Browse the repository at this point in the history
  • Loading branch information
devjiwonchoi committed Nov 18, 2023
1 parent 290b03e commit 6486090
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions src/build-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,10 @@ async function buildConfig(
tsOptions,
dts,
)
const outputExports = getExportConditionDist(pkg, exportCondition, cwd)
const typeOutputExports = getExportTypeDist(pkg, exportCondition, cwd)

let outputConfigs = []
const outputExports: Array<string | { format: 'cjs' | 'esm'; file: string }> =
dts
? getExportTypeDist(exportCondition, cwd)
: getExportConditionDist(pkg, exportCondition, cwd)

if (pkg.bin) {
const isSinglePathBin = typeof pkg.bin === 'string'
Expand All @@ -460,34 +460,38 @@ async function buildConfig(

for (const binDistPath of binDistPaths) {
const ext = extname(binDistPath).slice(1) as 'js' | 'cjs' | 'mjs'
const dtsExt = dtsExtentions[ext]

const filename = filenameWithoutExtension(binDistPath) ?? ''
const binTypeFile = `${filename}${dtsExt}`
if (dts) {
const dtsExt = dtsExtentions[ext]
const filename = filenameWithoutExtension(binDistPath) ?? ''
const binTypeFile = `${filename}${dtsExt}`

// ESM by default, CJS if the file extension is .cjs
const isCJS = ext === 'cjs'

outputExports.push({
format: isCJS ? 'cjs' : 'esm',
file: binDistPath,
})
outputExports.push(binTypeFile)
} else {
// ESM by default, CJS if the file extension is .cjs
const isCJS = ext === 'cjs'

typeOutputExports.push(binTypeFile)
outputExports.push({
format: isCJS ? 'cjs' : 'esm',
file: binDistPath,
})
}
}
}

let outputConfigs = []

// Generate dts job - single config
if (dts) {
outputConfigs = typeOutputExports.map((v) =>
outputConfigs = outputExports.map((v) =>
buildOutputConfigs(
pkg,
exportPaths,
{
...bundleConfig,
format: 'es',
useTypescript,
file: v,
file: v as string,
},
exportCondition,
cwd,
Expand All @@ -498,13 +502,18 @@ async function buildConfig(
} else {
// multi outputs with specified format
outputConfigs = outputExports.map((exportDist) => {
const { file, format } = exportDist as {
format: 'cjs' | 'esm'
file: string
}

return buildOutputConfigs(
pkg,
exportPaths,
{
...bundleConfig,
file: exportDist.file,
format: exportDist.format,
file,
format,
useTypescript,
},
exportCondition,
Expand All @@ -515,7 +524,7 @@ async function buildConfig(
})
// CLI output option is always prioritized
if (file) {
const fallbackFormat = outputExports[0]?.format
const fallbackFormat = (outputExports[0] as any)?.format
outputConfigs = [
buildOutputConfigs(
pkg,
Expand Down

0 comments on commit 6486090

Please sign in to comment.