Skip to content

Commit

Permalink
[Rebased] Remove automatic core-js polyfills (vercel#91)
Browse files Browse the repository at this point in the history
Rebase of #68, which was merged into a now-closed PR.

Closes #65.

When configured to use preset-env, this stops turbopack from inserting
dependencies on core-js. It's:

* Buggy/error-prone, at least through swc preset-env: 
  * vercel/the-three-body#65
* setImmediate "polyfills" always inserted for this nonstandard feature
when
    React (scheduler, iirc) does feature detection of it
* Big. Updating the snapshot alone for a test that only used [].includes
  resulted in a diff with nearly 4700 line deletions
* Not done by Next.js for modern targets in the past couple of years:
https://twitter.com/timneutkens/status/1234548900272517120

Test Plan: Added [].includes to preset_env snapshot test and verified no
references to core-js are inserted.
  • Loading branch information
wbinnssmith authored Oct 18, 2022
1 parent 80b17f1 commit 793262f
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 96 deletions.
3 changes: 1 addition & 2 deletions crates/next-dev/benches/bundlers/turbopack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ impl Bundler for Turbopack {
NpmPackage::new("react-refresh", "^0.12.0"),
NpmPackage::new("next", "^12.3.1"),
NpmPackage::new("@next/react-refresh-utils", "^12.3.1"),
// Dependencies on these are inserted by swc's preset_env
// Dependency on this is inserted by swc's preset_env
NpmPackage::new("@swc/helpers", "^0.4.11"),
NpmPackage::new("core-js", "^3.25.3"),
],
)
.context("failed to install from npm")?;
Expand Down
5 changes: 3 additions & 2 deletions crates/turbopack-ecmascript/src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use swc_core::{
common::{chain, util::take::Take, FileName, Mark, SourceMap},
ecma::{
ast::{Module, ModuleItem, Program},
preset_env::{self, Mode, Targets},
preset_env::{self, Targets},
transforms::{
base::{feature::FeatureFlag, helpers::inject_helpers, Assumptions},
react::react,
Expand Down Expand Up @@ -89,9 +89,10 @@ impl EcmascriptInputTransform {
}
EcmascriptInputTransform::PresetEnv(env) => {
let versions = env.runtime_versions().await?;
println!("versions {:?}", versions);
let config = swc_core::ecma::preset_env::Config {
targets: Some(Targets::Versions(*versions)),
mode: Some(Mode::Usage),
mode: None, // Don't insert core-js polyfills
..Default::default()
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Foo {}

console.log(Foo);
console.log(Foo, [].includes("foo"));

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": 3,
"sections": [
{"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["/[project]/node_modules/.pnpm/@[email protected]/node_modules/@swc/helpers/src/_class_call_check.mjs"],"sourcesContent":["export default function _classCallCheck(instance, Constructor) {\n if (!(instance instanceof Constructor)) {\n throw new TypeError(\"Cannot call a class as a function\");\n }\n}"],"names":[],"mappings":"AAAA;;;AAAe,SAAS,gBAAgB,QAAQ,EAAE,WAAW,EAAE;IAC7D,IAAI,CAAC,CAAC,oBAAoB,WAAW,GAAG;QACtC,MAAM,IAAI,UAAU,qCAAqC;IAC3D,CAAC;AACH"}},
{"offset": {"line": 12, "column": 0}, "map": {"version": 3, "names": [], "sources": [], "mappings": "A"}}]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": 3,
"sections": [
{"offset": {"line": 2, "column": 0}, "map": {"version":3,"sources":["/[project]/node_modules/.pnpm/@[email protected]/node_modules/@swc/helpers/src/_class_call_check.mjs"],"sourcesContent":["export default function _classCallCheck(instance, Constructor) {\n if (!(instance instanceof Constructor)) {\n throw new TypeError(\"Cannot call a class as a function\");\n }\n}"],"names":[],"mappings":"AAAA;;;AAAe,SAAS,gBAAgB,QAAQ,EAAE,WAAW,EAAE;IAC7D,IAAI,CAAC,CAAC,oBAAoB,WAAW,GAAG;QACtC,MAAM,IAAI,UAAU,qCAAqC;IAC3D,CAAC;AACH"}},
{"offset": {"line": 10, "column": 0}, "map": {"version": 3, "names": [], "sources": [], "mappings": "A"}}]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": 3,
"sections": [
{"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["/[project]/crates/turbopack/tests/snapshot/integration/preset_env/input/index.js"],"sourcesContent":["class Foo {}\n\nconsole.log(Foo);\n"],"names":[],"mappings":"AAAA;;;AAAA,IAAA,AAAM,MAAN,SAAM;;2IAAA;;AAEN,QAAQ,GAAG,CAAC"}},
{"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["/[project]/crates/turbopack/tests/snapshot/integration/preset_env/input/index.js"],"sourcesContent":["class Foo {}\n\nconsole.log(Foo, [].includes(\"foo\"));\n"],"names":[],"mappings":"AAAA;;;AAAA,IAAA,AAAM,MAAN,SAAM;;yMAAA;;AAEN,QAAQ,GAAG,CAAC,KAAK,EAAE,CAAC,QAAQ,CAAC"}},
{"offset": {"line": 12, "column": 0}, "map": {"version": 3, "names": [], "sources": [], "mappings": "A"}}]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": 3,
"sections": [
{"offset": {"line": 2, "column": 0}, "map": {"version":3,"sources":["/[project]/crates/turbopack/tests/snapshot/integration/preset_env/input/index.js"],"sourcesContent":["class Foo {}\n\nconsole.log(Foo);\n"],"names":[],"mappings":"AAAA;;;AAAA,IAAA,AAAM,MAAN,SAAM;;2IAAA;;AAEN,QAAQ,GAAG,CAAC"}},
{"offset": {"line": 2, "column": 0}, "map": {"version":3,"sources":["/[project]/crates/turbopack/tests/snapshot/integration/preset_env/input/index.js"],"sourcesContent":["class Foo {}\n\nconsole.log(Foo, [].includes(\"foo\"));\n"],"names":[],"mappings":"AAAA;;;AAAA,IAAA,AAAM,MAAN,SAAM;;yMAAA;;AAEN,QAAQ,GAAG,CAAC,KAAK,EAAE,CAAC,QAAQ,CAAC"}},
{"offset": {"line": 10, "column": 0}, "map": {"version": 3, "names": [], "sources": [], "mappings": "A"}}]
}

This file was deleted.

This file was deleted.

This file was deleted.

68 changes: 15 additions & 53 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 793262f

Please sign in to comment.