当 rollup.js 中有多个输入时,如何自定义输出文件名?

fbf*_*boy 0 rollup rollupjs

配置

\n
  {\n    input: ['src/index.js', 'src/module1/index.js', 'src/module2/index.js'],\n    output: {\n      dir: 'dist',\n      format: 'es'\n    }\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

结构

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dist\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.js\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index2.js\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index3.js\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 rollup.config.js\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.js\n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 module1\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo.js\n    \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.js\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 module2\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bar.js\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.js\n
Run Code Online (Sandbox Code Playgroud)\n

期望输出是这样的

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 dist\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 libname.js\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 module\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 module1.js\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 module2.js\n
Run Code Online (Sandbox Code Playgroud)\n

有什么选项或插件可以提供帮助吗?多谢!

\n

Ale*_*hev 5

在这里,您已经使用 Vite 进行了测试: https://rollupjs.org/configuration-options/#output-entryfilenames

import { defineConfig } from 'vite';
import path from 'path';

export default defineConfig(() => {
    return {
        build: {
            rollupOptions: {
                input: ['src/index.js', 'src/module1/index.js', 'src/module2/index.js'],
                output: {
                    entryFileNames: chunk => {

                        if (chunk.facadeModuleId.endsWith('src/index.js')) {
                            return 'libname.js'
                        }
                        if (chunk.facadeModuleId.includes('/module')) {
                            const dir = path.dirname(chunk.facadeModuleId);
                            return 'module/' + path.basename(dir) + '.js';
                        }

                    }
                }
            },
        },
    };
});
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述