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

问题描述 投票:0回答:1

配置

  {
    input: ['src/index.js', 'src/module1/index.js', 'src/module2/index.js'],
    output: {
      dir: 'dist',
      format: 'es'
    }
  }

结构

├── dist
│   ├── index.js
│   ├── index2.js
│   └── index3.js
├── rollup.config.js
└── src
    ├── index.js
    ├── module1
    │   ├── foo.js
    │   └── index.js
    └── module2
        ├── bar.js
        └── index.js

期望输出是这样的

├── dist
│   ├── libname.js
│   └── module
│       ├── module1.js
│       └── module2.js

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

rollup rollupjs
1个回答
4
投票

给你,用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';
                        }

                    }
                }
            },
        },
    };
});

© www.soinside.com 2019 - 2024. All rights reserved.