1+ import { mkdtemp , writeFile } from 'node:fs/promises'
2+ import { tmpdir } from 'node:os'
3+ import { join } from 'node:path'
4+ import util , { type InspectOptionsStylized } from 'node:util'
15import Debug from 'debug'
6+ import {
7+ VERSION as rolldownVersion ,
8+ type BuildOptions ,
9+ type InputOptions ,
10+ type OutputOptions ,
11+ type RolldownPluginOption ,
12+ } from 'rolldown'
213import { importGlobPlugin } from 'rolldown/experimental'
14+ import { version } from '../../package.json'
315import {
416 mergeUserOptions ,
517 type DtsOptions ,
@@ -15,12 +27,6 @@ import { resolveChunkAddon, resolveChunkFilename } from './output'
1527import { ReportPlugin } from './report'
1628import { ShebangPlugin } from './shebang'
1729import { getShimsInject } from './shims'
18- import type {
19- BuildOptions ,
20- InputOptions ,
21- OutputOptions ,
22- RolldownPluginOption ,
23- } from 'rolldown'
2430
2531const debug = Debug ( 'tsdown:rolldown' )
2632
@@ -234,3 +240,69 @@ export async function resolveOutputOptions(
234240 )
235241 return outputOptions
236242}
243+
244+ export async function getDebugRolldownDir ( ) : Promise < string | undefined > {
245+ if ( ! debug . enabled ) return
246+ return await mkdtemp ( join ( tmpdir ( ) , 'tsdown-config-' ) )
247+ }
248+
249+ export async function debugBuildOptions (
250+ dir : string ,
251+ name : string | undefined ,
252+ format : NormalizedFormat ,
253+ buildOptions : BuildOptions ,
254+ ) : Promise < void > {
255+ const outFile = join ( dir , `tsdown.config.${ format } .js` )
256+
257+ handlePluginInspect ( buildOptions . plugins )
258+ const serialized = util . formatWithOptions (
259+ {
260+ depth : null ,
261+ maxArrayLength : null ,
262+ maxStringLength : null ,
263+ } ,
264+ buildOptions ,
265+ )
266+ const code = `/*
267+ Auto-generated rolldown config for tsdown debug purposes
268+ tsdown v${ version } , rolldown v${ rolldownVersion }
269+ Generated on ${ new Date ( ) . toISOString ( ) }
270+ Package name: ${ name || 'not specified' }
271+ */
272+
273+ export default ${ serialized } \n`
274+ await writeFile ( outFile , code )
275+ debug (
276+ 'Wrote debug rolldown config for "%s" (%s) -> %s' ,
277+ name || 'default name' ,
278+ format ,
279+ outFile ,
280+ )
281+ }
282+
283+ function handlePluginInspect ( plugins : RolldownPluginOption ) {
284+ if ( Array . isArray ( plugins ) ) {
285+ for ( const plugin of plugins ) {
286+ handlePluginInspect ( plugin )
287+ }
288+ } else if (
289+ typeof plugins === 'object' &&
290+ plugins !== null &&
291+ 'name' in plugins
292+ ) {
293+ ; ( plugins as any ) [ util . inspect . custom ] = function (
294+ depth : number ,
295+ options : InspectOptionsStylized ,
296+ inspect : typeof util . inspect ,
297+ ) {
298+ if ( '_options' in plugins ) {
299+ return inspect (
300+ { name : plugins . name , options : ( plugins as any ) . _options } ,
301+ options ,
302+ )
303+ } else {
304+ return `"rolldown plugin: ${ plugins . name } "`
305+ }
306+ }
307+ }
308+ }
0 commit comments