Skip to content

eltorio/vue-sfc-bug

Repository files navigation

vue-sfc-bug

This template should help get you started developing with Vue 3 in Vite.

to reproduce

npm init vue@latest  # with typescript router pinia -no test (name is vue-sfc-bug
cd vue-sfc-bug
npm i

edit main.ts

const useImage = (url: string) => {
    return new URL(`/src/${url}`, import.meta.url).href;
  };

edit tsconfig.json

{
    compilerOptions": {
    "module": "esnext",
    }
}

run

npm run dev

and see error

12:04:38 PM [vite] Internal server error: At least one <template> or <script> is required in a single file component.
  Plugin: vite:vue
  File: /Users/name/Development/vue-sfc-bug/src/views/HomeView.vue
      at Object.parse (/Users/name/Development/vue-sfc-bug/node_modules/@vue/compiler-sfc/dist/compiler-sfc.cjs.js:5225:21)
      at createDescriptor (/Users/name/Development/vue-sfc-bug/node_modules/@vitejs/plugin-vue/dist/index.cjs:65:43)
      at transformMain (/Users/name/Development/vue-sfc-bug/node_modules/@vitejs/plugin-vue/dist/index.cjs:2216:34)
      at TransformContext.transform (/Users/name/Development/vue-sfc-bug/node_modules/@vitejs/plugin-vue/dist/index.cjs:2705:16)
      at Object.transform (file:///Users/name/Development/vue-sfc-bug/node_modules/vite/dist/node/chunks/dep-51c4f80a.js:40224:44)

Recommended IDE Setup

VSCode + Volar (and disable Vetur) + TypeScript Vue Plugin (Volar).

Type Support for .vue Imports in TS

TypeScript cannot handle type information for .vue imports by default, so we replace the tsc CLI with vue-tsc for type checking. In editors, we need TypeScript Vue Plugin (Volar) to make the TypeScript language service aware of .vue types.

If the standalone TypeScript plugin doesn't feel fast enough to you, Volar has also implemented a Take Over Mode that is more performant. You can enable it by the following steps:

  1. Disable the built-in TypeScript Extension
    1. Run Extensions: Show Built-in Extensions from VSCode's command palette
    2. Find TypeScript and JavaScript Language Features, right click and select Disable (Workspace)
  2. Reload the VSCode window by running Developer: Reload Window from the command palette.

Customize configuration

See Vite Configuration Reference.

Project Setup

npm install

Compile and Hot-Reload for Development

npm run dev

Type-Check, Compile and Minify for Production

npm run build

vue-sfc-bug with vue 3.2.44