11import { genImport } from 'knitwork'
22import MagicString from 'magic-string'
3+ import { resolve } from 'node:path'
34import { parseSync , type ImportDeclaration } from 'oxc-parser'
45import { createUnplugin } from 'unplugin'
6+ import { distDir } from '../dirs'
57
68const INCLUDE_VUE_RE = / \. v u e $ /
79const EXCLUDE_NODE_MODULES = / n o d e _ m o d u l e s /
810const DEFINE_COMPONENT_RE = / d e f i n e C o m p o n e n t /
911const DEFINE_NUXT_COMPONENT_RE = / d e f i n e N u x t C o m p o n e n t /
12+ const skipPath = normalizePath ( resolve ( distDir , 'runtime/hydration/component.ts' ) )
1013export const InjectHydrationPlugin = createUnplugin ( ( ) => {
1114 return [
1215 {
@@ -16,11 +19,12 @@ export const InjectHydrationPlugin = createUnplugin(() => {
1619 filter : {
1720 id : {
1821 include : / .( v u e | t s | j s | t s x | j s x ) $ / ,
19- exclude : EXCLUDE_NODE_MODULES ,
22+ exclude : [ skipPath , EXCLUDE_NODE_MODULES ] ,
2023 } ,
2124 code : / d e f i n e N u x t C o m p o n e n t | d e f i n e C o m p o n e n t / ,
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 : / (? ! d e f i n e C o m p o n e n t | d e f i n e N u x t C o m p o n e n t ) / ,
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+ }
0 commit comments