Skip to content

Commit f1de4ed

Browse files
committed
perf: prefer native filter
1 parent e323157 commit f1de4ed

File tree

4 files changed

+47
-54
lines changed

4 files changed

+47
-54
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@
6565
"magic-string": "^0.30.17",
6666
"picomatch": "^4.0.2",
6767
"unplugin": "^2.3.2",
68-
"unplugin-replace": "^0.6.0",
69-
"unplugin-utils": "^0.2.4"
68+
"unplugin-replace": "^0.6.0"
7069
},
7170
"devDependencies": {
7271
"@babel/types": "^7.27.1",

pnpm-lock.yaml

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

src/index.ts

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import MagicString from 'magic-string'
77
import { createUnplugin, type UnpluginInstance } from 'unplugin'
88
import ReplacePlugin from 'unplugin-replace'
9-
import { createFilter } from 'unplugin-utils'
109
import { scanEnums } from './core/enum'
1110
import { resolveOptions, type Options } from './core/options'
1211

@@ -18,8 +17,6 @@ const plugin: UnpluginInstance<Options | undefined, true> = createUnplugin<
1817
true
1918
>((rawOptions = {}, meta) => {
2019
const options = resolveOptions(rawOptions)
21-
const filter = createFilter(options.include, options.exclude)
22-
2320
const { declarations, defines } = scanEnums(options)
2421

2522
const replacePlugin = ReplacePlugin.raw(
@@ -38,52 +35,56 @@ const plugin: UnpluginInstance<Options | undefined, true> = createUnplugin<
3835
name,
3936
enforce: options.enforce,
4037

41-
transformInclude(id) {
42-
return filter(id)
43-
},
44-
45-
transform(code, id) {
46-
let s: MagicString | undefined
38+
transform: {
39+
filter: {
40+
id: {
41+
include: options.include,
42+
exclude: options.exclude,
43+
},
44+
},
45+
handler(code, id) {
46+
let s: MagicString | undefined
4747

48-
if (id in declarations) {
49-
s ||= new MagicString(code)
50-
for (const declaration of declarations[id]) {
51-
const {
52-
range: [start, end],
53-
id,
54-
members,
55-
} = declaration
56-
s.update(
57-
start,
58-
end,
59-
`export const ${id} = {${members
60-
.flatMap(({ name, value }) => {
61-
const forwardMapping = `${JSON.stringify(name)}: ${JSON.stringify(value)}`
62-
const reverseMapping = `${JSON.stringify(value.toString())}: ${JSON.stringify(name)}`
48+
if (id in declarations) {
49+
s ||= new MagicString(code)
50+
for (const declaration of declarations[id]) {
51+
const {
52+
range: [start, end],
53+
id,
54+
members,
55+
} = declaration
56+
s.update(
57+
start,
58+
end,
59+
`export const ${id} = {${members
60+
.flatMap(({ name, value }) => {
61+
const forwardMapping = `${JSON.stringify(name)}: ${JSON.stringify(value)}`
62+
const reverseMapping = `${JSON.stringify(value.toString())}: ${JSON.stringify(name)}`
6363
64-
// see https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
65-
return typeof value === 'string'
66-
? [
67-
forwardMapping,
68-
// string enum members do not get a reverse mapping generated at all
69-
]
70-
: [
71-
forwardMapping,
72-
// other enum members should support enum reverse mapping
73-
reverseMapping,
74-
]
75-
})
76-
.join(',\n')}}`,
77-
)
64+
// see https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
65+
return typeof value === 'string'
66+
? [
67+
forwardMapping,
68+
// string enum members do not get a reverse mapping generated at all
69+
]
70+
: [
71+
forwardMapping,
72+
// other enum members should support enum reverse mapping
73+
reverseMapping,
74+
]
75+
})
76+
.join(',\n')}}`,
77+
)
78+
}
7879
}
79-
}
8080

81-
if (s) {
82-
return {
83-
code: s.toString(),
84-
map: s.generateMap(),
81+
if (s) {
82+
return {
83+
code: s.toString(),
84+
map: s.generateMap(),
85+
}
8586
}
86-
}
87+
},
8788
},
8889
},
8990
]

tsdown.config.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { defineConfig } from 'tsdown'
22

33
export default defineConfig({
4-
entry: ['./src/*.ts'],
5-
format: ['esm'],
6-
target: 'node18.12',
7-
clean: true,
8-
dts: true,
4+
entry: './src/*.ts',
95
})

0 commit comments

Comments
 (0)