当我试图简化一个导入时,我遇到了webpack TypeError。以下代码可以正常工作,我从core/components/form/index.js
中导入了一个名为smartForm
的React高阶组件(HOC)。
core/components/form/index.js(对smartForm
进行命名导出)
export smartForm from './smart-form';
登录表单.jsx(导入并使用smartForm
)
import { smartForm } from 'core/components/form';
class LoginForm extends React.Component {
...
}
export default smartForm(LoginForm);
然而,我想将导入简化为 import { smartForm } from 'core'
。因此,在 core/index.js
中重新导出了 smart-form
并从 core
导入它。请参见下面的代码:
core/index.js(对 smartForm
进行了命名导出)
export { smartForm } from './components/form';
// export smartForm from './components/form'; <--- Also tried this
login-form.jsx(导入并使用smartForm
)
import { smartForm } from 'core';
class LoginForm extends React.Component {
...
}
export default smartForm(LoginForm); // <--- Runtime exception here
这段代码编译没有任何问题,但是我在export default smartForm(LoginForm);
这一行收到了以下运行时异常:
login-form.jsx:83 Uncaught TypeError: webpack_require.i(...)不是一个函数(…)
我错过了什么?
附注:我使用的Babel和插件版本如下:
"babel-core": "^6.18.2",
"babel-preset-es2015-webpack": "^6.4.3",
"babel-preset-react": "^6.16.0",
"babel-preset-stage-1": "^6.16.0",
import { smartForm } from './core'
就足够了。当给定一个目录时,节点模块系统会自动加载index.js
。(https://nodejs.org/api/modules.html#modules_folders_as_modules) - Spain Train