简体   繁体   English

当ES6代码中存在`import`和`require`指令时,Webpack绑定的代码崩溃

[英]Webpack-bundled code crashes when both `import` and `require` instructions are present in the ES6 code

Our Webpack bundle contains placeholders for the dynamic import of icons. 我们的Webpack包中包含用于动态导入图标的占位符。 An example dynamic import is as follows: 动态导入示例如下:

const { icon: iconName } = this.props;
const faIconName = `fa${iconName.replace(/./, c => c.toUpperCase())}`;

import(
  /* webpackInclude: /\.js$/ */
  `@fortawesome/pro-light-svg-icons/${faIconName}`
).then(faIcon => {
  if (this.mounted) {
    this.setState({ faIcon });
  }
});

We decided for this strategy in order to prevent Webpack from bundling up the whole collection of FontAwesome icons. 我们决定采用这种策略,以防止Webpack捆绑整个FontAwesome图标集。

Most recently we've realised the need to have internal builds where the dynamic import does not occur and pay the price of the larger bundle. 最近,我们已经意识到需要内部构建,而动态导入不会发生,并支付较大捆绑的价格。 The following code has been placed in our icon code (alongside the dynamic import above): 以下代码已放置在我们的图标代码中(与上面的动态导入一起):

const faIconName = `fa${iconName.replace(/./, c => c.toUpperCase())}`;
let faIcon;
try {
  faIcon = require(`@fortawesome/pro-light-svg-icons/${faIconName}.js`)[faIconName];
} catch (e) {}

Both loading strategies work fine when used one at a time . 当一次使用一个时,两种加载策略都可以正常工作 The issue comes when they coexist in the icon component. 当它们在图标组件中共存时会出现问题。

I have verified that the import instruction leads Webpack to create in the bundle what to me seems a synthetic JS file generated with the command: 我已经验证了import指令导致Webpack在bundle中创建对我来说似乎是用命令生成的合成JS文件:

webpack:/// ./node_modules/@fortawesome/pro-light-svg-icons lazy ^\.\/.*$ include: \.js$ namespace object

When both import and require instructions are present in the Icon component, the synthetic file is different from when the sole import is encountered. 当Icon组件中存在importrequire指令时,合成文件与遇到唯一import时不同。

In detail, the object called map in the synthetic file contains values that are arrays with 3 elements in the import case and with 2 elements in the import+require case; 详细地说,合成文件中名为map的对象包含的值是import大小写中包含3个元素且在import+require大小写中包含2个元素的数组; the synthetic code tries to access the third element and everything crashes. 合成代码试图访问第三个元素,一切都崩溃了。

Can anyone comment on this issue? 任何人都可以评论这个问题吗?

I found an answer, you may check my full answer here To make long story short i imported basing on the list and put all the imported components to Component's state. 我找到了答案,你可以在这里查看我的完整答案为了使长话题简短,我根据列表导入并将所有导入的组件放入Component的状态。 Afterwards i've created the React.createElememt's from the stored imported components and pulled all of them to Render 之后我从存储的导入组件中创建了React.createElememt,并将它们全部拉到渲染

componentDidMount = () => {
//we get elements list from any source to redux-store
        this.props.getForms();
//access redux-store to the list
        const forms = this.props.configBody.sets;
//make deep object copy
        const updatedState = { ...this.state };
        updatedState.modules = [];
        if (forms) {
//here is the very dynamic import magic: we map the import list and prepare to store the imports in Component`s state
            const importPromises = forms.map(p =>
                import(`../TemplateOrders/Template${p.order}`)
                    .then(module => {
                        updatedState.modules.push(module.default)
                    })
                    .catch(errorHandler(p))
            )
//wait till all imports are getting resolved
            Promise.all(importPromises)
                .then(res =>
//then run setState
                    this.setState({ ...updatedState }, () => {
                        console.log(this.state);
                    }))
        }
    }

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

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