Skip to content

Commit 853541b

Browse files
authored
perf: use transform filter hook (#145)
1 parent e2443f8 commit 853541b

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/plugins/hydration.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,42 @@
11
import MagicString from 'magic-string'
22
import { createUnplugin } from 'unplugin'
33

4+
const ID_INCLUDE = /\.vue$/
5+
const ID_EXCLUDE = /node_modules/
46
export const InjectHydrationPlugin = createUnplugin(() => {
57
return {
68
name: '@nuxt/hints:inject-hydration-check',
79
enforce: 'pre',
810
transformInclude(id) {
911
return id.endsWith('.vue') && !id.includes('node_modules')
1012
},
11-
transform(code) {
12-
const m = new MagicString(code)
13-
const re = /<script\s+setup[^>]*>/g
14-
const match = re.exec(code)
15-
if (!match) {
16-
return code
17-
}
13+
transform: {
14+
filter: {
15+
id: {
16+
include: ID_INCLUDE,
17+
exclude: ID_EXCLUDE,
18+
},
19+
},
1820

19-
// Add useHydrationCheck after the <script setup> tag
20-
m.appendRight(
21-
match.index + match[0].length,
22-
`\nimport { useHydrationCheck } from '@nuxt/hints/runtime/hydration/composables'\nuseHydrationCheck();`,
23-
)
21+
handler(code) {
22+
const m = new MagicString(code)
23+
const re = /<script\s+setup[^>]*>/g
24+
const match = re.exec(code)
25+
if (!match) {
26+
return code
27+
}
2428

25-
return {
26-
code: m.toString(),
27-
map: m.generateMap({ hires: true }),
28-
}
29+
// Add useHydrationCheck after the <script setup> tag
30+
m.appendRight(
31+
match.index + match[0].length,
32+
`\nimport { useHydrationCheck } from '@nuxt/hints/runtime/hydration/composables'\nuseHydrationCheck();`,
33+
)
34+
35+
return {
36+
code: m.toString(),
37+
map: m.generateMap({ hires: true }),
38+
}
39+
},
2940
},
3041
}
3142
})

0 commit comments

Comments
 (0)