diff --git a/src/core/options.ts b/src/core/options.ts index f1657df..5372467 100644 --- a/src/core/options.ts +++ b/src/core/options.ts @@ -1,3 +1,4 @@ +import type { IsolatedDeclarationsOptions } from 'oxc-transform' import type { TranspileOptions } from 'typescript' import type { FilterPattern } from 'unplugin-utils' @@ -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 } | { /** @@ -49,7 +56,7 @@ export type Options = { type Overwrite = Pick> & U export type OptionsResolved = Overwrite< - Required, + Required & { transformOptions?: any }, Pick > @@ -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, } } diff --git a/src/core/transformer.ts b/src/core/transformer.ts index a56aff0..dced7df 100644 --- a/src/core/transformer.ts +++ b/src/core/transformer.ts @@ -1,4 +1,5 @@ import path from 'node:path' +import type { IsolatedDeclarationsOptions } from 'oxc-transform' import type { CompilerOptions, TranspileOptions } from 'typescript' export interface TransformResult { @@ -17,7 +18,7 @@ function tryImport(pkg: string): Promise { export async function oxcTransform( id: string, code: string, - sourceMap?: boolean, + transformOptions?: IsolatedDeclarationsOptions, ): Promise { const oxc = await tryImport('oxc-transform') if (!oxc) { @@ -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, diff --git a/src/index.ts b/src/index.ts index 655b2e4..2c4c356 100644 --- a/src/index.ts +++ b/src/index.ts @@ -114,7 +114,10 @@ export const IsolatedDecl: UnpluginInstance = 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) @@ -123,7 +126,7 @@ export const IsolatedDecl: UnpluginInstance = result = await tsTransform( id, code, - (options as any).transformOptions, + options.transformOptions, options.sourceMap, ) break diff --git a/tests/__snapshots__/rollup/oxc-options.md b/tests/__snapshots__/rollup/oxc-options.md new file mode 100644 index 0000000..9fab2a5 --- /dev/null +++ b/tests/__snapshots__/rollup/oxc-options.md @@ -0,0 +1,20 @@ +## main.d.ts + +```ts +export declare class Test { + show(): void; +} + +``` +## main.js + +```js +class Test { + show() {} + /** @internal */ + hide() {} +} + +export { Test }; + +``` \ No newline at end of file diff --git a/tests/fixtures/oxc-options/main.ts b/tests/fixtures/oxc-options/main.ts new file mode 100644 index 0000000..7f33a9e --- /dev/null +++ b/tests/fixtures/oxc-options/main.ts @@ -0,0 +1,6 @@ +export class Test { + show(): void { } + + /** @internal */ + hide() { } +} diff --git a/tests/rollup.test.ts b/tests/rollup.test.ts index 9dbe40c..5350638 100644 --- a/tests/rollup.test.ts +++ b/tests/rollup.test.ts @@ -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) + }) })