Search code examples
rolluprollupjs

How can I customize the output filename when multiple inputs in rollup.js?


config

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

structure

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

expect the outputs to be like this

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

Is there any option or plugin could help? Thanks a lot!


Solution

  • Here you go, tested with 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';
                            }
    
                        }
                    }
                },
            },
        };
    });
    

    enter image description here