Skip to content
Merged
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
16 changes: 12 additions & 4 deletions src/core/options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IsolatedDeclarationsOptions } from 'oxc-transform'
import type { TranspileOptions } from 'typescript'
import type { FilterPattern } from 'unplugin-utils'

Expand Down Expand Up @@ -30,10 +31,16 @@ export type Options = {
} & (
| {
/**
* `oxc-transform` or `@swc/core` should be installed yourself
* if you want to use `oxc` or `swc` transformer.
* `@swc/core` should be installed yourself.
*/
transformer?: 'oxc' | 'swc'
transformer?: 'swc'
}
| {
/**
* `oxc-transform` should be installed yourself.
*/
transformer?: 'oxc'
transformOptions?: Omit<IsolatedDeclarationsOptions, 'sourceMap'>
}
| {
/**
Expand All @@ -49,7 +56,7 @@ export type Options = {
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U

export type OptionsResolved = Overwrite<
Required<Options>,
Required<Options> & { transformOptions?: any },
Pick<Options, 'enforce' | 'extraOutdir' | 'rewriteImports' | 'inputBase'>
>

Expand All @@ -65,5 +72,6 @@ export function resolveOptions(options: Options): OptionsResolved {
patchCjsDefaultExport: options.patchCjsDefaultExport || false,
rewriteImports: options.rewriteImports,
inputBase: options.inputBase,
transformOptions: (options as any).transformOptions,
}
}
5 changes: 3 additions & 2 deletions src/core/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path'
import type { IsolatedDeclarationsOptions } from 'oxc-transform'
import type { CompilerOptions, TranspileOptions } from 'typescript'

export interface TransformResult {
Expand All @@ -17,7 +18,7 @@ function tryImport<T>(pkg: string): Promise<T | null> {
export async function oxcTransform(
id: string,
code: string,
sourceMap?: boolean,
transformOptions?: IsolatedDeclarationsOptions,
): Promise<TransformResult> {
const oxc = await tryImport<typeof import('oxc-transform')>('oxc-transform')
if (!oxc) {
Expand All @@ -28,7 +29,7 @@ export async function oxcTransform(
],
}
}
const result = oxc.isolatedDeclaration(id, code, { sourcemap: sourceMap })
const result = oxc.isolatedDeclaration(id, code, transformOptions)
return {
...result,
map: result.map?.mappings,
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
let result: TransformResult
switch (options.transformer) {
case 'oxc':
result = await oxcTransform(id, code, options.sourceMap)
result = await oxcTransform(id, code, {
...options.transformOptions,
sourcemap: options.sourceMap,
})
break
case 'swc':
result = await swcTransform(id, code)
Expand All @@ -123,7 +126,7 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
result = await tsTransform(
id,
code,
(options as any).transformOptions,
options.transformOptions,
options.sourceMap,
)
break
Expand Down
20 changes: 20 additions & 0 deletions tests/__snapshots__/rollup/oxc-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## main.d.ts

```ts
export declare class Test {
show(): void;
}

```
## main.js

```js
class Test {
show() {}
/** @internal */
hide() {}
}

export { Test };

```
6 changes: 6 additions & 0 deletions tests/fixtures/oxc-options/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class Test {
show(): void { }

/** @internal */
hide() { }
}
22 changes: 22 additions & 0 deletions tests/rollup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,26 @@ describe.concurrent('rollup', () => {

await expectSnapshot(dist, `rollup/${dir}`, expect)
})

test('oxc options', async ({ expect }) => {
const dir = 'oxc-options'
const input = path.resolve(fixtures, dir, 'main.ts')
const dist = path.resolve(TEST_SANDBOX_FOLDER, dir)

const bundle = await rollup({
input,
plugins: [
UnpluginIsolatedDecl({
transformer: 'oxc',
transformOptions: { stripInternal: true },
}),
Oxc(),
],
logLevel: 'silent',
})

await bundle.write({ dir: dist })

await expectSnapshot(dist, `rollup/${dir}`, expect)
})
})