vite
vite copied to clipboard
设置envDir参数,热更新失效。
Describe the bug
像这样设置envDir: "./src/project/electronicCalendar",环境变量可以读取到但是热更新失效, vite版本是5.0.0。
Reproduction
wu
Steps to reproduce
No response
System Info
windows 10
Used Package Manager
npm
Logs
export default ({ mode }) => {
const env = loadEnv(mode, ${process.cwd()}
);
return defineConfig({
envDir: "./src/project/electronicCalendar",
base: env.VITE_BASE_URL,
esbuild: {
// 避免iconfont打包后乱码
charset: "ascii"
},
plugins: [
vue(),
createHtmlPlugin({
entry: "/src/project/electronicCalendar/main.js"
}),
// Icons图标自动下载
Icons({ autoInstall: true }),
AutoImport({
resolvers: [
// 这个是组件自动导入
ElementPlusResolver(),
IconsResolver({
prefix: "icon"
})
]
}),
// 自动导入components下的自定义组件
Components({
// 可以让我们使用自己定义组件的时候免去 import 的麻烦
dirs: ["src/components/", "src/layouts/components/", "src/project/electronicCalendar/components/"],
// 配置需要将哪些后缀类型的文件进行自动按需引入,'vue'为默认值
extensions: ["vue"],
// 解析组件
resolvers: [
// 这个是组件自动导入
ElementPlusResolver(),
// 自动注册图标组件
IconsResolver({
// 修改Icon组件前缀,不设置则默认为i,禁用则设置为false
prefix: "icon",
// 指定collection,即指定为elementplus图标集ep
enabledCollections: ["ep"]
})
]
}),
legacyPlugin({
targets: ["chrome 52"] // 需要兼容的目标列表,可以设置多个
}),
viteCompression({
filter: /.(js|mjs|json|css|html)$/i, //过滤器,对哪些类型的文件进行压缩,默认为/.(js|mjs|json|css|html)$/i
verbose: true, //是否在控制台输出压缩结果,默认为 true
disable: false, //是否禁用压缩,默认为 false
threshold: 10240, //启用压缩的文件大小限制,单位是字节,默认为 0
deleteOriginFile: false, //压缩后是否删除原文件,默认为 false
algorithm: "gzip", //采用的压缩算法,默认是 gzip
ext: ".gz" //生成的压缩包后缀
})
],
resolve: {
alias: {
"@submodule": fileURLToPath(new URL("./", import.meta.url)),
"@": ${process.cwd()}/src
},
/* 暂时先加.vue, .js, .json */
extensions: [".vue", ".js", ".json"]
},
build: {
minify: "terser",
outDir: "../production/electronicCalendar",
terserOptions: {
compress: {
//移除console和debugger
drop_console: true,
drop_debugger: true
}
},
// 关闭文件计算
reportCompressedSize: false,
// 关闭生成map文件 可以达到缩小打包体积
sourcemap: false, // 这个生产环境一定要关闭,不然打包的产物会很大
rollupOptions: {
output: {
//静态资源分类打包
chunkFileNames: "static/js/[name]-[hash].js",
entryFileNames: "static/js/[name]-[hash].js",
assetFileNames: "static/[ext]/[name]-[hash].[ext]",
manualChunks(id) {
//静态资源分拆打包
if (id.includes("node_modules")) {
return id.toString().split("node_modules/")[1].split("/")[0].toString();
}
}
}
}
}
});
};
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
Hello @Aigui. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs reproduction
will be closed if they have no activity within 3 days.