如何在多个输入时自定义输出文件名?

huangapple go评论87阅读模式
英文:

How can I customize the output filename when multiple inputs?

问题

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!
英文:

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!

答案1

得分: 2

这里是使用 Vite 测试过的
https://rollupjs.org/configuration-options/#output-entryfilenames
英文:

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';
                        }

                    }
                }
            },
        },
    };
});

如何在多个输入时自定义输出文件名?

huangapple
  • 本文由 发表于 2023年6月13日 10:00:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定