Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

regenerator-runtime/runtime after regenerator-runtime does not work #337

Closed
julien-f opened this issue Jan 9, 2018 · 6 comments
Closed
Assignees

Comments

@julien-f
Copy link

julien-f commented Jan 9, 2018

require('regenerator-runtime/runtime') does not work if regenerator-runtime has been previously required.

require('regenerator-runtime')

// regeneratorRuntime is undefined
console.log(global.regeneratorRuntime)

require('regenerator-runtime/runtime')

// regeneratorRuntime is still undefined
console.log(global.regeneratorRuntime)

Repro repo.

@julien-f
Copy link
Author

julien-f commented Jan 9, 2018

I think we should clear the require cache at the end of runtime-module.js:

delete require.cache[require('./path').path]

@julien-f julien-f changed the title Requiring regenerator-runtime does not work regenerator-runtime/runtime after regenerator-runtime does not work Jan 9, 2018
@benjamn
Copy link
Contributor

benjamn commented Jan 9, 2018

Why not just define it globally (or better, just locally) yourself?

Doing anything with require.cache would make this code Node-specific, so that's not really an option.

@julien-f
Copy link
Author

julien-f commented Jan 9, 2018

babel-runtime@6 use regenerator-runtime but @babel/preset-env@7 with the polyfill option is using regenerator-runtime/runtime.

So when I use a dependency using @babel/preset-env in my babel-runtime application, it fails with regeneratorRuntime is not defined.

@jquense
Copy link

jquense commented May 18, 2018

lost a few hours trying to figure out why the runtime was inexplicably disappearing in a webpack app. this seems like really weird behavio (i'm sure there is a reason). Why not have the module version not add a global and have a polyfill import that requires it and appends to the global object which is a lot more common of a pattern for these sorts of things..

@benjamn
Copy link
Contributor

benjamn commented Nov 16, 2018

Please note that #353 has landed as of regenerator-runtime@0.13.0. While this version is not yet used by @babel/runtime, the conversion to CommonJS is very likely to affect this issue (hopefully in positive ways!), so I would encourage you to review those changes if you have a minute.

@wmertens
Copy link

Aha! I'm using pnpm which uses a non-flat modules layout and the reason I'm getting a failing build is because I added regenerator-runtime with the wrong version to the top of the modules tree :-(

@benjamn benjamn self-assigned this Mar 7, 2019
benjamn added a commit that referenced this issue Mar 7, 2019
In #353, I simplified the implementation of regenerator-runtime by
converting runtime.js to a normal CommonJS module. No more global object
detection, and no more sneaky capture/deletion of regeneratorRuntime.

Unfortunately, folks who use only @babel/polyfill and not
@babel/transform-runtime may have existing code that relies on a global
regeneratorRuntime variable. That code will eventually disappear
(:crossed-fingers:), but in the meantime we can continue supporting it by
simply ensuring that regeneratorRuntime is defined globally as a
side-effect of importing regenerator-runtime.

This turns out to be much easier than detecting the global object, as long
as runtime.js runs as a non-strict CommonJS module. See my code comments
for another fallback solution that should cover all remaining cases.

Background discussion:
babel/babel#7646 (comment)

Fixes #363.
Fixes #337.
Fixes #367.
benjamn added a commit that referenced this issue Mar 7, 2019
* Define regeneratorRuntime globally as a side-effect, again.

In #353, I simplified the implementation of regenerator-runtime by
converting runtime.js to a normal CommonJS module. No more global object
detection, and no more sneaky capture/deletion of regeneratorRuntime.

Unfortunately, folks who use only @babel/polyfill and not
@babel/transform-runtime may have existing code that relies on a global
regeneratorRuntime variable. That code will eventually disappear
(:crossed_fingers:), but in the meantime we can continue supporting it by
simply ensuring that regeneratorRuntime is defined globally as a
side-effect of importing regenerator-runtime.

This turns out to be much easier than detecting the global object, as long
as runtime.js runs as a non-strict CommonJS module. See my code comments
for another fallback solution that should cover all remaining cases.

Background discussion:
babel/babel#7646 (comment)

Fixes #363.
Fixes #337.
Fixes #367.

* Update regenerator/package-lock.json.

* Fix Travis CI tests by running `npm i` instead of `npm ci`.

* Prevent Travis from running `npm ci` by default.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants