Skip to content

Commit f503f25

Browse files
committed
feat: add cjsDefault option
closes #417
1 parent f234fcf commit f503f25

File tree

5 files changed

+40
-14
lines changed

5 files changed

+40
-14
lines changed

pnpm-lock.yaml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ catalogs:
4646
empathic: ^2.0.0
4747
hookable: ^5.5.3
4848
rolldown: ^1.0.0-beta.31
49-
rolldown-plugin-dts: ^0.15.3
49+
rolldown-plugin-dts: ^0.15.6
5050
semver: ^7.7.2
5151
tinyexec: ^1.0.1
5252
tinyglobby: ^0.2.14

src/features/rolldown.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export async function resolveInputOptions(
8383
loader,
8484
name,
8585
logger,
86+
cjsDefault,
8687
} = config
8788

8889
const plugins: RolldownPluginOption = []
@@ -102,7 +103,13 @@ export async function resolveInputOptions(
102103
if (format === 'es') {
103104
plugins.push(dtsPlugin(options))
104105
} else if (cjsDts) {
105-
plugins.push(dtsPlugin({ ...options, emitDtsOnly: true }))
106+
plugins.push(
107+
dtsPlugin({
108+
...options,
109+
emitDtsOnly: true,
110+
cjsDefault,
111+
}),
112+
)
106113
}
107114
}
108115
if (!cjsDts) {
@@ -171,7 +178,16 @@ export async function resolveOutputOptions(
171178
format: NormalizedFormat,
172179
cjsDts: boolean,
173180
): Promise<OutputOptions> {
174-
const { entry, outDir, sourcemap, minify, unbundle, banner, footer } = config
181+
const {
182+
entry,
183+
outDir,
184+
sourcemap,
185+
minify,
186+
unbundle,
187+
banner,
188+
footer,
189+
cjsDefault,
190+
} = config
175191

176192
const [entryFileNames, chunkFileNames] = resolveChunkFilename(
177193
config,
@@ -184,6 +200,7 @@ export async function resolveOutputOptions(
184200
name: config.globalName,
185201
sourcemap,
186202
dir: outDir,
203+
exports: cjsDefault ? 'auto' : 'named',
187204
minify: !cjsDts && minify,
188205
entryFileNames,
189206
chunkFileNames,

src/options/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ async function resolveConfig(
205205
unbundle = typeof bundle === 'boolean' ? !bundle : false,
206206
removeNodeProtocol,
207207
nodeProtocol,
208+
cjsDefault = true,
208209
} = userConfig
209210

210211
const logger = createLogger(logLevel, { customLogger, failOnWarn })
@@ -325,6 +326,7 @@ async function resolveConfig(
325326
exports,
326327
unbundle,
327328
nodeProtocol,
329+
cjsDefault,
328330
}
329331

330332
return config

src/options/types.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export interface Workspace {
102102
* Options for tsdown.
103103
*/
104104
export interface Options {
105-
/// build options
105+
// #region Input Options
106106
/**
107107
* Defaults to `'src/index.ts'` if it exists.
108108
*/
@@ -136,7 +136,8 @@ export interface Options {
136136
context: { cjsDts: boolean },
137137
) => Awaitable<InputOptions | void | null>)
138138

139-
/// output options
139+
//#region Output Options
140+
140141
/** @default ['es'] */
141142
format?: Format | Format[]
142143
globalName?: string
@@ -153,6 +154,9 @@ export interface Options {
153154
clean?: boolean | string[]
154155
/** @default false */
155156
minify?: boolean | 'dce-only' | MinifyOptions
157+
footer?: ChunkAddon
158+
banner?: ChunkAddon
159+
156160
/**
157161
* Specifies the compilation target environment(s).
158162
*
@@ -214,6 +218,11 @@ export interface Options {
214218
*/
215219
outExtensions?: OutExtensionFactory
216220

221+
/**
222+
* @default true
223+
*/
224+
cjsDefault?: boolean
225+
217226
outputOptions?:
218227
| OutputOptions
219228
| ((
@@ -284,7 +293,7 @@ export interface Options {
284293
*/
285294
fromVite?: boolean | 'vitest'
286295

287-
/// addons
296+
//#region Addons
288297
/**
289298
* Emit TypeScript declaration files (.d.ts).
290299
*
@@ -416,8 +425,6 @@ export interface Options {
416425
* Filter workspace packages. This option is only available in workspace mode.
417426
*/
418427
filter?: RegExp | string | string[]
419-
footer?: ChunkAddon
420-
banner?: ChunkAddon
421428
}
422429

423430
/**

0 commit comments

Comments
 (0)