Skip to content

Commit 4a05c9a

Browse files
committed
perf: perfer native filter
1 parent a65ea47 commit 4a05c9a

File tree

4 files changed

+38
-37
lines changed

4 files changed

+38
-37
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@
6666
"ast-kit": "^2.0.0",
6767
"magic-string-ast": "^0.9.1",
6868
"muggle-string": "^0.4.1",
69-
"unplugin": "^2.3.2",
70-
"unplugin-utils": "^0.2.4"
69+
"unplugin": "^2.3.2"
7170
},
7271
"devDependencies": {
7372
"@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/core/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FilterPattern } from 'unplugin-utils'
1+
import type { FilterPattern } from 'unplugin'
22

33
type MarkRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>
44

src/index.ts

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { babelParse, getLang } from 'ast-kit'
22
import { generateTransform, MagicString } from 'magic-string-ast'
33
import { createUnplugin, type UnpluginInstance } from 'unplugin'
4-
import { createFilter } from 'unplugin-utils'
54
import { resolveOption, type Options } from './core/options'
65
import { resolveName } from './core/utils'
76
import type * as t from '@babel/types'
@@ -16,47 +15,53 @@ function getNodeStart(node: t.Node) {
1615
export const VueNamedExport: UnpluginInstance<Options | undefined, false> =
1716
createUnplugin((rawOptions = {}) => {
1817
const options = resolveOption(rawOptions)
19-
const filter = createFilter(options.include, options.exclude)
2018

2119
const name = 'unplugin-vue-named-export'
2220
return {
2321
name,
2422
enforce: 'post',
2523

26-
transformInclude(id) {
27-
return filter(id)
28-
},
29-
30-
async transform(code, id) {
31-
const lang = getLang(id)
32-
33-
const program = babelParse(code, lang)
34-
const defaultExport = program.body.find(
35-
(node): node is t.ExportDefaultDeclaration =>
36-
node.type === 'ExportDefaultDeclaration',
37-
)
38-
if (!defaultExport) return
24+
transform: {
25+
filter: {
26+
id: {
27+
include: options.include,
28+
exclude: options.exclude,
29+
},
30+
},
31+
async handler(code, id) {
32+
const lang = getLang(id)
3933

40-
const s = new MagicString(code)
41-
const resolvedName = await (options.resolveName || resolveName)(id)
34+
const program = babelParse(code, lang)
35+
const defaultExport = program.body.find(
36+
(node): node is t.ExportDefaultDeclaration =>
37+
node.type === 'ExportDefaultDeclaration',
38+
)
39+
if (!defaultExport) return
4240

43-
s.overwrite(
44-
defaultExport.start!,
45-
getNodeStart(defaultExport.declaration),
46-
`export const ${resolvedName} = `,
47-
)
41+
const s = new MagicString(code)
42+
const resolvedName = await (options.resolveName || resolveName)(id)
4843

49-
if (!options.removeDefault) {
50-
s.appendLeft(defaultExport.end!, `\nexport default ${resolvedName};`)
51-
} else {
52-
// hack Vite HMR
53-
s.replace(
54-
/const \{ default: updated, (.*) \} = mod/,
55-
(_, $1) => `const { "${resolvedName}": updated, ${$1} } = mod`,
44+
s.overwrite(
45+
defaultExport.start!,
46+
getNodeStart(defaultExport.declaration),
47+
`export const ${resolvedName} = `,
5648
)
57-
}
5849

59-
return generateTransform(s, id)
50+
if (!options.removeDefault) {
51+
s.appendLeft(
52+
defaultExport.end!,
53+
`\nexport default ${resolvedName};`,
54+
)
55+
} else {
56+
// hack Vite HMR
57+
s.replace(
58+
/const \{ default: updated, (.*) \} = mod/,
59+
(_, $1) => `const { "${resolvedName}": updated, ${$1} } = mod`,
60+
)
61+
}
62+
63+
return generateTransform(s, id)
64+
},
6065
},
6166

6267
vite: {

0 commit comments

Comments
 (0)