Skip to content
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
9 changes: 6 additions & 3 deletions src/config/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ export async function resolveUserConfig(
(removeNodeProtocol ? 'strip' : false)

outDir = path.resolve(cwd, outDir)
clean = resolveClean(clean, outDir, cwd)

const rawEntry = entry
const [resolvedEntry, resolvedRoot] = await resolveEntry(
Expand Down Expand Up @@ -290,12 +289,11 @@ export async function resolveUserConfig(
}

/// keep-sorted
const config: Omit<ResolvedConfig, 'format'> = {
const config: Omit<ResolvedConfig, 'clean' | 'format'> = {
...userConfig,
alias,
attw,
cjsDefault,
clean,
configDeps,
copy: publicDir || copy,
css,
Expand Down Expand Up @@ -346,6 +344,9 @@ export async function resolveUserConfig(
const resolvedConfigs = formats.map((fmt, idx): ResolvedConfig => {
const once = idx === 0
const overrides = objectFormat ? format[fmt] : undefined
const formatOutDir = overrides?.outDir
? path.resolve(cwd, overrides.outDir)
: outDir
return {
...config,
// only copy once
Expand All @@ -354,6 +355,8 @@ export async function resolveUserConfig(
onSuccess: once ? config.onSuccess : undefined,
format: normalizeFormat(fmt),
...overrides,
outDir: formatOutDir,
clean: resolveClean(overrides?.clean ?? clean, formatOutDir, cwd),
}
})

Expand Down
2 changes: 1 addition & 1 deletion src/features/exe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function validateSea({
entry,
logger,
nameLabel,
}: Omit<ResolvedConfig, 'format'>): void {
}: Omit<ResolvedConfig, 'clean' | 'format'>): void {
if (process.versions.bun || process.versions.deno) {
throw new Error(
'The `exe` option is not supported in Bun and Deno environments.',
Expand Down
37 changes: 37 additions & 0 deletions tests/clean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,43 @@ describe('clean', () => {
expect(oldFileExists).toBe(false)
})

test('should clean per-format outDir when using object format', async (context) => {
const files = {
'index.ts': 'export const hello = "world"',
}

// Create per-format directories and stale files first
const testDir = getTestDir(context.task)
const esmPath = path.join(testDir, 'esm')
const cjsPath = path.join(testDir, 'cjs')

await mkdir(esmPath, { recursive: true })
await mkdir(cjsPath, { recursive: true })

await writeFile(path.join(esmPath, 'old-esm.js'), 'old esm')
await writeFile(path.join(cjsPath, 'old-cjs.js'), 'old cjs')

// Run build with per-format outDir overrides and clean: true
await testBuild({
context,
files,
options: {
clean: true,
format: {
esm: { outDir: 'esm' },
cjs: { outDir: 'cjs' },
},
},
snapshot: false,
})

// Verify each per-format outDir was cleaned and new output was produced
expect(await fsExists(path.join(esmPath, 'old-esm.js'))).toBe(false)
expect(await fsExists(path.join(cjsPath, 'old-cjs.js'))).toBe(false)
expect(await fsExists(path.join(esmPath, 'index.mjs'))).toBe(true)
expect(await fsExists(path.join(cjsPath, 'index.cjs'))).toBe(true)
})

test('should clean files with specific extensions', async (context) => {
const files = {
'index.ts': 'export const hello = "world"',
Expand Down
Loading