React-hot-loader: The published tagCommonExports.js is incorrectly optimized in 3.0.0-beta.7

Created on 4 Oct 2017  路  5Comments  路  Source: gaearon/react-hot-loader

This was a head-scratcher. I had react-hot-loader working in a build, and a yarn upgrade stopped it. After diagnosing a variety of issues, I found that the react-hot-loader/webpack loader was using module.exports to find module exports and register them. It needed to be looking at __webpack_exports__.

I'm skipping the usual issue reporting because you can repro this by just pulling the tarball published for 3.0.0-beta.7 here: https://registry.npmjs.org/react-hot-loader/-/react-hot-loader-3.0.0-beta.7.tgz

If you open it up, you'll see this is what lib/webpack/tagCommonJSExports.js looks like:

'use strict';

/* global __FILENAME__ */

;(function register() {
  // eslint-disable-line no-extra-semi
  /* react-hot-loader/webpack */
  if (process.env.NODE_ENV !== 'production') {
    if (typeof __REACT_HOT_LOADER__ === 'undefined') {
      return;
    }

    if (typeof module.exports === 'function') {
      __REACT_HOT_LOADER__.register(module.exports, 'module.exports', __FILENAME__);
      return;
    }
// ...

But here's what the source looks like:

/* global __FILENAME__ */

;(function register() {
  // eslint-disable-line no-extra-semi
  /* react-hot-loader/webpack */
  if (process.env.NODE_ENV !== 'production') {
    if (typeof __REACT_HOT_LOADER__ === 'undefined') {
      return
    }

    /* eslint-disable camelcase, no-undef */
    const webpackExports =
      typeof __webpack_exports__ !== 'undefined'
        ? __webpack_exports__
        : module.exports
    /* eslint-enable camelcase, no-undef */

    if (typeof webpackExports === 'function') {
      __REACT_HOT_LOADER__.register(
        webpackExports,
        'module.exports',
        __FILENAME__,
      )
      return
    }

See the difference? webpackExports was optimized out in an erroneous build. I can't reproduce it on my machine because when I run yarn build, I get this:

'use strict';

/* global __FILENAME__ */

;(function register() {
  // eslint-disable-line no-extra-semi
  /* react-hot-loader/webpack */
  if (process.env.NODE_ENV !== 'production') {
    if (typeof __REACT_HOT_LOADER__ === 'undefined') {
      return;
    }

    /* eslint-disable camelcase, no-undef */
    var webpackExports = typeof __webpack_exports__ !== 'undefined' ? __webpack_exports__ : module.exports;
    /* eslint-enable camelcase, no-undef */

    if (typeof webpackExports === 'function') {
      __REACT_HOT_LOADER__.register(webpackExports, 'module.exports', __FILENAME__);
      return;
    }
// ...

Solution:

Rebuild the package and publish a version bump. Something happened with the last published build to cause it to optimize out the webpackExports variable.

All 5 comments

@AaronFriel thank you! Great find! I think I will work on a v4 soon because 3.0 is very confusing, beta for more than a year now 馃. I will pay a special attention to what you found.

Correction: it looks like the tarball and version hasn't been updated since some key fixes were implemented over the past few months.

@gaearon, is there a reason these fixes aren't being published?

We will publish a new version as soon as possible, currently no one has time to do it.

How long does it take to publish a new version? (Honestly asking, I'm not familiar with the NPM publishing process.)

@AaronFriel publishing on npm is the final step, it doesn't take so much time.

I want to be careful by publishing a good working version and I don't want to do it in 5 minutes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mtscout6 picture mtscout6  路  3Comments

Opty1712 picture Opty1712  路  4Comments

niba picture niba  路  4Comments

tiberiumaxim picture tiberiumaxim  路  4Comments

Anahkiasen picture Anahkiasen  路  5Comments