swc icon indicating copy to clipboard operation
swc copied to clipboard

`env.mode: "entry"` removes core-js import even though it's

Open mischnic opened this issue 1 year ago • 5 comments

Describe the bug

Imports of specific core-js parts are removed with a mode: usage.

Babel doesn't remove them: https://babeljs.io/repl#?browsers=Chrome%2040&build=&builtIns=entry&corejs=3.21&spec=false&loose=false&code_lz=JYWwDg9gTgLgBAIgMbQKYFoBWBnA9CCAEwFcAbVPCgOmximADsBzKgQxnVdJlSgfeAA3VAgDcAKHFA&debug=false&forceAllTransforms=false&shippedProposals=true&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=unambiguous&lineWrap=false&presets=env&prettier=false&targets=&version=7.18.7&externalPlugins=&assumptions=%7B%7D

Input code

import "core-js/modules/es.string.at-alternative";

Config

{
  "env": {
    "mode": "entry",
    "targets": "Chrome 40"
  },
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es5",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": true
}

Playground link

https://play.swc.rs/?version=1.2.210&code=H4sIAAAAAAAAA8vMLcgvKlFQSs4vStXNKtbPzU8pzUkt1k8t1isuKcrMS9dLLNFNzClJLcpLLMksS1Wy5gIApRxsVzMAAAA%3D&config=H4sIAAAAAAAAA0WOvQ7CMAyEXwV57sAADF2ZeYgouCUof7JdRBTl3RujRmz23Xenq4DxA3OFkJ4Ic%2F%2BECkwghlYU7sr9RSng6XKGNsGbrcLZECPpxSWK%2BWrQBsOWXBZQrEuL8YxtVCnC1%2B75lBgPd4LgoluKNtkUMiHz3zJx9YNsvahP3FSoICX%2FxvJNR42OI%2Bf4cYBCG7YdCn3HAOIAAAA%3D

Expected behavior

Don't remove the import

Actual behavior

Import is removed

Version

1.2.210

Additional context

No response

mischnic avatar Jul 06 '22 20:07 mischnic

This is expected. You need to specify coreJs version -- String#at is introduced in a later version of core-js, and swc default coreJs version to 3.0, thus remove the import.

If you use same config for babel,

{
    presets: [["@babel/preset-env", { useBuiltIns: "entry", corejs: "3" }]];
}

same output would be generated

Austaras avatar Jul 08 '22 06:07 Austaras

@Austaras But I have the latest core-js version installed in my repo. As I understand it babel/swc can import their own core-js version but I want to import only what I need myself. Why would importing work differently for the latest version of core-js than for all other packages? Is this really intended?

danieltroger avatar Jul 08 '22 07:07 danieltroger

So should Parcel be detecting the version of core-js that's installed, and then pass that version (e.g. "3.23.3") to swc as preset_env_config.core_js?

(currently we use this preset env swc config: https://github.com/parcel-bundler/parcel/blob/99cf50510995112722f76f115dc7f2fd21587f34/packages/transformers/js/core/src/lib.rs#L278-L295)

mischnic avatar Jul 08 '22 07:07 mischnic

So should Parcel be detecting the version of core-js that's installed, and then pass that version (e.g. "3.23.3") to swc as preset_env_config.core_js?

(currently we use this preset env swc config: https://github.com/parcel-bundler/parcel/blob/99cf50510995112722f76f115dc7f2fd21587f34/packages/transformers/js/core/src/lib.rs#L278-L295)

That sounds good as parcel is the bundler. Although 3.22 would be enough. It's a bit wired to build this feature into swc.

Austaras avatar Jul 08 '22 08:07 Austaras

@Austaras But I have the latest core-js version installed in my repo. As I understand it babel/swc can import their own core-js version but I want to import only what I need myself. Why would importing work differently for the latest version of core-js than for all other packages? Is this really intended?

Babel didn't do auto core js version detection.

Austaras avatar Jul 08 '22 08:07 Austaras