解决Vite无法获取env环境变量的问题
在使用Vite 5时,出现使用import.meta.env仅包含默认的一些环境变量,无法获取到在.env文件定义的环境变量的问题。 查阅 官方文档 有以下说明: Vite 默认是不加载 .env 文件的,因为这些文件需要在执行完 Vite 配置后才能确定加载哪一个,举个例子,root 和 envDir 选项会影响加载行为。不过当你的确需要时,你可以使用 Vite 导出的 loadEnv 函数来加载指定的 .env 文件。 因此,需要在vite.config.js中手动定义所需的环境变量。例如获取.env文件中的VITE_MY_APP_ENV变量,可…
2024-09-14修复date-fns-tz导入失败问题
在执行yarn build打包node项目时,出现以下错误: [vite]: Rollup failed to resolve import "date-fns/_lib/cloneObject/index.js" from "node_modules/date-fns-tz/esm/formatInTimeZone/index.js". This is most likely unintended because it can break your application at runtime. If you do want to externalize this module explicitly add it to `build.rollupOptions.external` error during build: Error: [vite]: Rollup failed to resolve import "date-fns/_lib/clon…
2022-09-23vite项目在二级目录下的部署
使用history路由模式,假定项目在admin目录下,需要使用类似于如下url访问: https://myapp.com/admin/auth/login https://myapp.com/admin/dashboard 修改 公共基础路径,适配静态文件。 vite.config.js export default defineConfig({ base: '/admin/', // ... }); 修改路由前缀,内部路由跳转时追加上前缀。 src/router/index.ts const router = createRouter({ history: createWebHistory('/admin/'), routes }) Nginx 配置。 location /admin { …
2021-11-29