汇总可选输入

Rollup optional input

Rollup 中是否有一种方法可以在未找到输入时跳过它?目前,一旦找不到文件,构建就会失败并显示 Error: Could not resolve entry module (src/index.js)

我浏览了文档并四处搜索,但似乎找不到实现此目的的选项或挂钩。在下面的简化示例中,我想在找不到 src/index.js 时继续下一个 page.js 构建。

export default [
    {
        input: 'src/index.js',
        output: [
            {
                file: 'dist/esm/index.esm.js',
                format: 'esm',
            },
            {
                file: 'dist/cjs/index.js',
                format: 'cjs',
            },
        ],
        plugins: [
            // ...
        ],
    },
    {
        input: 'page.js',
        output: [
            {
                file: 'dist/esm/page.esm.js',
                format: 'esm',
            },
            {
                file: 'dist/cjs/page.js',
                format: 'cjs',
            },
        ],
        plugins: [
            // ...
        ],
    },
];

不知道这是否可行,请编写代码进一步说明我所说的潜在解决方案。

const fs = require('fs');
const path = 'src/index.js';

const config = [
{
        input: 'page.js',
        output: [
            {
                file: 'dist/esm/page.esm.js',
                format: 'esm',
            },
            {
                file: 'dist/cjs/page.js',
                format: 'cjs',
            },
        ],
        plugins: [
            // ...
        ],
}];

const determineFileExistsForConfig = () => {
  try {
     // if index exists, add to the config
     if (fs.existsSync(path)) {
       config.push({
        input: 'src/index.js',
        output: [
            {
                file: 'dist/esm/index.esm.js',
                format: 'esm',
            },
            {
                file: 'dist/cjs/index.js',
                format: 'cjs',
            },
        ],
        plugins: [
            // ...
        ],
    });
     }
  } catch(err) {
    return config;
  }
}


const finalConfig = determineFileExistsForConfig();
export default finalConfig;