Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaks with .mjs #1102

Closed
QuentinRoy opened this issue Nov 13, 2018 · 6 comments
Closed

Breaks with .mjs #1102

QuentinRoy opened this issue Nov 13, 2018 · 6 comments
Assignees
Milestone

Comments

@QuentinRoy
Copy link

QuentinRoy commented Nov 13, 2018

Description

react-hot-loader breaks the execution of any project that includes a .mjs file.

Expected behavior

One should be able to import .mjs files. These are more and more common. Eg. GraphQL is publishing .mjs files.

Actual behavior

It breaks the execution of the app (not the compilation). I think the main reason is that Webpack has a special kind of hidden mode for .mjs file and will entirely ignore any require (which react-hot-loader is injecting). For example, when using GraphQL:

Uncaught ReferenceError: require is not defined
    at eval (defineToJSON.mjs:4)
    at eval (defineToJSON.mjs:7)
    at Module.../node_modules/graphql/jsutils/defineToJSON.mjs (VM53 experiment.js:1900)
    at __webpack_require__ (VM53 experiment.js:725)
    at fn (VM53 experiment.js:102)
    at eval (definition.mjs:49)
    at Module.../node_modules/graphql/type/definition.mjs (VM53 experiment.js:2308)
    at __webpack_require__ (VM53 experiment.js:725)
    at fn (VM53 experiment.js:102)
    at eval (validate.mjs:5)

When inspecting the code, it is clear that require is not being properly compiled. Here is one of graphql modules as imported in my project:

__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "graphql", function() { return graphql; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "graphqlSync", function() { return graphqlSync; });
/* harmony import */ var _type_validate__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./type/validate */ "../node_modules/graphql/type/validate.mjs");
/* harmony import */ var _language_parser__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./language/parser */ "../node_modules/graphql/language/parser.mjs");
/* harmony import */ var _validation_validate__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./validation/validate */ "../node_modules/graphql/validation/validate.mjs");
/* harmony import */ var _execution_execute__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./execution/execute */ "../node_modules/graphql/execution/execute.mjs");
(function () {
  var enterModule = require('react-hot-loader').enterModule;

  enterModule && enterModule(module);
})();

It should be __webpack__require__. The behaviour is intended in Webpack. I believe the new rule is that no require should never be injected in a .mjs file.

Environment

React Hot Loader version: 4.3.12

Run these commands in the project folder and fill in their results:

  1. node -v: v8.11.3
  2. npm -v: 5.6.0

Then, specify:

  1. Operating system: Mac OS 10.14.1
  2. Browser and version: Firefox 63.0.1, Safari 12.0.1, Google Chrome 70.0.3538.77

Reproducible Demo

https://github.com/QuentinRoy/react-hot-loader-mjs-crash

@theKashey
Copy link
Collaborator

It sounds like

if import is found in the file, then use import to get react-hot-loader, else use require.

Would affect both babel and webpack logic.

@theKashey theKashey self-assigned this Nov 14, 2018
@theKashey theKashey added this to the v5 milestone Dec 21, 2018
@theKashey theKashey added the bug label Dec 21, 2018
@jimmyn
Copy link

jimmyn commented Jan 21, 2019

I'm facing the same issue, is there any workaround?

@theKashey
Copy link
Collaborator

probably it's time to use a global variable :)

@PutziSan
Copy link

PutziSan commented Feb 1, 2019

I'm sorry, but I don't understand what safetyNet controls exactly. Do I have to use safetyNet: true to make it work for mjs files? And what else does that do?

@theKashey
Copy link
Collaborator

theKashey commented Feb 1, 2019

const plugins = [
    ["react-hot-loader/babel", {safetyNet: false}]
  ];

The new (4.6.5) version is require friendly(initial issue), while disabling safety net makes it module friendly (strange, but you cant module.hot.accept in mjs)

I've just tested your demo - one change to babelrc and it works!

@chardskarth
Copy link

const plugins = [
    ["react-hot-loader/babel", {safetyNet: false}]
  ];

The new (4.6.5) version is require friendly(initial issue), while disabling safety net makes it module friendly (strange, but you cant module.hot.accept in mjs)

I've just tested your demo - one change to babelrc and it works!

This also solved my issue. My error showed "module is not defined". Thanks a bunch @theKashey

victorlin added a commit to nextstrain/auspice that referenced this issue Oct 4, 2022
After upgrading webpack and plugins/loaders, there was an error from
react-hot-loader:

    ReferenceError: module is not defined

A known workaround is to enable safetyNet [1] so the code referencing
`module` is not executed.

I'm not sure why this was not shown before the webpack upgrades, but
don't feel like digging deeper to find out.

[1]: gaearon/react-hot-loader#1102 (comment)
victorlin added a commit to nextstrain/auspice that referenced this issue Oct 10, 2022
After upgrading webpack and plugins/loaders, there was an error from
react-hot-loader:

    ReferenceError: module is not defined

A known workaround is to enable safetyNet [1] so the code referencing
`module` is not executed.

I'm not sure why this was not shown before the webpack upgrades, but
don't feel like digging deeper to find out.

[1]: gaearon/react-hot-loader#1102 (comment)
victorlin added a commit to nextstrain/auspice that referenced this issue Nov 22, 2022
After upgrading webpack and plugins/loaders, there was an error from
react-hot-loader:

    ReferenceError: module is not defined

A known workaround is to enable safetyNet [1] so the code referencing
`module` is not executed.

I'm not sure why this was not shown before the webpack upgrades, but
don't feel like digging deeper to find out.

[1]: gaearon/react-hot-loader#1102 (comment)
victorlin added a commit to nextstrain/auspice that referenced this issue Nov 22, 2022
After upgrading webpack and plugins/loaders, there was an error from
react-hot-loader:

    ReferenceError: module is not defined

A known workaround is to enable safetyNet [1] so the code referencing
`module` is not executed.

I'm not sure why this was not shown before the webpack upgrades, but
don't feel like digging deeper to find out.

[1]: gaearon/react-hot-loader#1102 (comment)
victorlin added a commit to nextstrain/auspice that referenced this issue Nov 22, 2022
After upgrading webpack and plugins/loaders, there was an error from
react-hot-loader:

    ReferenceError: module is not defined

A known workaround is to enable safetyNet [1] so the code referencing
`module` is not executed.

I'm not sure why this was not shown before the webpack upgrades, but
don't feel like digging deeper to find out.

[1]: gaearon/react-hot-loader#1102 (comment)
victorlin added a commit to nextstrain/auspice that referenced this issue Dec 3, 2022
After upgrading webpack and plugins/loaders, there was an error from
react-hot-loader:

    ReferenceError: module is not defined

A known workaround is to enable safetyNet [1] so the code referencing
`module` is not executed.

I'm not sure why this was not shown before the webpack upgrades, but
don't feel like digging deeper to find out.

[1]: gaearon/react-hot-loader#1102 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants