假设我有这个文件夹结构:
parent
|-parent.html
|-parent.js
|-child
|--child.html
|--child.js
我希望它们在我的 dist 文件夹中以相同的结构输出。
默认情况下,这是获取输出的内容:
dist/assets/parent.js
dist/assets/child.js
我希望他们像这样输出:
dist/parent/parent.js
dist/parent/child/child.js
我尝试改变assetFileNames
Rollup 的选项,但它没有做任何事情。
输出文件名在 Rollup 中配置为build.rollupOptions https://vitejs.dev/config/#build-rollupoptions. Set output.entryFileNames https://rollupjs.org/guide/en/#outputentryfilenames配置条目的位置.js
文件以匹配其原始目录结构:
// vite.config.js
import { fileURLToPath } from 'url';
import { defineConfig } from 'vite';
import path from 'path';
const rootDir = fileURLToPath(new URL('.', import.meta.url));
export default defineConfig({
build: {
rollupOptions: {
input: {
parent: './parent/parent.html',
child: './parent/child/child.html',
},
output: {
entryFileNames: (assetInfo) => {
// assetInfo.facadeModuleId contains the file's full path
if (assetInfo.facadeModuleId) {
const assetPath = path.dirname(assetInfo.facadeModuleId).replace(rootDir, '');
return assetPath + '/[name]-[hash].js';
} else {
return 'assets/js/[name]-[hash].js';
}
},
},
},
},
});
demo https://stackblitz.com/edit/vitejs-output-file-paths-1amrwa?file=vite.config.js
Notes
-
资产(例如.css
文件)和共享模块(供应商.js
chunks) 无法使用上述解决方案进行重定向,因为相关挂钩中的资产信息不提供文件的完整路径。
-
在普通 Rollup 项目中,output.preserveModules=true https://rollupjs.org/guide/en/#outputpreservemodules本来就达到了最初的目的,但是这个选项和Vite自己的Rollup设置有冲突。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)