React项目循环引用问题

511 阅读1分钟

今天项目中出现了这样一串报错

Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization

找了一圈网上的评论大部分都说明是循环引用的报错问题。仔细研究后发现确实是循环引用的问题。 找到了一个webpack插件circular-dependency-plugin专门分析项目中循环引用的问题。

image.png 不查不知道,一查就发现好多循环引用的问题

简单介绍一下插件的使用

第一步

npm i circular-dependency-plugin -D

第二步进行配置

const CircularDependencyPlugin = require('circular-dependency-plugin');

module.exports = {
    ...,
    plugins: [
      new CircularDependsPlugin(
        {
          exclude: /node_modules/,
          include: /src/,
          failOnError: false,
          allowAsyncCycles: false,
          cwd: process.cwd()
        }
      )
    ]
}

之后再运行项目就可以了