Skip to content

Commit 42cde5f

Browse files
committed
perf: use native filter
1 parent 9413716 commit 42cde5f

File tree

5 files changed

+52
-38
lines changed

5 files changed

+52
-38
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@
7777
"oxc-minify": ">=0.96.0",
7878
"oxc-resolver": "^11.13.1",
7979
"oxc-transform": ">=0.96.0",
80-
"unplugin": "^2.3.10",
81-
"unplugin-utils": "^0.3.1"
80+
"unplugin": "^2.3.10"
8281
},
8382
"devDependencies": {
83+
"@antfu/utils": "^9.3.0",
8484
"@sxzz/eslint-config": "^7.2.8",
8585
"@sxzz/prettier-config": "^2.2.4",
8686
"@sxzz/test-utils": "^0.5.12",
@@ -93,7 +93,7 @@
9393
"tsdown": "^0.16.1",
9494
"tsx": "^4.20.6",
9595
"typescript": "^5.9.3",
96-
"unloader": "^0.5.0",
96+
"unloader": "^0.7.0",
9797
"vite": "^7.2.2",
9898
"vitest": "^4.0.8"
9999
},

pnpm-lock.yaml

Lines changed: 15 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/options.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { MinifyOptions } from 'oxc-minify'
22
import type { NapiResolveOptions } from 'oxc-resolver'
33
import type { TransformOptions } from 'oxc-transform'
4-
import type { UnpluginContextMeta } from 'unplugin'
5-
import type { FilterPattern } from 'unplugin-utils'
4+
import type { FilterPattern, UnpluginContextMeta } from 'unplugin'
65

76
export interface Options {
87
/**

src/index.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { readFileSync } from 'node:fs'
22
import path from 'node:path'
33
import process from 'node:process'
4+
import { toArray } from '@antfu/utils'
45
import { ResolverFactory } from 'oxc-resolver'
56
import { transform as oxcTransform } from 'oxc-transform'
67
import { createUnplugin, type UnpluginInstance } from 'unplugin'
7-
import { createFilter } from 'unplugin-utils'
88
import { resolveOptions, type Options } from './core/options'
99
import { getModuleFormat } from './core/utils'
1010
import type { RenderedChunk } from 'rollup'
@@ -13,7 +13,6 @@ import type { Plugin as UnloaderPlugin } from 'unloader'
1313
export 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: [/\.json$/, ...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 },

tsdown.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { defineConfig } from 'tsdown'
33
export default defineConfig({
44
entry: './src/*.ts',
55
exports: true,
6-
inlineOnly: [],
6+
inlineOnly: ['@antfu/utils'],
77
})

0 commit comments

Comments
 (0)