[英]How do I need to shape my modules for dynamic import() for webpack?
When trying to use import() with webpack
using one of our components, I end up getting back an object like this instead of the module: 当尝试使用我们的组件之一将
import() with webpack
使用时,我最终会找回这样的对象而不是模块:
{
__webpackChunkName: "_nteract_notebook_app_component_6bdbaebb993d75f174e800c88ff17fc7"
}
Example code: 示例代码:
import("@nteract/notebook-app-component").then(App => {
// Rely on App!
// ...
// Except not, because we've got that strange little webpack object
})
Which is in steep contrast to how lodash loads, which provides the real module. 这与lodash的加载方式形成鲜明对比,后者提供了实际的模块。
import("lodash").then(_ => {
// Full blown lodash here!
})
If I look at the generated chunk file, it does have javascript in it. 如果我查看生成的块文件,它确实包含了javascript。 One thing (that might be a red herring) is that some modules have
/*! no static exports found */
一件事(可能是红色鲱鱼)是某些模块具有
/*! no static exports found */
/*! no static exports found */
in them yet have code as well. /*! no static exports found */
,但也有代码。 Here's the first 10 lines of the chunk: 这是该块的前10行:
$ head -n 10 dist/chunks/_nteract_notebook_app_component_6bdbaebb993d75f174e800c88ff17fc7.js
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["chunks/_nteract_notebook_app_component_6bdbaebb993d75f174e800c88ff17fc7"],{
/***/ "../../../packages/dropdown-menu/src/index.js":
/*!************************************************************************************************!*\
!*** /Users/kylek/code/src/github.com/nteract/nteract-ext/packages/dropdown-menu/src/index.js ***!
\************************************************************************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
After that it's a big chunk of eval
ed code this is the actual module. 之后,这是大量的
eval
代码,这是实际的模块。
This is a result of using the next.js preset for babel, due to their custom handling of import()
. 这是由于对babel使用next.js预设的结果,因为它们对
import()
进行了自定义处理 。 They create an object with the webpackChunkName
to be used in a lookup later using their dynamic()
function. 他们使用
webpackChunkName
创建一个对象,以便稍后使用他们的dynamic()
函数在查找中使用。
In order to allow dynamic imports with webpack (and babel), you need to not use the next/babel
preset on non-next.js apps and add the syntax-dynamic-import
plugin to your babelrc. 为了允许通过webpack(和babel)进行动态导入,您无需在非next.js应用程序上使用
next/babel
预设,而可以将syntax-dynamic-import
插件添加到babelrc中。
npm install --save-dev babel-plugin-syntax-dynamic-import
Example babelrc: 示例babelrc:
{
"presets": ["env", "react"],
"plugins": [
"syntax-dynamic-import",
"styled-jsx/babel"
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.