From a08f758c0450aa65a0aff4c243d39fe2e2d5ecfc Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Sat, 4 Mar 2023 07:51:31 +0100 Subject: [PATCH] inject swc helpers (vercel/turbo#4060) ### Description This fixed the `_extends` is not defined issue. --- .../basic/swc-helpers/input/pages/index.js | 25 +++++++++++++++++++ crates/turbopack-ecmascript/src/parse.rs | 4 +++ ...ransforms_preset_env_input_index_097653.js | 1 + ...forms_preset_env_input_index_097653.js.map | 4 +-- 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 crates/next-dev-tests/tests/integration/next/basic/swc-helpers/input/pages/index.js diff --git a/crates/next-dev-tests/tests/integration/next/basic/swc-helpers/input/pages/index.js b/crates/next-dev-tests/tests/integration/next/basic/swc-helpers/input/pages/index.js new file mode 100644 index 0000000000000..54000fcc5abe5 --- /dev/null +++ b/crates/next-dev-tests/tests/integration/next/basic/swc-helpers/input/pages/index.js @@ -0,0 +1,25 @@ +import { useEffect } from "react"; + +export default function Page() { + useEffect(() => { + // Only run on client + import("@turbo/pack-test-harness").then(runTests); + }); + + const props1 = { className: "foo" }; + const props2 = { style: { color: "red" } }; + // this will require the `_extends` helper on SSR + return ( +
+ Hello World +
+ ); +} + +function runTests() { + it("should render the #test element in red and with class foo", () => { + const el = document.getElementById("test"); + expect(el.style.color).toBe("red"); + expect(el.className).toBe("foo"); + }); +} diff --git a/crates/turbopack-ecmascript/src/parse.rs b/crates/turbopack-ecmascript/src/parse.rs index 57b3daf06af96..84c1aa9170618 100644 --- a/crates/turbopack-ecmascript/src/parse.rs +++ b/crates/turbopack-ecmascript/src/parse.rs @@ -289,6 +289,10 @@ async fn parse_content( transform.apply(&mut parsed_program, &context).await?; } + parsed_program.visit_mut_with( + &mut swc_core::ecma::transforms::base::helpers::inject_helpers(unresolved_mark), + ); + let eval_context = EvalContext::new(&parsed_program, unresolved_mark); Ok::(ParseResult::Ok { diff --git a/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_097653.js b/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_097653.js index 77d9a49d06058..84b577e0fa121 100644 --- a/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_097653.js +++ b/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_097653.js @@ -5,6 +5,7 @@ var __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$node_modules$2f40$swc$2f$helpers$2f$src$2f$_class_call_check$2e$mjs__$28$ecmascript$29__ = __turbopack_import__("[project]/crates/turbopack-tests/tests/node_modules/@swc/helpers/src/_class_call_check.mjs (ecmascript)"); "__TURBOPACK__ecmascript__hoisting__location__"; ; +; var Foo = function Foo() { "use strict"; __TURBOPACK__imported__module__$5b$project$5d2f$crates$2f$turbopack$2d$tests$2f$tests$2f$node_modules$2f40$swc$2f$helpers$2f$src$2f$_class_call_check$2e$mjs__$28$ecmascript$29__["default"](this, Foo); diff --git a/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_097653.js.map b/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_097653.js.map index 505d332bd4d26..1e6e076dfc14f 100644 --- a/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_097653.js.map +++ b/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/79fb1_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_097653.js.map @@ -1,6 +1,6 @@ { "version": 3, "sections": [ - {"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/input/index.js"],"sourcesContent":["class Foo {}\n\nconsole.log(Foo, [].includes(\"foo\"));\n"],"names":[],"mappings":";;;AAAA,IAAA,AAAM,MAAN,SAAM;;uMAAA;;AAEN,QAAQ,GAAG,CAAC,KAAK,EAAE,CAAC,QAAQ,CAAC"}}, - {"offset": {"line": 12, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] + {"offset": {"line": 4, "column": 0}, "map": {"version":3,"sources":["/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/input/index.js"],"sourcesContent":["class Foo {}\n\nconsole.log(Foo, [].includes(\"foo\"));\n"],"names":[],"mappings":";;;;AAAA,IAAA,AAAM,MAAN,SAAM;;uMAAA;;AAEN,QAAQ,GAAG,CAAC,KAAK,EAAE,CAAC,QAAQ,CAAC"}}, + {"offset": {"line": 13, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":"A"}}] } \ No newline at end of file