From 261dc67ac6bd7bfb1e9c15a1a467387a8f110c16 Mon Sep 17 00:00:00 2001 From: pshu Date: Sun, 19 May 2024 23:31:35 +0800 Subject: [PATCH 1/3] =?UTF-8?q?test:=20=E2=9C=85=20add=20runtime=20assert?= =?UTF-8?q?=20in=20decorator=20and=20target=20to=20chrome=2040?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- e2e/fixtures/javascript.legacy-decorator/expect.js | 11 +++++++++-- .../javascript.legacy-decorator/mako.config.json | 3 +++ e2e/fixtures/javascript.legacy-decorator/src/index.ts | 10 ++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/e2e/fixtures/javascript.legacy-decorator/expect.js b/e2e/fixtures/javascript.legacy-decorator/expect.js index d63534c49..da5a10318 100644 --- a/e2e/fixtures/javascript.legacy-decorator/expect.js +++ b/e2e/fixtures/javascript.legacy-decorator/expect.js @@ -1,11 +1,18 @@ const assert = require("assert"); -const { parseBuildResult, moduleReg } = require("../../../scripts/test-utils"); +const { + parseBuildResult, + moduleReg, + injectSimpleJest, +} = require("../../../scripts/test-utils"); const { files } = parseBuildResult(__dirname); const names = Object.keys(files).join(","); const content = files["index.js"]; +injectSimpleJest(); assert( content.includes(`Foo = _ts_decorate._([`), - "legacy decorator should works" + "legacy decorator should works", ); + +require("./dist/index.js"); diff --git a/e2e/fixtures/javascript.legacy-decorator/mako.config.json b/e2e/fixtures/javascript.legacy-decorator/mako.config.json index 41f3c531b..c43691a0e 100644 --- a/e2e/fixtures/javascript.legacy-decorator/mako.config.json +++ b/e2e/fixtures/javascript.legacy-decorator/mako.config.json @@ -2,5 +2,8 @@ "minify": false, "optimization": { "skipModules": false + }, + "targets": { + "chrome": 40 } } diff --git a/e2e/fixtures/javascript.legacy-decorator/src/index.ts b/e2e/fixtures/javascript.legacy-decorator/src/index.ts index 626285fb2..1e0ef1855 100644 --- a/e2e/fixtures/javascript.legacy-decorator/src/index.ts +++ b/e2e/fixtures/javascript.legacy-decorator/src/index.ts @@ -1,2 +1,12 @@ @bar() class Foo {} + +function bar() { + return function (theClass) { + theClass.bar = true; + }; +} + +it("should run the decortate", () => { + expect(Foo.bar).toBe(true); +}); From 3016e03a43bcd843d2da593227b80f16d09fee91 Mon Sep 17 00:00:00 2001 From: pshu Date: Mon, 20 May 2024 00:24:11 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=F0=9F=90=9B=20move=20decorator=20be?= =?UTF-8?q?fore=20preset=5Fenv?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/mako/src/build/transform.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/mako/src/build/transform.rs b/crates/mako/src/build/transform.rs index e960bb020..f5955d392 100644 --- a/crates/mako/src/build/transform.rs +++ b/crates/mako/src/build/transform.rs @@ -135,6 +135,11 @@ impl Transform { // folders let mut folders: Vec> = vec![]; + folders.push(Box::new(decorators(decorators::Config { + legacy: true, + emit_metadata: false, + ..Default::default() + }))); // TODO: is it a problem to clone comments? let comments = origin_comments.get_swc_comments().clone(); folders.push(Box::new(swc_preset_env::preset_env( @@ -150,11 +155,6 @@ impl Transform { Assumptions::default(), &mut FeatureFlag::default(), ))); - folders.push(Box::new(decorators(decorators::Config { - legacy: true, - emit_metadata: false, - ..Default::default() - }))); // simplify, but keep top level dead code // e.g. import x from 'foo'; but x is not used // this must be kept for tree shaking to work From 832cc6f44cedf4e82c0a6aacd88819de606f2afc Mon Sep 17 00:00:00 2001 From: pshu Date: Mon, 20 May 2024 15:05:52 +0800 Subject: [PATCH 3/3] add comment on decorators --- crates/mako/src/build/transform.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/mako/src/build/transform.rs b/crates/mako/src/build/transform.rs index f5955d392..3f936f30f 100644 --- a/crates/mako/src/build/transform.rs +++ b/crates/mako/src/build/transform.rs @@ -135,6 +135,7 @@ impl Transform { // folders let mut folders: Vec> = vec![]; + // decorators should go before preset_env, when compile down to es5, classes become functions, then the decorators on the functions will be removed silently. folders.push(Box::new(decorators(decorators::Config { legacy: true, emit_metadata: false,