11import path from 'node:path'
2- import remapping from '@ampproject/remapping'
32import { minify } from 'oxc-minify'
43import { ResolverFactory } from 'oxc-resolver'
54import { transform } from 'oxc-transform'
65import { createUnplugin , type UnpluginInstance } from 'unplugin'
76import { createFilter } from 'unplugin-utils'
87import { resolveOptions , type Options } from './core/options'
8+ import type { RenderedChunk } from 'rollup'
99
1010export const Oxc : UnpluginInstance < Options | undefined , false > = createUnplugin (
1111 ( rawOptions = { } ) => {
1212 const options = resolveOptions ( rawOptions )
1313 const filter = createFilter ( options . include , options . exclude )
1414
15- const name = 'unplugin-oxc'
15+ const renderChunk : any =
16+ options . minify !== false
17+ ? ( code : string , chunk : RenderedChunk ) => {
18+ const result = minify ( chunk . fileName , code , {
19+ ...options . minify ,
20+ sourcemap : options . sourcemap ,
21+ } )
22+ return {
23+ code : result . code ,
24+ map : result . map ,
25+ }
26+ }
27+ : undefined
28+
1629 return {
17- name,
30+ name : 'unplugin-oxc' ,
1831 enforce : options . enforce ,
1932
2033 resolveId :
@@ -40,34 +53,19 @@ export const Oxc: UnpluginInstance<Options | undefined, false> = createUnplugin(
4053 } ,
4154
4255 transform :
43- options . transform !== false || options . minify !== false
56+ options . transform !== false
4457 ? ( code , id ) => {
45- let map
46- if ( options . transform !== false ) {
47- const result = transform ( id , code , {
48- ...options . transform ,
49- sourcemap : options . sourcemap ,
50- } )
51- code = result . code
52- map = result . map
53- }
54-
55- if ( options . minify !== false ) {
56- const result = minify ( id , code , {
57- ...options . minify ,
58- sourcemap : options . sourcemap ,
59- } )
60- code = result . code
61- if ( map && result . map ) {
62- map = remapping ( [ result . map , map ] , ( ) => null , { } )
63- } else {
64- map = result . map
65- }
66- }
67-
68- return { code, map }
58+ const result = transform ( id , code , {
59+ ...options . transform ,
60+ sourcemap : options . sourcemap ,
61+ } )
62+ return { code : result . code , map : result . map }
6963 }
7064 : undefined ,
65+
66+ rollup : { renderChunk } ,
67+ rolldown : { renderChunk } ,
68+ vite : { renderChunk } ,
7169 }
7270 } ,
7371)
0 commit comments