Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.

fix(core): fix analyzer duplicate injection code #268

Merged
merged 2 commits into from
Dec 13, 2023
Merged
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
10 changes: 10 additions & 0 deletions packages/core/src/compiler/trace-rerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export function analyzeByTraceRerender(code: MS, locations: InsertLocation[]) {
});\n`,
}

// to avoid duplicate injection
const currentCode = code.toString()
const shouldInject = Object.entries(apiNames).map(([, alias]) => {
return !currentCode.includes(alias)
}).every(Boolean)

if (!shouldInject)
return code

locations.forEach(({ start, end }, idx) => {
if (idx === 0) {
code = ensureImport(code, {
Expand All @@ -30,6 +39,7 @@ export function analyzeByTraceRerender(code: MS, locations: InsertLocation[]) {
})),
}, start)
}

entries(injectedCodes).forEach(([, appendCode]) => {
code.prependLeft(end, appendCode)
})
Expand Down
1 change: 1 addition & 0 deletions packages/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.4.0",
"@vitejs/plugin-vue-jsx": "^3.1.0",
"serve": "^14.2.1",
"typescript": "^5.2.2",
"vite": "^4.4.11",
Expand Down
2 changes: 2 additions & 0 deletions packages/playground/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { computed, reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import ReadCounter from './components/ReadCounter.vue'
import TSX from './components/TSX.vue'

const count = ref(0)
const doubleCount = computed(() => {
Expand Down Expand Up @@ -43,6 +44,7 @@ const router = useRouter()
{{ count }}
{{ doubleCount }}
<ReadCounter />
TSX: <TSX />
<RouterView />
<!-- <HelloWorld msg="Vite + Vue" /> -->
<button @click="router.push('/about')">
Expand Down
9 changes: 9 additions & 0 deletions packages/playground/src/components/TSX.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script lang="tsx">
import { defineComponent } from 'vue'

export default defineComponent({
setup() {
return () => <div>TSX Component</div>
},
})
</script>
2 changes: 2 additions & 0 deletions packages/playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { resolve } from 'node:path'
import type { Plugin } from 'vite'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import jsx from '@vitejs/plugin-vue-jsx'
import VueDevTools from 'vite-plugin-vue-devtools'

// https://vitejs.dev/config/
Expand All @@ -17,5 +18,6 @@ export default defineConfig({
plugins: [
VueDevTools() as Plugin[],
vue(),
jsx(),
],
})
Loading