如何设置vite.config.js基本公共路径?

2024-05-17

我正在尝试设置一个base url对于我的开发和生产环境,但是vitejs配置未解决。

根据vitejs https://vitejs.dev/config/,您可以在配置选项中设置在开发或生产中提供服务时的基本公共路径。

当从命令行运行 vite 时,Vite 会自动尝试解析项目根目录中名为 vite.config.js 的配置文件。

问题是我的申请请求没有通过'http://localhost:8080/',但仍附加到默认服务端口http://localhost:3000/.

我当前的配置如下:

// vite.config.js
export default {
  base: 'http://localhost:8080/'
}
// packages.json
{
  "name": "vue3ui",
  "version": "0.0.0",
  "scripts": {
    "dev": "vite",
    "build": "vite build"
  },
  "dependencies": {
    ...,
    "vue": "^3.0.11"
  },
  "devDependencies": {
    "@vue/compiler-sfc": "^3.0.11",
    "vite": "^1.0.0-rc.13"
  }
}

Not totally clear to me but I will try to give you an answer to achieve what you want.

我正在尝试为我的开发和生产环境设置一个基本 url

Edit:我又读了一遍你的问题,我认为你正在寻找要点C关于这个答案。

应该做出改变vite.config.js

A)您正在寻找更改运行端口3000 to 8080, 调整server.port

server: {
  port: '8080'
}

B)但如果你想继续跑步localhost:3000并将请求转发至localhost:8080那么你必须调整server.proxy

server: {
    proxy: {
      '/': {
        target: 'http://localhost:8080/'
      },

      '/admin': {
        target: 'http://localhost:8081/'
      }
    }
  }

example:

  • '/':将所有请求代理到localhost:8080
  • '/admin':将仅代理具有端点的请求/admin to http://localhost:8081

C)根据开发或生产环境更改基本路径

.env file :

// .env
 
// Running locally
APP_ENV=local
// you change port of local/dev here to :8000
// do not forget to adjust `server.port`
ASSET_URL=http://localhost:3000
 
// Running production build
APP_ENV=production
ASSET_URL=https://your-prod-asset-domain.com

vite.config.js:

const ASSET_URL = process.env.ASSET_URL || '';

export default { 
  base: `${ASSET_URL}/dist/`,

  [...]
}

如果这不是您想要的,请更准确,我将编辑我的答案。

欲了解更多信息,请前往官方文档:https://vitejs.dev/config/#server-options https://vitejs.dev/config/#server-options

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何设置vite.config.js基本公共路径? 的相关文章

随机推荐