Skip to content

Commit

Permalink
feat: print debug log
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Nov 21, 2024
1 parent b9a1595 commit 709f2e0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 35 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
},
"dependencies": {
"@rollup/pluginutils": "^5.1.3",
"debug": "^4.3.7",
"magic-string": "^0.30.12",
"oxc-parser": "^0.36.0",
"unplugin": "^1.16.0"
Expand All @@ -109,6 +110,7 @@
"@swc/core": "^1.9.2",
"@sxzz/eslint-config": "^4.4.1",
"@sxzz/prettier-config": "^2.0.2",
"@types/debug": "^4.1.12",
"@types/node": "^22.9.0",
"bumpp": "^9.8.1",
"esbuild": "^0.24.0",
Expand Down
40 changes: 6 additions & 34 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Foo = 'bar'
24 changes: 23 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { mkdir, readFile, writeFile } from 'node:fs/promises'
import path from 'node:path'
import { createFilter } from '@rollup/pluginutils'
import Debug from 'debug'
import MagicString from 'magic-string'
import { parseAsync } from 'oxc-parser'
import {
Expand Down Expand Up @@ -36,8 +37,12 @@ import type {
PluginContext,
} from 'rollup'

const debug = Debug('unplugin-isolated-decl')

export type { Options }

export type * from './core/types'

/**
* The main unplugin instance.
*/
Expand Down Expand Up @@ -117,6 +122,9 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
code = s.toString()
}

const label = debug.enabled && `[${options.transformer}]`
debug(label, 'transform', id)

let result: TransformResult
switch (options.transformer) {
case 'oxc':
Expand All @@ -133,6 +141,12 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
)
}
const { code: sourceText, errors } = result
debug(
label,
'transformed',
id,
errors.length ? 'with errors' : 'successfully',
)
if (errors.length) {
if (options.ignoreErrors) {
context.warn(errors[0])
Expand All @@ -143,7 +157,10 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
}
addOutput(id, sourceText)

if (!program) return
if (!program) {
debug('cannot parse', id)
return
}
const typeImports = program.body.filter(
(
node,
Expand Down Expand Up @@ -189,6 +206,7 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
} catch {
continue
}
debug('transform type import:', resolved)
await transform(context, source, resolved)
}
}
Expand Down Expand Up @@ -233,6 +251,7 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
if (options.patchCjsDefaultExport && fileName.endsWith('.d.cts')) {
source = patchCjsDefaultExport(source)
}
debug('[rollup] emit dts file:', fileName)
this.emitFile({
type: 'asset',
fileName,
Expand Down Expand Up @@ -293,6 +312,7 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
if (options.patchCjsDefaultExport && fileName.endsWith('.d.cts')) {
source = patchCjsDefaultExport(source)
}
debug('[farm] emit dts file:', fileName)
farmPluginContext.emitFile({
type: 'asset',
fileName,
Expand Down Expand Up @@ -352,7 +372,9 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
if (write) {
await mkdir(path.dirname(filePath), { recursive: true })
await writeFile(filePath, source)
debug('[esbuild] write dts file:', filePath)
} else {
debug('[esbuild] emit dts file:', filePath)
result.outputFiles!.push({
path: filePath,
contents: textEncoder.encode(source),
Expand Down

0 comments on commit 709f2e0

Please sign in to comment.