|
1 | 1 | import { genImport } from 'knitwork' |
2 | 2 | import MagicString from 'magic-string' |
3 | 3 | import { resolve } from 'node:path' |
4 | | -import { parseSync, type ImportDeclaration } from 'oxc-parser' |
| 4 | +import { parseSync, type ImportDeclaration, type ImportDeclarationSpecifier, type ImportSpecifier } from 'oxc-parser' |
5 | 5 | import { createUnplugin } from 'unplugin' |
6 | 6 | import { distDir } from '../dirs' |
7 | 7 |
|
@@ -31,23 +31,31 @@ export const InjectHydrationPlugin = createUnplugin(() => { |
31 | 31 | const hasDefineComponent = DEFINE_COMPONENT_RE.test(code) |
32 | 32 | const hasDefineNuxtComponent = DEFINE_NUXT_COMPONENT_RE.test(code) |
33 | 33 |
|
34 | | - const defineComponentImport = findImportSpecifier(imports as ImportDeclaration[], 'defineComponent', ['vue', '#imports']) |
| 34 | + const defineComponentImport = findImportSpecifier( |
| 35 | + imports, |
| 36 | + 'defineComponent', |
| 37 | + ['vue', '#imports'], |
| 38 | + (specifier, nextSpecifier) => { |
| 39 | + m.remove( |
| 40 | + specifier.start, |
| 41 | + nextSpecifier?.start ?? specifier.end, |
| 42 | + ) |
| 43 | + }, |
| 44 | + ) |
35 | 45 | const defineComponentAlias = defineComponentImport?.local.name || 'defineComponent' |
36 | | - if (defineComponentImport) { |
37 | | - m.remove( |
38 | | - defineComponentImport.start, |
39 | | - defineComponentImport.end, |
40 | | - ) |
41 | | - } |
42 | 46 |
|
43 | | - const defineNuxtComponentImport = findImportSpecifier(imports as ImportDeclaration[], 'defineNuxtComponent', ['#app/composables/component', '#imports', '#app', 'nuxt/app']) |
| 47 | + const defineNuxtComponentImport = findImportSpecifier( |
| 48 | + imports, |
| 49 | + 'defineNuxtComponent', |
| 50 | + ['#app/composables/component', '#imports', '#app', 'nuxt/app'], |
| 51 | + (specifier, next) => { |
| 52 | + m.remove( |
| 53 | + specifier.start, |
| 54 | + next?.start ?? specifier.end, |
| 55 | + ) |
| 56 | + }, |
| 57 | + ) |
44 | 58 | const defineNuxtComponentAlias = defineNuxtComponentImport?.local.name || 'defineNuxtComponent' |
45 | | - if (defineNuxtComponentImport) { |
46 | | - m.remove( |
47 | | - defineNuxtComponentImport.start, |
48 | | - defineNuxtComponentImport.end, |
49 | | - ) |
50 | | - } |
51 | 59 |
|
52 | 60 | const importsToAdd = new Set([ |
53 | 61 | hasDefineComponent |
@@ -121,11 +129,24 @@ export const InjectHydrationPlugin = createUnplugin(() => { |
121 | 129 | /** |
122 | 130 | * Finds an import specifier for a given imported name from specified package names. |
123 | 131 | */ |
124 | | -function findImportSpecifier(importDecl: ImportDeclaration[], importedName: string, pkgNames: string | string[]) { |
| 132 | +function findImportSpecifier( |
| 133 | + importDecl: ImportDeclaration[], |
| 134 | + importedName: string, |
| 135 | + pkgNames: string | string[], |
| 136 | + callback?: (specifier: ImportSpecifier, nextSpecifier?: ImportDeclarationSpecifier) => void, |
| 137 | +) { |
125 | 138 | const names = Array.isArray(pkgNames) ? pkgNames : [pkgNames] |
126 | | - return importDecl.find(imp => names.includes(imp.source.value))?.specifiers.find((specifier) => { |
127 | | - return specifier.type === 'ImportSpecifier' && specifier.imported.type === 'Identifier' && specifier.imported.name === importedName |
128 | | - }) |
| 139 | + const decl = importDecl.find(imp => names.includes(imp.source.value)) |
| 140 | + if (!decl) { |
| 141 | + return |
| 142 | + } |
| 143 | + for (let i = 0; i < decl.specifiers.length; i++) { |
| 144 | + const specifier = decl.specifiers[i]! |
| 145 | + if (specifier.type === 'ImportSpecifier' && specifier.imported.type === 'Identifier' && specifier.imported.name === importedName) { |
| 146 | + callback?.(specifier, decl.specifiers[i + 1]) |
| 147 | + return specifier |
| 148 | + } |
| 149 | + } |
129 | 150 | } |
130 | 151 |
|
131 | 152 | function normalizePath(path: string) { |
|
0 commit comments