vite.config.ts 配置别名报错

发布于 2022-09-12 23:47:49 字数 2020 浏览 18 评论 0

环境配置 vue3 + ts
配置如下:(vite.config.ts)

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
const path = require('path')

function resolve(dir: string) {
  const _path: string = path.resolve(__dirname, dir);
  return _path;
}

// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      '/@/': resolve('src'),
      // '/@component/': resolve('src/components'),
      '/@components/': path.resolve(__dirname, './src/components')
    }
  },
  plugins: [vue()]
})

报错如下:
html:C:/Users/**/Desktop/vite-project/src/App.vue:4:23: error: Could not resolve "/@components/HelloWorld.vue" (the plugin "vite:dep-scan" didn't set a resolve directory)

4 │ import HelloWorld from '/@components/HelloWorld.vue'

error when starting dev server:
Error: Build failed with 1 error:
html:C:/Users/**/Desktop/vite-project/src/App.vue:4:23: error: Could not resolve "/@components/HelloWorld.vue" (the plugin "vite:dep-scan" didn't set a resolve directory)

at failureErrorWithLog (C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:1443:15)
at C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:1125:28
at runOnEndCallbacks (C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:915:63)
at buildResponseToResult (C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:1123:7)
at C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:1230:14
at C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:606:9
at handleIncomingPacket (C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:703:9)
at Socket.readFromStdout (C:\Users\fw0009626\Desktop\vite-project\node_modules\esbuild\lib\main.js:573:7)
at Socket.emit (events.js:315:20)
at addChunk (internal/streams/readable.js:309:12)      ╵                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£噩梦荏苒 2022-09-19 23:47:50

vite 版本 2.3.3 目前已经解决
vite.config.ts 配置如下:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
const path = require('path')
// import path from 'path'

function _resolve(dir: string) {
  return path.resolve(__dirname, dir);
}

// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      '@': _resolve('src'),
      '@assets': _resolve('src/assets'),
      '@comps': _resolve('src/components'),
      '@utils': _resolve('src/utils'),
      '@router': _resolve('src/router'),
      '@store': _resolve('src/store'),
    }
  },
  plugins: [vue()]
})

页面引用如下:

import HelloWorld from '@comps/HelloWorld.vue'
import Workld from '@assets/World.vue';
各自安好 2022-09-19 23:47:50
"devDependencies": {
    "@vitejs/plugin-vue": "^1.9.3",
    "sass": "^1.43.4",
    "sass-loader": "^12.3.0",
    "typescript": "^4.4.3",
    "vite": "^2.6.4",
    "vue-tsc": "^0.3.0"
  }

vite版本2.6.4,ts版本4.4.3,按照下面这种方式配置也可以

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';

// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: [
      {
        find: '@',
        replacement: path.resolve(__dirname, 'src'),
      },
    ],
  },
  plugins: [vue()],
});

页面引用如下

import Button from '@/packages/components/button/button.vue';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文