From c65d90060a6cd6baac1ffff18b80c29dc935439c Mon Sep 17 00:00:00 2001 From: Jinbao1001 Date: Tue, 15 Oct 2024 15:27:11 +0800 Subject: [PATCH 1/2] feat: legacy interop --- crates/mako/src/config.rs | 2 ++ crates/mako/src/plugins/runtime.rs | 28 ++++++++++++++++--- e2e/fixtures/config.legacy_interop/expect.js | 6 ++++ .../config.legacy_interop/mako.config.json | 3 ++ e2e/fixtures/config.legacy_interop/src/a.ts | 1 + .../config.legacy_interop/src/index.ts | 10 +++++++ 6 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 e2e/fixtures/config.legacy_interop/expect.js create mode 100644 e2e/fixtures/config.legacy_interop/mako.config.json create mode 100644 e2e/fixtures/config.legacy_interop/src/a.ts create mode 100644 e2e/fixtures/config.legacy_interop/src/index.ts diff --git a/crates/mako/src/config.rs b/crates/mako/src/config.rs index 1dbc8c3ef..0fd93dea5 100644 --- a/crates/mako/src/config.rs +++ b/crates/mako/src/config.rs @@ -175,6 +175,8 @@ pub struct Config { pub clean: bool, pub node_polyfill: bool, pub ignores: Vec, + #[serde(rename = "legacyInterop")] + pub legacy_interop: bool, #[serde( rename = "_minifish", deserialize_with = "deserialize_minifish", diff --git a/crates/mako/src/plugins/runtime.rs b/crates/mako/src/plugins/runtime.rs index 77828ae9c..1915e7bec 100644 --- a/crates/mako/src/plugins/runtime.rs +++ b/crates/mako/src/plugins/runtime.rs @@ -46,7 +46,8 @@ impl MakoRuntime { let helpers = SwcHelpers::full_helpers() .into_iter() .map(|source| { - let code = Self::get_swc_helper_code(&source).unwrap(); + let code = + Self::get_swc_helper_code(&source, context.config.legacy_interop).unwrap(); let module_id: ModuleId = source.into(); let module_id = module_id.generate(context); format!("\"{}\": {}", module_id, code) @@ -65,7 +66,22 @@ impl MakoRuntime { )) } - fn get_swc_helper_code(path: &str) -> Result { + fn modify_legacy_code(origin_code: &str, legacy_interop: bool) -> String { + let mut new_code = origin_code.to_string(); + if legacy_interop { + new_code = new_code.replace( + "// ### legacy_interop ###", + r#" + if (typeof obj === "function") { + return obj + }; + "#, + ); + } + new_code + } + + fn get_swc_helper_code(path: &str, legacy_interop: bool) -> Result { let code = match path { "@swc/helpers/_/_interop_require_default" => r#" function(module, exports, __mako_require__) { @@ -93,7 +109,8 @@ function(module, exports, __mako_require__) { } } "#.trim(), - "@swc/helpers/_/_interop_require_wildcard" => r#" + "@swc/helpers/_/_interop_require_wildcard" => { + let origin = r#" function(module, exports, __mako_require__) { __mako_require__.d(exports, "__esModule", { value: true @@ -125,6 +142,7 @@ function(module, exports, __mako_require__) { if (obj === null || typeof obj !== "object" && typeof obj !== "function") return { default: obj }; + // ### legacy_interop ### var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) return cache.get(obj); var newObj = {}; @@ -139,7 +157,9 @@ function(module, exports, __mako_require__) { return newObj; } } - "#.trim(), + "#.trim(); + &Self::modify_legacy_code(origin, legacy_interop) + }, "@swc/helpers/_/_export_star" => r#" function(module, exports, __mako_require__) { __mako_require__.d(exports, "__esModule", { diff --git a/e2e/fixtures/config.legacy_interop/expect.js b/e2e/fixtures/config.legacy_interop/expect.js new file mode 100644 index 000000000..b4976c8e6 --- /dev/null +++ b/e2e/fixtures/config.legacy_interop/expect.js @@ -0,0 +1,6 @@ +const { injectSimpleJest } = require("../../../scripts/test-utils"); + +injectSimpleJest() + +require("./dist/index.js"); + diff --git a/e2e/fixtures/config.legacy_interop/mako.config.json b/e2e/fixtures/config.legacy_interop/mako.config.json new file mode 100644 index 000000000..b2ba3d880 --- /dev/null +++ b/e2e/fixtures/config.legacy_interop/mako.config.json @@ -0,0 +1,3 @@ +{ + "legacyInterop": true +} diff --git a/e2e/fixtures/config.legacy_interop/src/a.ts b/e2e/fixtures/config.legacy_interop/src/a.ts new file mode 100644 index 000000000..4e62402fb --- /dev/null +++ b/e2e/fixtures/config.legacy_interop/src/a.ts @@ -0,0 +1 @@ +module.exports = () => 1 diff --git a/e2e/fixtures/config.legacy_interop/src/index.ts b/e2e/fixtures/config.legacy_interop/src/index.ts new file mode 100644 index 000000000..0e83923a7 --- /dev/null +++ b/e2e/fixtures/config.legacy_interop/src/index.ts @@ -0,0 +1,10 @@ +// @ts-ignore +import * as a from './a' + + + + +it('should work when config legacyInterop',()=>{ + expect(a()).toBe(1) +}); + From beb113a3e82fed7af23ebb3f678249568610eae3 Mon Sep 17 00:00:00 2001 From: Jinbao1001 Date: Tue, 15 Oct 2024 15:43:22 +0800 Subject: [PATCH 2/2] fix: default value --- crates/mako/src/config/mako.config.default.json | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/mako/src/config/mako.config.default.json b/crates/mako/src/config/mako.config.default.json index bfbce56dd..267b8af71 100644 --- a/crates/mako/src/config/mako.config.default.json +++ b/crates/mako/src/config/mako.config.default.json @@ -72,6 +72,7 @@ "graphviz": false } }, + "legacyInterop": false, "useDefineForClassFields": true, "emitDecoratorMetadata": false, "watch": { "ignorePaths": [], "_nodeModulesRegexes": [] },