vite版本5.3.5,nodejs版本20.12.2,项目3个入口,默认的index.html是PC端入口,另外两个是移动端,移动端需要用pxtorem转换,PC端不转换,我通过配置exclude来判断。
打包时PC端一部分页面的样式会被转换,甚至有的对应到源码同一个vue文件中,一部分px会被转成rem,一部分还是px,但我通过在配置的exclude方法中console.log时,发现并不包含PC端的文件。
在不分项目的情况下,这个问题能否解决?如何解决?
开发模式下一切正常
vite.config.js
import { defineConfig, loadEnv } from "vite";
import path from "path";
import createVitePlugins from "./vite/plugins";
import history from "connect-history-api-fallback";
import postCssPxToRem from "postcss-pxtorem";
const includelist = ["src/largetext", "src/views/mobile", "node_modules/vant"];
export default defineConfig(({ mode, command }) => {
return {
plugins: [
...createVitePlugins(loadEnv(mode, process.cwd()), command === "build"),
{
name: "vite-plugin-history",
configureServer(server) {
server.middlewares.use(
history({
rewrites: [
{ from: /^\/mobile/, to: "/webview.html" },
{ from: /^\/largetext/, to: "/largetext.html" }
],
htmlAcceptHeaders: ["text/html", "application/xhtml+xml"]
})
);
}
}
],
resolve: {
alias: {
"~": path.resolve(__dirname, "./"),
"@": path.resolve(__dirname, "./src")
},
extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"]
},
server: {
port: 4080,
host: true,
open: true
},
css: {
postcss: {
plugins: [
{
postcssPlugin: "internal:charset-removal",
AtRule: {
charset: (atRule) => {
if (atRule.name === "charset") {
atRule.remove();
}
}
}
},
postCssPxToRem({
rootValue: 37.5,
propList: ["*"],
exclude(file) {
return includelist.every((path) => !file.includes(path));
}
})
]
}
},
build: {
rollupOptions: {
input: {
pc: path.resolve(__dirname, "index.html"),
webview: path.resolve(__dirname, "webview.html"),
largetext: path.resolve(__dirname, "largetext.html")
}
}
}
};
});
一开始我的nodejs版本是18.x,升到20以后打包正常了,我以为是nodejs版本问题,但第二次打包就又出现了问题,之后不断测试发现,出问题的页面是随机的。
最终没找到原因,但换个思路解决了,vite配置分成三份,对应开发,PC端打包,移动端打包,package.json的scripts中,不同命令指定对应的vite配置