Skip to content

Commit

Permalink
feat(create-vite): update vue templates
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Aug 25, 2021
1 parent 9eb4052 commit fe9ebe7
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 81 deletions.
20 changes: 2 additions & 18 deletions packages/create-vite/template-vue-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,8 @@ This template should help get you started developing with Vue 3 and Typescript i

## Recommended IDE Setup

[VSCode](https://code.visualstudio.com/) + [Vetur](https://marketplace.visualstudio.com/items?itemName=octref.vetur). Make sure to enable `vetur.experimental.templateInterpolationService` in settings!

### If Using `<script setup>`

[`<script setup>`](https://github.com/vuejs/rfcs/pull/227) is a feature that is currently in RFC stage. To get proper IDE support for the syntax, use [Volar](https://marketplace.visualstudio.com/items?itemName=johnsoncodehk.volar) instead of Vetur (and disable Vetur).
- [VSCode](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=johnsoncodehk.volar)

## Type Support For `.vue` Imports in TS

Since TypeScript cannot handle type information for `.vue` imports, they are shimmed to be a generic Vue component type by default. In most cases this is fine if you don't really care about component prop types outside of templates. However, if you wish to get actual prop types in `.vue` imports (for example to get props validation when using manual `h(...)` calls), you can use the following:

### If Using Volar

Run `Volar: Switch TS Plugin on/off` from VSCode command palette.

### If Using Vetur

1. Install and add `@vuedx/typescript-plugin-vue` to the [plugins section](https://www.typescriptlang.org/tsconfig#plugins) in `tsconfig.json`
2. Delete `src/shims-vue.d.ts` as it is no longer needed to provide module info to Typescript
3. Open `src/main.ts` in VSCode
4. Open the VSCode command palette
5. Search and run "Select TypeScript version" -> "Use workspace version"
Since TypeScript cannot handle type information for `.vue` imports, they are shimmed to be a generic Vue component type by default. In most cases this is fine if you don't really care about component prop types outside of templates. However, if you wish to get actual prop types in `.vue` imports (for example to get props validation when using manual `h(...)` calls), you can enable Volar's `.vue` type support plugin by running `Volar: Switch TS Plugin on/off` from VSCode command palette.
4 changes: 2 additions & 2 deletions packages/create-vite/template-vue-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"vue": "^3.2.6"
},
"devDependencies": {
"@vitejs/plugin-vue": "^1.3.0",
"@vitejs/plugin-vue": "^1.6.0",
"@vue/compiler-sfc": "^3.0.5",
"typescript": "^4.3.2",
"vite": "^2.4.4",
"vite": "^2.5.1",
"vue-tsc": "^0.2.2"
}
}
18 changes: 6 additions & 12 deletions packages/create-vite/template-vue-ts/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
<script setup lang="ts">
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
import HelloWorld from './components/HelloWorld.vue'
</script>

<template>
<img alt="Vue logo" src="./assets/logo.png" />
<HelloWorld msg="Hello Vue 3 + TypeScript + Vite" />
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import HelloWorld from './components/HelloWorld.vue'
export default defineComponent({
name: 'App',
components: {
HelloWorld
}
})
</script>

<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
Expand Down
34 changes: 8 additions & 26 deletions packages/create-vite/template-vue-ts/src/components/HelloWorld.vue
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<script setup lang="ts">
import { ref } from 'vue'
defineProps<{ msg: string }>()
const count = ref(0)
</script>

<template>
<h1>{{ msg }}</h1>

<p>
Recommended IDE setup:
<a href="https://code.visualstudio.com/" target="_blank">VSCode</a>
+
<a
href="https://marketplace.visualstudio.com/items?itemName=octref.vetur"
target="_blank"
>
Vetur
</a>
or
<a href="https://github.com/johnsoncodehk/volar" target="_blank">Volar</a>
(if using
<code>&lt;script setup&gt;</code>)
</p>

<p>See <code>README.md</code> for more information.</p>
Expand All @@ -34,23 +33,6 @@
</p>
</template>

<script lang="ts">
import { ref, defineComponent } from 'vue'
export default defineComponent({
name: 'HelloWorld',
props: {
msg: {
type: String,
required: true
}
},
setup: () => {
const count = ref(0)
return { count }
}
})
</script>

<style scoped>
a {
color: #42b983;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference types="vite/client" />

declare module '*.vue' {
import { DefineComponent } from 'vue'
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
Expand Down
1 change: 0 additions & 1 deletion packages/create-vite/template-vue-ts/src/vite-env.d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/create-vite/template-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"vue": "^3.2.6"
},
"devDependencies": {
"@vitejs/plugin-vue": "^1.3.0",
"@vitejs/plugin-vue": "^1.6.0",
"@vue/compiler-sfc": "^3.0.5",
"vite": "^2.4.4"
"vite": "^2.5.1"
}
}
13 changes: 6 additions & 7 deletions packages/create-vite/template-vue/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<script setup>
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
import HelloWorld from './components/HelloWorld.vue'
</script>

<template>
<img alt="Vue logo" src="./assets/logo.png" />
<HelloWorld msg="Hello Vue 3 + Vite" />
</template>

<script setup>
import HelloWorld from './components/HelloWorld.vue'
// This starter template is using Vue 3 experimental <script setup> SFCs
// Check out https://github.com/vuejs/rfcs/blob/master/active-rfcs/0040-script-setup.md
</script>

<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
Expand Down
31 changes: 18 additions & 13 deletions packages/create-vite/template-vue/src/components/HelloWorld.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
<script setup>
import { ref } from 'vue'
defineProps({
msg: String
})
const count = ref(0)
</script>

<template>
<h1>{{ msg }}</h1>

<p>
Recommended IDE setup:
<a href="https://code.visualstudio.com/" target="_blank">VSCode</a>
+
<a href="https://github.com/johnsoncodehk/volar" target="_blank">Volar</a>
</p>

<p>
<a href="https://vitejs.dev/guide/features.html" target="_blank">
Vite Documentation
Expand All @@ -9,25 +26,13 @@
<a href="https://v3.vuejs.org/" target="_blank">Vue 3 Documentation</a>
</p>

<button type="button" @click="state.count++">
count is: {{ state.count }}
</button>
<button type="button" @click="count++">count is: {{ count }}</button>
<p>
Edit
<code>components/HelloWorld.vue</code> to test hot module replacement.
</p>
</template>

<script setup>
import { defineProps, reactive } from 'vue'
defineProps({
msg: String
})
const state = reactive({ count: 0 })
</script>

<style scoped>
a {
color: #42b983;
Expand Down

0 comments on commit fe9ebe7

Please sign in to comment.