11import { readFileSync } from 'node:fs'
22import path from 'node:path'
33import process from 'node:process'
4+ import { toArray } from '@antfu/utils'
45import { ResolverFactory } from 'oxc-resolver'
56import { transform as oxcTransform } from 'oxc-transform'
67import { createUnplugin , type UnpluginInstance } from 'unplugin'
7- import { createFilter } from 'unplugin-utils'
88import { resolveOptions , type Options } from './core/options'
99import { getModuleFormat } from './core/utils'
1010import type { RenderedChunk } from 'rollup'
@@ -13,7 +13,6 @@ import type { Plugin as UnloaderPlugin } from 'unloader'
1313export const Oxc : UnpluginInstance < Options | undefined , false > = createUnplugin (
1414 ( rawOptions = { } , { framework } ) => {
1515 const options = resolveOptions ( rawOptions , framework )
16- const filter = createFilter ( options . include , options . exclude )
1716
1817 const resolveId =
1918 options . resolve === false
@@ -103,25 +102,32 @@ export const Oxc: UnpluginInstance<Options | undefined, false> = createUnplugin(
103102 options ( config ) {
104103 config . sourcemap ||= options . sourcemap
105104 } ,
106- load ( id ) {
107- if ( id . endsWith ( '.json' ) ) {
108- let code = readFileSync ( id , 'utf8' )
105+ load : {
106+ filter : {
107+ id : {
108+ include : [ / \. j s o n $ / , ...toArray ( options . include ) ] ,
109+ exclude : options . exclude ,
110+ } ,
111+ } ,
112+ handler ( id ) {
113+ if ( id . endsWith ( '.json' ) ) {
114+ let code = readFileSync ( id , 'utf8' )
109115
110- const json = JSON . parse ( code )
111- code = `const json = ${ code } \nexport default json\n`
112- const i = 0
113- for ( const key of Object . keys ( json ) ) {
114- const sanitizedKey = `_${ key . replaceAll ( / \W / g, '_' ) } ${ i } `
115- code +=
116- `\nconst ${ sanitizedKey } = json[${ JSON . stringify ( key ) } ]\n` +
117- `export { ${ sanitizedKey } as ${ JSON . stringify ( key ) } }\n`
116+ const json = JSON . parse ( code )
117+ code = `const json = ${ code } \nexport default json\n`
118+ const i = 0
119+ for ( const key of Object . keys ( json ) ) {
120+ const sanitizedKey = `_${ key . replaceAll ( / \W / g, '_' ) } ${ i } `
121+ code +=
122+ `\nconst ${ sanitizedKey } = json[${ JSON . stringify ( key ) } ]\n` +
123+ `export { ${ sanitizedKey } as ${ JSON . stringify ( key ) } }\n`
124+ }
125+ return { code, format : 'module' }
118126 }
119- return { code, format : 'module' }
120- }
121127
122- if ( ! filter ( id ) ) return
123- const contents = readFileSync ( id , 'utf8' )
124- return contents
128+ const contents = readFileSync ( id , 'utf8' )
129+ return contents
130+ } ,
125131 } ,
126132 }
127133
@@ -130,11 +136,14 @@ export const Oxc: UnpluginInstance<Options | undefined, false> = createUnplugin(
130136 enforce : options . enforce ,
131137
132138 resolveId,
133-
134- transformInclude ( id ) {
135- return filter ( id )
136- } ,
137- transform,
139+ transform : transform
140+ ? {
141+ filter : {
142+ id : { include : options . include , exclude : options . exclude } ,
143+ } ,
144+ handler : transform ,
145+ }
146+ : undefined ,
138147
139148 rollup : { renderChunk } ,
140149 rolldown : { renderChunk } ,
0 commit comments