首页 > 解决方案 > 汇总可选输入

问题描述

如果找不到输入,汇总中有没有办法跳过输入?目前,Error: Could not resolve entry module (src/index.js)一旦找不到文件,构建就会失败。

我浏览了文档并四处搜索,但似乎找不到实现此目的的选项或钩子。在下面的简化示例中,我想在找不到page.js时继续下一个构建。src/index.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: [
            // ...
        ],
    },
];

标签: javascriptnode.jswebpackrolluprollupjs

解决方案


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

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;

推荐阅读