From 79bbf4086c56caf210ab23bdf2b17440ce12ed27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 26 Aug 2023 09:05:26 +0900 Subject: [PATCH 01/10] Rename --- crates/swc/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index 304edb49e84f..a361dccca412 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -997,7 +997,7 @@ impl Compiler { None }; - self.process_js_inner(handler, orig.as_ref(), config) + self.apply_transforms(handler, orig.as_ref(), config) }) } @@ -1208,7 +1208,7 @@ impl Compiler { } #[tracing::instrument(level = "info", skip_all)] - fn process_js_inner( + fn apply_transforms( &self, handler: &Handler, orig: Option<&sourcemap::SourceMap>, From 0ddecc19c6b8381fdf8c4810909556ce4711ab95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 26 Aug 2023 09:10:12 +0900 Subject: [PATCH 02/10] instrument --- crates/swc/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index a361dccca412..b2b0e1e8cde7 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -1207,7 +1207,7 @@ impl Compiler { ) } - #[tracing::instrument(level = "info", skip_all)] + #[tracing::instrument(name = "Swc::apply_transforms", level = "info", skip_all)] fn apply_transforms( &self, handler: &Handler, From 9a4ea7edb6dcce58887e30767851819e27954eb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 26 Aug 2023 09:10:53 +0900 Subject: [PATCH 03/10] instrument --- crates/swc/src/lib.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index b2b0e1e8cde7..c3a7968c0ac3 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -733,7 +733,7 @@ impl Compiler { } } - #[tracing::instrument(level = "info", skip_all)] + #[tracing::instrument(skip_all)] pub fn read_config(&self, opts: &Options, name: &FileName) -> Result, Error> { static CUR_DIR: Lazy = Lazy::new(|| { if cfg!(target_arch = "wasm32") { @@ -845,7 +845,7 @@ impl Compiler { /// This method handles merging of config. /// /// This method does **not** parse module. - #[tracing::instrument(level = "info", skip_all)] + #[tracing::instrument(skip_all)] pub fn parse_js_as_input<'a, P>( &'a self, fm: Lrc, @@ -908,7 +908,7 @@ impl Compiler { }) } - #[tracing::instrument(level = "info", skip_all)] + #[tracing::instrument(skip_all)] pub fn transform( &self, handler: &Handler, @@ -936,7 +936,7 @@ impl Compiler { /// /// This means, you can use `noop_visit_type`, `noop_fold_type` and /// `noop_visit_mut_type` in your visitor to reduce the binary size. - #[tracing::instrument(level = "info", skip_all)] + #[tracing::instrument(skip_all)] pub fn process_js_with_custom_pass( &self, fm: Arc, @@ -1001,7 +1001,7 @@ impl Compiler { }) } - #[tracing::instrument(level = "info", skip(self, handler, opts))] + #[tracing::instrument(skip(self, handler, opts))] pub fn process_js_file( &self, fm: Arc, @@ -1019,7 +1019,7 @@ impl Compiler { ) } - #[tracing::instrument(level = "info", skip_all)] + #[tracing::instrument(skip_all)] pub fn minify( &self, fm: Arc, @@ -1186,7 +1186,7 @@ impl Compiler { /// You can use custom pass with this method. /// /// There exists a [PassBuilder] to help building custom passes. - #[tracing::instrument(level = "info", skip_all)] + #[tracing::instrument(skip_all)] pub fn process_js( &self, handler: &Handler, @@ -1207,7 +1207,7 @@ impl Compiler { ) } - #[tracing::instrument(name = "Swc::apply_transforms", level = "info", skip_all)] + #[tracing::instrument(name = "swc::Compiler::apply_transforms", skip_all)] fn apply_transforms( &self, handler: &Handler, @@ -1281,7 +1281,7 @@ fn find_swcrc(path: &Path, root: &Path, root_mode: RootMode) -> Option None } -#[tracing::instrument(level = "info", skip_all)] +#[tracing::instrument(skip_all)] fn load_swcrc(path: &Path) -> Result { let content = read_to_string(path).context("failed to read config (.swcrc) file")?; From f023bb4cba833d959dcfdc1ff37ac439c6b367d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 26 Aug 2023 09:16:53 +0900 Subject: [PATCH 04/10] add option --- crates/swc/src/config/mod.rs | 164 ++++++++++++++++++----------------- 1 file changed, 86 insertions(+), 78 deletions(-) diff --git a/crates/swc/src/config/mod.rs b/crates/swc/src/config/mod.rs index d1e034bb4711..23b4b84c31a3 100644 --- a/crates/swc/src/config/mod.rs +++ b/crates/swc/src/config/mod.rs @@ -324,7 +324,7 @@ impl Options { config: Option, comments: Option<&'a SingleThreadedComments>, custom_before_pass: impl FnOnce(&Program) -> P, - ) -> Result, Error> + ) -> Result>, Error> where P: 'a + swc_ecma_visit::Fold, { @@ -355,6 +355,7 @@ impl Options { experimental, lints, preserve_all_comments, + experimental_disable_builtin_transforms, .. } = cfg.jsc; let loose = loose.into_bool(); @@ -759,88 +760,92 @@ impl Options { noop() }; - let pass = chain!( - lint_to_fold(swc_ecma_lints::rules::all(LintParams { - program: &program, - lint_config: &lints, - top_level_ctxt, - unresolved_ctxt, - es_version, - source_map: cm.clone(), - })), - // Decorators may use type information - Optional::new( - match transform.decorator_version.unwrap_or_default() { - DecoratorVersion::V202112 => { - Either::Left(decorators(decorators::Config { - legacy: transform.legacy_decorator.into_bool(), - emit_metadata: transform.decorator_metadata.into_bool(), - use_define_for_class_fields: !assumptions.set_public_class_fields, - })) - } - DecoratorVersion::V202203 => { - Either::Right( + let pass: Box = if experimental_disable_builtin_transforms.into_bool() { + Box::new(plugin_transforms) + } else { + Box::new(chain!( + lint_to_fold(swc_ecma_lints::rules::all(LintParams { + program: &program, + lint_config: &lints, + top_level_ctxt, + unresolved_ctxt, + es_version, + source_map: cm.clone(), + })), + // Decorators may use type information + Optional::new( + match transform.decorator_version.unwrap_or_default() { + DecoratorVersion::V202112 => { + Either::Left(decorators(decorators::Config { + legacy: transform.legacy_decorator.into_bool(), + emit_metadata: transform.decorator_metadata.into_bool(), + use_define_for_class_fields: !assumptions.set_public_class_fields, + })) + } + DecoratorVersion::V202203 => { + Either::Right( swc_ecma_transforms::proposals::decorator_2022_03::decorator_2022_03(), ) - } - }, - syntax.decorators() - ), - // The transform strips import assertions, so it's only enabled if - // keep_import_assertions is false. - Optional::new(import_assertions(), !keep_import_assertions), - Optional::new( - typescript::strip_with_jsx::>( - cm.clone(), - typescript::Config { - pragma: Some( - transform - .react - .pragma - .clone() - .unwrap_or_else(default_pragma) - ), - pragma_frag: Some( - transform - .react - .pragma_frag - .clone() - .unwrap_or_else(default_pragma_frag) - ), - ts_enum_config: TsEnumConfig { - treat_const_enum_as_enum: transform - .treat_const_enum_as_enum - .into_bool(), - ts_enum_is_readonly: assumptions.ts_enum_is_readonly, - }, - import_export_assign_config, - ..Default::default() + } }, - comments.map(|v| v as _), - top_level_mark + syntax.decorators() ), - syntax.typescript() - ), - plugin_transforms, - custom_before_pass(&program), - // handle jsx - Optional::new( - react::react::<&dyn Comments>( - cm.clone(), - comments.map(|v| v as _), - transform.react, - top_level_mark, - unresolved_mark + // The transform strips import assertions, so it's only enabled if + // keep_import_assertions is false. + Optional::new(import_assertions(), !keep_import_assertions), + Optional::new( + typescript::strip_with_jsx::>( + cm.clone(), + typescript::Config { + pragma: Some( + transform + .react + .pragma + .clone() + .unwrap_or_else(default_pragma) + ), + pragma_frag: Some( + transform + .react + .pragma_frag + .clone() + .unwrap_or_else(default_pragma_frag) + ), + ts_enum_config: TsEnumConfig { + treat_const_enum_as_enum: transform + .treat_const_enum_as_enum + .into_bool(), + ts_enum_is_readonly: assumptions.ts_enum_is_readonly, + }, + import_export_assign_config, + ..Default::default() + }, + comments.map(|v| v as _), + top_level_mark + ), + syntax.typescript() ), - syntax.jsx() - ), - pass, - Optional::new(jest::jest(), transform.hidden.jest.into_bool()), - Optional::new( - dropped_comments_preserver(comments.cloned()), - preserve_all_comments - ), - ); + plugin_transforms, + custom_before_pass(&program), + // handle jsx + Optional::new( + react::react::<&dyn Comments>( + cm.clone(), + comments.map(|v| v as _), + transform.react, + top_level_mark, + unresolved_mark + ), + syntax.jsx() + ), + pass, + Optional::new(jest::jest(), transform.hidden.jest.into_bool()), + Optional::new( + dropped_comments_preserver(comments.cloned()), + preserve_all_comments + ), + )) + }; Ok(BuiltInput { program, @@ -1410,6 +1415,9 @@ pub struct JscConfig { #[serde(default)] pub output: JscOutputConfig, + + #[serde(default)] + pub experimental_disable_builtin_transforms: BoolConfig, } #[derive(Debug, Default, Clone, Serialize, Deserialize, Merge)] From 57bb1e7e8f642d1ad46ea8ef8c6574fb640e9433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 26 Aug 2023 09:23:43 +0900 Subject: [PATCH 05/10] Add a test --- node-swc/__tests__/transform/experimental.mjs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 node-swc/__tests__/transform/experimental.mjs diff --git a/node-swc/__tests__/transform/experimental.mjs b/node-swc/__tests__/transform/experimental.mjs new file mode 100644 index 000000000000..ad0f40f3b691 --- /dev/null +++ b/node-swc/__tests__/transform/experimental.mjs @@ -0,0 +1,31 @@ +import swc from "../../.."; + +describe("when jsc.experimentalDisableBuiltinTransforms is true", () => { + it("should preserve TypeScript", async () => { + const { code } = await swc.transform( + ` + const hello: Type = () => { + console.log('hello'); + }; + `, + { + jsc: { + parser: { + syntax: "typescript", + }, + experimental: { + disableBuiltinTransforms: true, + }, + }, + minify: false, + } + ); + + expect(code).toMatchInlineSnapshot(` + "const hello: Type = ()=>{ + console.log(\\"hello\\"); + }; + " + `); + }); +}); From f66437c57b2b5c290b3daf3e0563f53739118e05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 26 Aug 2023 09:23:50 +0900 Subject: [PATCH 06/10] Move option --- crates/swc/src/config/mod.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/swc/src/config/mod.rs b/crates/swc/src/config/mod.rs index 23b4b84c31a3..2d582b9b52d1 100644 --- a/crates/swc/src/config/mod.rs +++ b/crates/swc/src/config/mod.rs @@ -355,7 +355,6 @@ impl Options { experimental, lints, preserve_all_comments, - experimental_disable_builtin_transforms, .. } = cfg.jsc; let loose = loose.into_bool(); @@ -760,7 +759,7 @@ impl Options { noop() }; - let pass: Box = if experimental_disable_builtin_transforms.into_bool() { + let pass: Box = if experimental.disable_builtin_transforms.into_bool() { Box::new(plugin_transforms) } else { Box::new(chain!( @@ -1415,9 +1414,6 @@ pub struct JscConfig { #[serde(default)] pub output: JscOutputConfig, - - #[serde(default)] - pub experimental_disable_builtin_transforms: BoolConfig, } #[derive(Debug, Default, Clone, Serialize, Deserialize, Merge)] @@ -1462,6 +1458,9 @@ pub struct JscExperimental { /// and will not be considered as breaking changes. #[serde(default)] pub cache_root: Option, + + #[serde(default)] + pub disable_builtin_transforms: BoolConfig, } #[derive(Debug, Clone, Copy, Serialize, Deserialize)] From 20e05dd533539483a7e8cd62ec9ae3e03bc4f67d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 26 Aug 2023 09:31:32 +0900 Subject: [PATCH 07/10] cmt --- packages/types/index.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/types/index.ts b/packages/types/index.ts index 137b9c97b7c0..a5910fdf271f 100644 --- a/packages/types/index.ts +++ b/packages/types/index.ts @@ -591,6 +591,9 @@ export interface JscConfig { */ keepClassNames?: boolean; + /** + * This is experimental, and can be removed without a major version bump. + */ experimental?: { optimizeHygiene?: boolean; keepImportAssertions?: boolean; @@ -608,6 +611,11 @@ export interface JscConfig { * Second parameter of tuple is JSON based configuration for the plugin. */ plugins?: Array<[string, Record]>; + + /** + * Disable builtin transforms. If enabled, only Wasm plugins are used. + */ + disableBuiltinTransforms?: boolean; }; baseUrl?: string; From 6fea213cb78f769a239956c7db72705aa76da101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 26 Aug 2023 09:34:10 +0900 Subject: [PATCH 08/10] type --- crates/swc/src/config/mod.rs | 7 +++++-- node-swc/__tests__/transform/experimental.mjs | 2 +- packages/types/index.ts | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/swc/src/config/mod.rs b/crates/swc/src/config/mod.rs index 2d582b9b52d1..6b86cab03ae6 100644 --- a/crates/swc/src/config/mod.rs +++ b/crates/swc/src/config/mod.rs @@ -759,7 +759,10 @@ impl Options { noop() }; - let pass: Box = if experimental.disable_builtin_transforms.into_bool() { + let pass: Box = if experimental + .disable_builtin_transforms_for_internal_testing + .into_bool() + { Box::new(plugin_transforms) } else { Box::new(chain!( @@ -1460,7 +1463,7 @@ pub struct JscExperimental { pub cache_root: Option, #[serde(default)] - pub disable_builtin_transforms: BoolConfig, + pub disable_builtin_transforms_for_internal_testing: BoolConfig, } #[derive(Debug, Clone, Copy, Serialize, Deserialize)] diff --git a/node-swc/__tests__/transform/experimental.mjs b/node-swc/__tests__/transform/experimental.mjs index ad0f40f3b691..aada902e32e2 100644 --- a/node-swc/__tests__/transform/experimental.mjs +++ b/node-swc/__tests__/transform/experimental.mjs @@ -14,7 +14,7 @@ describe("when jsc.experimentalDisableBuiltinTransforms is true", () => { syntax: "typescript", }, experimental: { - disableBuiltinTransforms: true, + disableBuiltinTransformsForInternalTesting: true, }, }, minify: false, diff --git a/packages/types/index.ts b/packages/types/index.ts index a5910fdf271f..59f2e6e04ff2 100644 --- a/packages/types/index.ts +++ b/packages/types/index.ts @@ -615,7 +615,7 @@ export interface JscConfig { /** * Disable builtin transforms. If enabled, only Wasm plugins are used. */ - disableBuiltinTransforms?: boolean; + disableBuiltinTransformsForInternalTesting?: boolean; }; baseUrl?: string; From 47b6bb2c3bd5bb388ce2d0fd29bf84cbcf5fb1b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 26 Aug 2023 09:34:23 +0900 Subject: [PATCH 09/10] types --- packages/types/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/types/package.json b/packages/types/package.json index 49d98f10a880..8131a218c843 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -1,6 +1,6 @@ { "name": "@swc/types", - "version": "0.1.3", + "version": "0.1.4", "description": "Typings for the swc project.", "sideEffects": false, "scripts": { From e61bd5accc0aea1bd385f6d00396fab88f6469c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 26 Aug 2023 09:35:39 +0900 Subject: [PATCH 10/10] types --- package.json | 2 +- yarn.lock | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index b7213245b0c3..e7bfacc4fd4a 100644 --- a/package.json +++ b/package.json @@ -178,6 +178,6 @@ ], "packageManager": "yarn@3.5.0", "dependencies": { - "@swc/types": "^0.1.3" + "@swc/types": "^0.1.4" } } diff --git a/yarn.lock b/yarn.lock index c310f29f7be3..45e2a4ac1b6b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2564,7 +2564,7 @@ __metadata: "@swc/core-win32-x64-msvc": 1.2.146 "@swc/helpers": ^0.5.0 "@swc/plugin-jest": latest - "@swc/types": ^0.1.3 + "@swc/types": ^0.1.4 "@taplo/cli": ^0.3.2 "@types/jest": ^28.1.4 "@types/node": ^20.5.0 @@ -2647,10 +2647,10 @@ __metadata: languageName: node linkType: hard -"@swc/types@npm:^0.1.3": - version: 0.1.3 - resolution: "@swc/types@npm:0.1.3" - checksum: 89b86ac338076a4c7ad3b8b016bb7d9ea6357517716d46232c372dc21f93166e08ed334da0a157e02cd5a1b1ff651542ddcd6f43988ee74b16f837a840681143 +"@swc/types@npm:^0.1.4": + version: 0.1.4 + resolution: "@swc/types@npm:0.1.4" + checksum: 9b09de7dca8e4b19bfb43f9e332c771855158cb761d26000807fe858447ecbc5342a6c257b26d9aa5497f7138fc58913693e2bee222e5042e0e8f57c2979ae66 languageName: node linkType: hard