简体   繁体   English

使用 webpack/react/gatsby 进行动态导入和热重载

[英]Dynamic imports and hot reload with webpack / react / gatsby

I'm developing a little website with the framework gatsby but I'm having an hard time with dynamic import and the hot reload provided by webpack.我正在使用 gatsby 框架开发一个小网站,但是我在动态导入和 webpack 提供的热重载方面遇到了困难。

Here is my react component :这是我的反应组件:

import React from 'react'

import styles from './_frame.module.scss'

class Frame extends React.Component {
  constructor(props) {
    super(props);
    this.frame = React.createRef();
  }

  render() {
    return (
      <div className={styles.frame}
           ref={this.frame}>
        <div className="inner wrap--redux">
          <canvas className={styles.canvas}
                  id="canvas"
                  width="800"
                  height="600">
          </canvas>
        </div>
      </div>
    );
  }

  componentDidMount() {
    import(`../../experiments/${this.props.slug}`)
      .then(res => {
        new res.default(this.frame.current);
      });
  }
};
export default Frame

I would like when I make a modification to the JavaScript imported that the hot reload triggers and refreshes my component.我希望当我对导入的 JavaScript 进行修改时,热重载会触发并刷新我的组件。 Currently I have to refresh my browser manually to observe the changes made.目前我必须手动刷新浏览器以观察所做的更改。

You can see the whole repository here: https://github.com/maximeparisse/webgl-journey您可以在此处查看整个存储库: https : //github.com/maximeparisse/webgl-journey

And the file from which the code is extracted: https://github.com/maximeparisse/webgl-journey/blob/master/src/components/experiments/frame.js以及从中提取代码的文件: https : //github.com/maximeparisse/webgl-journey/blob/master/src/components/experiments/frame.js

If you want to reproduce just - git clone the repository - npm i - gatsby develop如果你只想复制 - git clone the repository - npm i - gatsby develop

Thanks for your help.谢谢你的帮助。

After trying many things, including wsl, npm clenaup etc, I realized that my antivirus sophos was blocking the webpack hot reload socket __webpack_hmr .在尝试了很多事情之后,包括 wsl、npm clenaup 等,我意识到我的杀毒软件sophos正在阻止 webpack 热重载套接字__webpack_hmr

All I had to do is to disable the antivirus for a while.我所要做的就是暂时禁用防病毒软件。 ( You are free to uninstall it or maybe change to another one ). (您可以随意卸载它或更换另一个)。

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

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