React配置 webpack多页应用打包失败

4 篇文章 0 订阅
3 篇文章 0 订阅

React配置 webpack多页应用打包失败,报错Cannot read property ‘filter’ of undefined解决方案

react的多页应用,运行 npm run build 后控制台报错:

npm run build
node scripts/build.js
Creating an optimized production build...
Failed to compile.
Cannot read property 'filter' of undefined
  • 控制台并没有明确提示错误位置,所以在webpack.config.js 文件中找用到 filter方法的地方检查。以为是 entry
    配置有问题,但经过检查发现问题并不在 entry 之后对比了不同版本的 create-react-app 生成的
    webpack.config.js文件中所用到 filter方法的地方,发现新的配置文件中 ManifestPlugin
    的配置项发生了变化 可以看到配置项中多了 generate 这一属性,其中就有用到 filter方法。其中
    entrypoints.main 调用了 filter方法,经过输出得知 entrypoints.main 的值是与 entry
    配置对应的。 默认情况下 entry 的值是一个 Array,因此 entrypoints.main 的值也一个
    Array,但是配置多页面时是把其改为了 Object,Object 没有 filter方法,因此报错

解决方法:

  • 修改 entrypoints.main 中的 main 为你在 entry 中配置的项目首页的 key。我的 entry 配置中首页的
    key 为 index ,因此可改为 entrypoints.index

修改之前

 new ManifestPlugin({
                fileName: 'asset-manifest.json',
                publicPath: paths.publicUrlOrPath,
                generate: (seed, files, entrypoints) => {
                    const manifestFiles = files.reduce((manifest, file) => {
                        manifest[file.name] = file.path;
                        return manifest;
                    }, seed);
                    const entrypointFiles = entrypoints.main.filter(
                        fileName => !fileName.endsWith('.map')
                    );

                    return {
                        files: manifestFiles,
                        entrypoints: entrypointFiles,
                    };
                },
            }),

修改之后

 new ManifestPlugin({
                fileName: 'asset-manifest.json',
                publicPath: paths.publicUrlOrPath,
                generate: (seed, files, entrypoints) => {
                    const manifestFiles = files.reduce((manifest, file) => {
                        manifest[file.name] = file.path;
                        return manifest;
                    }, seed);
                    const entrypointFiles = entrypoints.index.filter(
                        fileName => !fileName.endsWith('.map')
                    );

                    return {
                        files: manifestFiles,
                        entrypoints: entrypointFiles,
                    };
                },
            }),
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值