Skip to content

Commit 24c5d97

Browse files
authored
fix: skip hydration components transformation (#164)
1 parent a109759 commit 24c5d97

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/dirs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { dirname } from 'node:path'
2+
import { fileURLToPath } from 'node:url'
3+
4+
export const distDir = dirname(fileURLToPath(import.meta.url))

src/plugins/hydration.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { genImport } from 'knitwork'
22
import MagicString from 'magic-string'
3+
import { resolve } from 'node:path'
34
import { parseSync, type ImportDeclaration } from 'oxc-parser'
45
import { createUnplugin } from 'unplugin'
6+
import { distDir } from '../dirs'
57

68
const INCLUDE_VUE_RE = /\.vue$/
79
const EXCLUDE_NODE_MODULES = /node_modules/
810
const DEFINE_COMPONENT_RE = /defineComponent/
911
const DEFINE_NUXT_COMPONENT_RE = /defineNuxtComponent/
12+
const skipPath = normalizePath(resolve(distDir, 'runtime/hydration/component.ts'))
1013
export const InjectHydrationPlugin = createUnplugin(() => {
1114
return [
1215
{
@@ -16,11 +19,12 @@ export const InjectHydrationPlugin = createUnplugin(() => {
1619
filter: {
1720
id: {
1821
include: /.(vue|ts|js|tsx|jsx)$/,
19-
exclude: EXCLUDE_NODE_MODULES,
22+
exclude: [skipPath, EXCLUDE_NODE_MODULES],
2023
},
2124
code: /defineNuxtComponent|defineComponent/,
2225
},
23-
handler(code, id) {
26+
async handler(code, id) {
27+
console.log(id)
2428
const m = new MagicString(code)
2529
const { program } = parseSync(id, code)
2630
const imports = program.body.filter(node => node.type === 'ImportDeclaration')
@@ -77,7 +81,7 @@ export const InjectHydrationPlugin = createUnplugin(() => {
7781
filter: {
7882
id: {
7983
include: INCLUDE_VUE_RE,
80-
exclude: EXCLUDE_NODE_MODULES,
84+
exclude: [skipPath, EXCLUDE_NODE_MODULES],
8185
},
8286
code: /(?!defineComponent|defineNuxtComponent)/,
8387
},
@@ -123,3 +127,7 @@ function findImportSpecifier(importDecl: ImportDeclaration[], importedName: stri
123127
return specifier.type === 'ImportSpecifier' && specifier.imported.type === 'Identifier' && specifier.imported.name === importedName
124128
})
125129
}
130+
131+
function normalizePath(path: string) {
132+
return path.replace(/\\/g, '/')
133+
}

src/runtime/hydration/component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { defineComponent as _defineComponent, type DefineComponent } from 'vue'
3-
import { defineNuxtComponent as _defineNuxtComponent } from 'nuxt/app'
2+
import { defineNuxtComponent as _defineNuxtComponent, defineComponent as _defineComponent } from '#imports'
43
import { useHydrationCheck } from './composables'
4+
import type { DefineComponent } from 'vue'
55

66
export const defineNuxtComponent: typeof _defineComponent
77
= function defineNuxtComponent(...args: any[]): any {

0 commit comments

Comments
 (0)