简体   繁体   English

使用Webpack导入时出现意外的未定义

[英]Unexpected undefined while import with Webpack

I have a problem that has never happened to me before: I'm compiling a little basic starter browser web app (with React) using Webpack + Babel 7. I've got three different file: 我遇到的问题以前从未发生过:我正在使用Webpack + Babel 7编译一个基本的入门浏览器Web应用程序(使用React)。我有三个不同的文件:

  1. withAuth.js The Auth High Order Component withAuth.js Auth高阶组件
  2. NavBar.js The NavBar Component NavBar.js NavBar组件
  3. Login.js The Login Form Login.js登录表单

If I import the withAuth HOC in the NavBar is everything alright, but if I import the withAuth component in the Login.js file it return undefined 如果我在NavBar中导入withAuth HOC,就可以了,但是如果我在Login.js文件中导入withAuth组件,则返回undefined

/** withAuth.js */

console.log('withAuth Loaded');

const withAuth = Child => ChildProps => (
    <AuthContext.Consumer>
        { authClient => <Child {...ChildProps} authClient={authClient} }
    </AuthContext.Consumer>
)

export { withAuth };


/** NavBar.js */
import { withAuth } from 'HOC/Auth';

console.log('NavBar Loaded', withAuth); // <- My HOC

const NavBarComponent = (authClient) => { /* ... My Code ... */ }

const NavBar = withAuth(NavBarComponent);

export default NavBar;


/** Login.js */
import { withAuth } from 'HOC/Auth';

console.log('Login Loaded', withAuth); // <- undefined ??

const LoginFormComponent = (authClient) => { /* ... My Code ... */ }

const LoginForm = withAuth(LoginFormComponent);
//                /|\
//                 |
//    Will produce an Error, withAuth is Undefined

This is my Webpack Configuration: 这是我的Webpack配置:

/** webpack.config.js */

module.exports = {
    entry: { core: 'index.js' },
    resolve: {
        alias: {
            HOC: './path/to/hoc/folder'
        }
    },
    optimization: {
        runtimeChunk: 'single',
        splitChunks: {
            chunks: 'all'
        }
    },
    plugins: [ /* Various Plugin */ ],
    module: {
       rules: [ /* My Rules */ ]
    }
}

Any one know why my HOC is undefined ? 有人知道为什么我的HOC undefined吗?

Edit: I've placed Console Log in the tree file. 编辑:我已经将控制台日志放在树文件中。 The result are: 结果是:

'Login Loaded' - undefined
'withAuth Loaded'
'NavBar Loaded' - function() { }

Edit 2: This is the files structure: 编辑2:这是文件结构:

app/
|-high-order-component/
| |-auth/
|   |-withAuth.js
|
|-layout-component/
| |-navbar/
|   |-index.js
|
|-pages/
  |-auth/
    |-login.js

Resolved 解决

After much testing and research throughout the afternoon I came to the solution of the problem. 经过整个下午的大量测试和研究,我找到了解决问题的方法。 As I said in the question, mine is a larger project and I only partially wrote its structure because I thought the problem was located in those three files. 就像我在问题中所说的,我的是一个更大的项目,我只写了一部分结构,因为我认为问题出在这三个文件中。

In reality, the problem was a Circular Dependency problem and not a Webpack configuration problem. 实际上,该问题是循环依赖问题,而不是Webpack配置问题。 In my project I have a module called 'Route' that store all Path and all Component for Path, so I can build the React Router using Array Map function. 在我的项目中,我有一个名为“ Route”的模块,该模块存储所有Path和Path的所有组件,因此我可以使用Array Map函数构建React Router。 That module has a function that allow me to Route through path and that can return me a path string to a Component. 该模块具有允许我通过路径进行路由的功能,并且可以向我返回指向组件的路径字符串。 My problem was due to the fact that this module is often called in the project and this has created a Circular Dependency. 我的问题是由于在项目中经常调用此模块,并且这已经创建了循环依赖关系。

Webpack doesn't show the Circular Dependency during compiling, but I found useful adding a plugin, called CircualDependencyPlugin . Webpack在编译过程中没有显示循环依赖,但是我发现添加一个名为CircualDependencyPlugin的插件很有用。 This plugin will break Webpack compiling when a Circual Dependency will be found. 找到循环依赖项时,此插件将中断Webpack的编译。

Splitting the Route module into two files solved my problem. Route模块拆分为两个文件解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM