Skip to content

fix(transform): fixed the issue of duplicate import statements in jsx syntax #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
} from 'vue/compiler-sfc'
import { type Transform, transform } from 'sucrase'
import hashId from 'hash-sum'
import { extractVueImport, mergeVueImports } from './utils'

export const COMP_IDENTIFIER = `__sfc__`

Expand Down Expand Up @@ -172,7 +173,20 @@ export async function compileFile(
if (Array.isArray(clientTemplateResult)) {
return clientTemplateResult
}
clientCode += `;${clientTemplateResult}`

if (isJSX) {
// Template compilation products may have the same Vue import as script compilation products
const { cleanedCode: clientCleanedCode, imports: clientImports } =
extractVueImport(clientCode)
const { cleanedCode: templateCleanedCode, imports: templateImports } =
extractVueImport(clientTemplateResult)
const mergeImports = mergeVueImports(clientImports, templateImports)

clientCode = `${mergeImports}
${clientCleanedCode};${templateCleanedCode}`
} else {
clientCode += `;${clientTemplateResult}`
}

const ssrTemplateResult = await doCompileTemplate(
store,
Expand All @@ -185,7 +199,19 @@ export async function compileFile(
)
if (typeof ssrTemplateResult === 'string') {
// ssr compile failure is fine
ssrCode += `;${ssrTemplateResult}`
if (isJSX) {
// Template compilation products may have the same Vue import as script compilation products
const { cleanedCode: ssrCleanedCode, imports: ssrImports } =
extractVueImport(ssrCode)
const { cleanedCode: templateCleanedCode, imports: templateImports } =
extractVueImport(ssrTemplateResult)
const mergeImports = mergeVueImports(ssrImports, templateImports)

ssrCode = `${mergeImports}
${ssrCleanedCode};${templateCleanedCode}`
} else {
ssrCode += `;${ssrTemplateResult}`
}
} else {
ssrCode = `/* SSR compile error: ${ssrTemplateResult[0]} */`
}
Expand Down
41 changes: 41 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,44 @@ export function atou(base64: string): string {
// https://base64.guru/developers/javascript/examples/unicode-strings
return decodeURIComponent(escape(binary))
}

export function extractVueImport(sourceCode: string) {
const importPattern = /^import\s*{[^}]*}\s*from\s*["']vue["'];?/gm

const importMatches = sourceCode.match(importPattern)

const imports = importMatches
? importMatches.map((match) => match.trim())[0]
: ''

const cleanedCode = sourceCode.replace(importPattern, '')

return {
cleanedCode,
imports,
}
}

export function mergeVueImports(imports1: string, imports2: string) {
const importPattern = /{([\s\S]*?)}/

const matches1 = imports1.match(importPattern)
const content1 = matches1 ? matches1[1].trim() : ''

const matches2 = imports2.match(importPattern)
const content2 = matches2 ? matches2[1].trim() : ''

const mergedContent = Array.from(
new Set(
content1
.split(',')
.map((item) => item.trim())
.concat(content2.split(',').map((item) => item.trim())),
),
).join(', ')

//Merge import statements of template and jsx
const mergedImport = `import { ${mergedContent} } from 'vue';`

return mergedImport
}