SKing7
SKing7

Reputation: 2356

why the use of window in webpack entry file will affact the React hot reload?

Env: [email protected] + [email protected] + [email protected] + [email protected]

Entry File: index.js

const MOUNT_NODE = document.getElementById('root');

// Line A
const __DEV__ = window.__DEV__;


let render = () => {
  const App = require('./components/App').default;
  const routes = require('./pages/router').default(store);

  ReactDOM.render(
    <App store={store} routes={routes} />,
    MOUNT_NODE
  );
};

// Development Tools
// ------------------------------------
if (__DEV__) {
  if (module.hot) {
    const renderApp = render;
    const renderError = (error) => {
      const RedBox = require('redbox-react').default;

      ReactDOM.render(<RedBox error={error} />, MOUNT_NODE);
    };

    render = () => {
      try {
        renderApp();
      } catch (e) {
        renderError(e);
      }
    };

    // Setup hot module replacement
    module.hot.accept([
      './components/App',
      './pages/router',
    ], () =>
      setImmediate(() => {
        ReactDOM.unmountComponentAtNode(MOUNT_NODE);
        render();
      })
    );
    module.hot.accept();
  }
}

the __DEV__ is defined in webpack config file:

new webpack.DefinePlugin(Object.assign({
  __DEV__: project.env === 'development',
}, project.globals))

and then i start the local dev server.

but when i change the jsx, the hot reload don't work, and the warning tips:

process-update.js?e13e:95 [HMR] The following modules couldn't be hot updated: (Full reload needed) This is usually because the modules which have changed (and their parents) do not know how to hot reload themselves. See https://webpack.js.org/concepts/hot-module-replacement/ for more details.

but when i delete the line A, the hot reload work fine, the warning tips also disappear。

this is why? thanks.

Upvotes: 0

Views: 217

Answers (1)

Tan Duong
Tan Duong

Reputation: 2142

It should be string. You need to use JSON.stringify to convert Boolean to String. It should be

new webpack.DefinePlugin(Object.assign({
  __DEV__: JSON.stringify(project.env === 'development'),
}, project.globals))

Please check DefinePlugin

Upvotes: 0

Related Questions