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

Commit

Permalink
Define regeneratorRuntime globally as a side-effect, again.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
benjamn committed Mar 7, 2019
1 parent 70cde2f commit 2fcb4e4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/regenerator-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"generator",
"async"
],
"sideEffects": true,
"repository": {
"type": "git",
"url": "https://github.com/facebook/regenerator/tree/master/packages/regenerator-runtime"
Expand Down
17 changes: 16 additions & 1 deletion packages/regenerator-runtime/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

var regeneratorRuntime = (function (exports) {
var runtime = (function (exports) {
"use strict";

var Op = Object.prototype;
Expand Down Expand Up @@ -709,3 +709,18 @@ var regeneratorRuntime = (function (exports) {
// the regeneratorRuntime variable at the top of this file.
typeof module === "object" ? module.exports : {}
));

try {
regeneratorRuntime = runtime;
} catch (accidentalStrictMode) {
// This module should not be running in strict mode, so the above
// assignment should always work unless something is misconfigured. Just
// in case runtime.js accidentally runs in strict mode, we can escape
// strict mode using a global Function call. This could conceivably fail
// if a Content Security Policy forbids using Function, but in that case
// the proper solution is to fix the accidental strict mode problem. If
// you've misconfigured your bundler to force strict mode and applied a
// CSP to forbid Function, and you're not willing to fix either of those
// problems, please detail your unique predicament in a GitHub issue.
Function("r", "regeneratorRuntime = r")(runtime);
}
2 changes: 1 addition & 1 deletion test/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* LICENSE file in the root directory of this source tree.
*/

global.regeneratorRuntime = require("regenerator-runtime");
require("regenerator-runtime");

0 comments on commit 2fcb4e4

Please sign in to comment.