欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > 【Vite】控制打包结构

【Vite】控制打包结构

2025/2/23 12:55:13 来源:https://blog.csdn.net/Superman_H/article/details/139905550  浏览:    关键词:【Vite】控制打包结构

配置 vite.config.json 文件:

import { defineConfig } from "vite";export default defineConfig({// ...build: {rollupOptions: {output: {entryFileNames: "js/[name]-[hash].js",chunkFileNames: "js/[name]-[hash].js",assetFileNames(chunkInfo) {// CSS 文件if (chunkInfo.name?.endsWith(".css")) {return "css/[name]-[hash][extname]";}// 字体文件if ([".woff", ".woff2", ".ttf", ".eot"].some((ext) =>chunkInfo.name?.endsWith(ext))) {return "font/[name]-[hash][extname]";}// 图片文件if ([".png", ".jpg", ".jpeg", ".gif", ".svg", ".webp"].some((ext) => chunkInfo.name?.endsWith(ext))) {return "imgs/[name]-[hash][extname]";}// 其他文件return "assets/[name]-[hash][extname]";},},},},
});

如果为 Vite + TS 项目,此时 vite.config.json 文件会在 endsWith 处抛出错误:Property ‘endsWith’ does not exist on type ‘string’. Do you need to change your target library? Try changing the ‘lib’ compiler option to ‘es2015’ or later.

打开 tsconfig.json 一看 target,是 "ES2020" 昂!奇了怪了…

仔细看看 tsconfig.json 文件,欸~ tsconfig.json 文件的最下面有一行配置:

    "references": [{ "path": "./tsconfig.node.json" }]

到 tsconfig.node.json 一看,没有配置 target,默认为 "ES3"!再看一眼 include,为 ["vite.config.ts"]!回到 tsconfig.json 看一眼 include,为 ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"]

噢~ 我好像懂了!tsconfig.json 为业务文件的 TS 配置文件,tsconfig.node.json 为 vite.config.ts 的 TS 配置文件!

于是我在 tsconfig.node.json 中配置 target"ES2020"

{"compilerOptions": {// 其他配置项"target": "ES2020" // 添加这一行},"include": ["vite.config.ts"]
}

OK!没有报错了~


版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词