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

transform-runtime is not removing regenerator-runtime import added by preset-env #10759

Open
slavafomin opened this issue Nov 25, 2019 · 4 comments · May be fixed by #10768
Open

transform-runtime is not removing regenerator-runtime import added by preset-env #10759

slavafomin opened this issue Nov 25, 2019 · 4 comments · May be fixed by #10768

Comments

@slavafomin
Copy link

@slavafomin slavafomin commented Nov 25, 2019

Hello!

Thank you for this great tool!

Problem

However, I'm looking at situation when the transform-runtime plugin is not removing global regenerator-runtime/runtime import, which is introduced by the preset-env preset.

Use case

We were working on some libraries and were using preset-env to actually transpile the code to the required target execution environment as well as to add needed global core-js and regenerator-runtime imports (usage mode). We were also using transform-runtime to optimize babel helper imports.

And now we would like to migrate from using global regeneratorRuntime usages to the explicitly imported ones (pure), so we've enabled regenerator option of the transform-runtime.

More details

Source code

// src/main.js

export async function foo() {
  return await new Promise(resolve => setTimeout(resolve, 1000));
}

Babel config

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": "IE 11",
        "useBuiltIns": "usage",
        "corejs": 3,
        "modules": false,
        "debug": true
      }
    ]
  ],
  "plugins": [
    [
      "@babel/plugin-transform-runtime",
      {
        "helpers": true,
        "regenerator": true
      }
    ]
  ],
  "exclude": "node_modules/**"
}

Transpiled result

// dist/main.js

import "core-js/modules/es.object.to-string";
import "core-js/modules/es.promise";

import "regenerator-runtime/runtime";
import _regeneratorRuntime from "@babel/runtime/regenerator";

export function foo() {
  return _regeneratorRuntime.async(function foo$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          _context.next = 2;
          return _regeneratorRuntime.awrap(new Promise(function (resolve) {
            return setTimeout(resolve, 1000);
          }));

        case 2:
          return _context.abrupt("return", _context.sent);

        case 3:
        case "end":
          return _context.stop();
      }
    }
  });
}

As you can see, the generated file contains both pure and global imports for regenerator-runtime, however, only explicit (pure) import should be left:

import "regenerator-runtime/runtime";
import _regeneratorRuntime from "@babel/runtime/regenerator";

I believe, that transform-runtime should remove such global imports (isn't it it's purpose in the first place?), or at least there should be an option for the preset-env to skip adding the global import for regenerator-runtime (I couldn't find any).

  1. What do you think about this issue?

  2. Do you know of any workarounds I can use right now to actually remove such superfluous imports?

Thank you.

@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Nov 25, 2019

Hey @slavafomin! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@slavafomin

This comment has been minimized.

Copy link
Author

@slavafomin slavafomin commented Nov 25, 2019

The only way to work around this that I've found so far is to use babel-plugin-transform-remove-imports plugin:

[
  'babel-plugin-transform-remove-imports', {
    test: /^regenerator-runtime\/runtime/,
  },
],
@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Nov 26, 2019

Out of curiosity, why are you ok with core-js using global imports but not regenerator-runtime?

@slavafomin

This comment has been minimized.

Copy link
Author

@slavafomin slavafomin commented Nov 27, 2019

@nicolo-ribaudo I can't get into much details right now, but we are working in complex environment with a lot of library code, end-user applications and dedicated service for polyfills and we are gradually switching between different approaches and we don't want to introduce breaking changes for application authors, so we've decided to encapsulate the regenerator-runtime in our libraries for now, but leave the global core-js imports. In our next step we would get rid of global imports for core-js as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.