swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
30.93k stars 1.21k forks source link

Consider Ponyfill with `core-js-pure` instead of `core-js` to avoid pollute global environment. #5584

Open morlay opened 2 years ago

morlay commented 2 years ago

Describe the feature

Array.from(new Set([1, 2, 3, 2, 1]));          // => [1, 2, 3]
[1, 2, 3, 4, 5].group(it => it % 2);           // => { 1: [1, 3, 5], 0: [2, 4] }
Promise.resolve(42).then(x => console.log(x)); // => 42
structuredClone(new Set([1, 2, 3]));           // => new Set([1, 2, 3])
queueMicrotask(() => console.log('called as microtask'));

Could be

// core-js-prue is the version without global namespace pollution
// for easier to transform, we could has some rule of the importers. just like babel did
//  _<Class>$<static_method>
import _Array$from from 'core-js-pure/actual/array/from';
// _<instance_method><class>(instance, ...instance.method_args)
// need to wrap instance as first arg.
import _groupArray from 'core-js-pure/actual/array/group';
// _<Class>
import _Set from 'core-js-pure/actual/set';
import _Promise from 'core-js-pure/actual/promise';
// _<global_method>
import _structuredClone from 'core-js-pure/actual/structured-clone';
import _queueMicrotask from 'core-js-pure/actual/queue-microtask';

_Array$from(new Set([1, 2, 3, 2, 1]));                // => [1, 2, 3]
_groupArray([1, 2, 3, 4, 5], it => it % 2);          // => { 1: [1, 3, 5], 0: [2, 4] }
_Promise.resolve(42).then(x => console.log(x)); // => 42
_structuredClone(new Set([1, 2, 3]));           // => new Set([1, 2, 3])
_queueMicrotask(() => console.log('called as microtask'));

Babel plugin or link to the feature description

https://babeljs.io/docs/en/babel-plugin-transform-runtime#core-js-aliasing

Additional context

https://ponyfoo.com/articles/polyfills-or-ponyfills https://github.com/zloirock/core-js/blob/master/README.md

it could be enable when corejs@3 and usage

kdy1 commented 2 years ago

Your link to babel is something completely different, and I think this is a huge security risk

kdy1 commented 2 years ago

Sorry I misread it

morlay commented 2 years ago

Your link to babel is something completely different, and I think this is a huge security risk

@babel/runtime-corejs3/{core-js,core-js-stable} just a wrap of core-js-pure

image
zloirock commented 2 years ago

Note that the pure version / runtime transform has many limitations and many features are not available. However, it's the best option, for example, for libraries that should not pollute the global namespace.

babel-plugin-polyfill-corejs3 injects core-js-pure directly instead of @babel/runtime.

dac09 commented 1 year ago

Hey wanted to mention that this would be an important feature to use swc to build libraries.

It's sort of fine to pollute the global scope when you're using SWC to build your app, but when building a library that someone uses in their app, it feels like a bad idea.

(or am I being too cautious here?)

kdy1 commented 1 year ago

Don't leave comments like +1 It does not help at all