Skip to content

Commit 1b62755

Browse files
committed
fix: adapt [email protected]+ setup script
1 parent c8884ba commit 1b62755

File tree

7 files changed

+147
-80
lines changed

7 files changed

+147
-80
lines changed

example/components/BothScripts.vue

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,31 @@
55
</template>
66

77
<script lang="ts" setup>
8-
import { defineProps } from 'vue'
8+
import { defineProps, ref } from 'vue'
99
1010
const props = defineProps({
1111
tag: {
1212
type: String,
1313
default: 'div'
14-
}
14+
},
15+
count: {
16+
type: Number,
17+
default: 0
18+
}
1519
})
20+
21+
const currentCount = ref(props.count)
22+
23+
const inc = () => {
24+
currentCount.value++
25+
}
1626
</script>
1727

1828
<script lang="ts">
19-
import { defineComponent } from 'vue'
29+
// do not use defineComponent
2030
21-
export default defineComponent({
31+
export default {
2232
name: 'BothScripts',
2333
customOptions: {}
24-
})
34+
}
2535
</script>

example/components/Setup.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</template>
77

88
<script setup lang="ts">
9-
import { defineProps, defineEmits, ref } from 'vue'
9+
import { defineProps, ref } from 'vue'
1010
import HelloWorld from './HelloWorld.vue'
1111
1212
const props = defineProps({
@@ -24,6 +24,4 @@ const inc = () => {
2424
currentCount.value++
2525
emit('on-add', currentCount.value)
2626
}
27-
28-
defineExpose({})
2927
</script>

example/vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export default defineConfig({
3030
outputDir: 'types',
3131
exclude: ['src/ignore'],
3232
staticImport: true,
33-
insertTypesEntry: true
33+
insertTypesEntry: true,
34+
logDiagnostics: true
3435
}),
3536
vue()
3637
]

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@typescript-eslint/eslint-plugin": "^4.29.2",
1919
"@typescript-eslint/parser": "^4.29.2",
2020
"@vitejs/plugin-vue": "^1.4.0",
21-
"@vue/compiler-sfc": "^3.2.4",
21+
"@vue/compiler-sfc": "^3.2.6",
2222
"chalk": "^4.1.2",
2323
"commitizen": "^4.2.4",
2424
"conventional-changelog-cli": "^2.1.1",
@@ -40,7 +40,7 @@
4040
"tsup": "^4.14.0",
4141
"typescript": "4.3.5",
4242
"vite": "^2.4.3",
43-
"vue": "^3.2.4"
43+
"vue": "^3.2.6"
4444
},
4545
"engines": {
4646
"node": ">=12.0.0"

src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
normalizeGlob,
1010
transformDynamicImport,
1111
transformAliasImport,
12-
removePureImport
12+
removePureImport,
13+
transferSetupPosition
1314
} from './transform'
1415
import { isNativeObj, mergeObjects, ensureAbsolute, ensureArray, runParallel } from './utils'
1516

@@ -211,10 +212,11 @@ export default (options: PluginOptions = {}): Plugin => {
211212
}
212213

213214
// 不断言会影响 setup 的类型推断
214-
content = content.replace(
215-
'...__default__',
216-
'...(__default__ as Record<string, unknown>)'
217-
)
215+
// content = content.replace(
216+
// '...__default__',
217+
// '...(__default__ as Record<string, unknown>)'
218+
// )
219+
content = transferSetupPosition(content)
218220
content += '\nexport default _sfc_main\n'
219221

220222
if (scriptSetup.lang === 'ts') {

src/transform.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,19 @@ const pureImportRE = /import\s?['"][^;\n]+?['"];?\n?/g
113113
export function removePureImport(content: string) {
114114
return content.replace(pureImportRE, '')
115115
}
116+
117+
const setupFunctionRE = /function setup\([\s\S]+\)\s+?\{[\s\S]+return __returned__\n\}/
118+
119+
export function transferSetupPosition(content: string) {
120+
const match = content.match(setupFunctionRE)
121+
122+
if (match) {
123+
const setupFunction = match[0]
124+
125+
return content
126+
.replace(setupFunction, '')
127+
.replace('setup})', setupFunction.slice('function '.length) + '\n\r})')
128+
}
129+
130+
return content
131+
}

0 commit comments

Comments
 (0)