From 98f14931e7fa24b9d888eb5149c647840978b096 Mon Sep 17 00:00:00 2001 From: magic-akari Date: Sat, 1 Apr 2023 13:40:25 +0800 Subject: [PATCH] fix(es/modules): Hint nodejs for multiple `export *` (#7184) --- .../src/common_js.rs | 53 +++++++++---------- .../tests/fixture/common/issue-7180/input.js | 2 + .../fixture/common/issue-7180/module.json | 3 ++ .../fixture/common/issue-7180/output.amd.js | 13 +++++ .../fixture/common/issue-7180/output.cjs | 7 +++ .../fixture/common/issue-7180/output.umd.js | 16 ++++++ 6 files changed, 65 insertions(+), 29 deletions(-) create mode 100644 crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/input.js create mode 100644 crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/module.json create mode 100644 crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/output.amd.js create mode 100644 crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/output.cjs create mode 100644 crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/output.umd.js diff --git a/crates/swc_ecma_transforms_module/src/common_js.rs b/crates/swc_ecma_transforms_module/src/common_js.rs index c98248019830..c096f3d5d56b 100644 --- a/crates/swc_ecma_transforms_module/src/common_js.rs +++ b/crates/swc_ecma_transforms_module/src/common_js.rs @@ -530,41 +530,36 @@ where /// emit [cjs-module-lexer](https://github.com/nodejs/cjs-module-lexer) friendly exports list /// ```javascript - /// 0 && (__export(require("foo"))); + /// 0 && __export(require("foo")) && __export(require("bar")); /// ``` fn emit_lexer_ts_reexport(&self, link: &Link) -> Option { - let mut seq_list = vec![]; - link.iter().for_each(|(src, LinkItem(_, _, link_flag))| { - if link_flag.export_star() { + link.iter() + .filter(|(.., LinkItem(.., link_flag))| link_flag.export_star()) + .map(|(src, ..)| { let import_expr = self.resolver .make_require_call(self.unresolved_mark, src.clone(), DUMMY_SP); - let export = Expr::Ident(quote_ident!("__export")) - .as_call(DUMMY_SP, vec![import_expr.as_arg()]); - - seq_list.push(Box::new(export)); - } - }); - - if seq_list.is_empty() { - None - } else { - let seq_expr = SeqExpr { - span: DUMMY_SP, - exprs: seq_list, - } - .into(); - - let expr = BinExpr { - span: DUMMY_SP, - op: op!("&&"), - left: 0.into(), - right: seq_expr, - }; - - Some(expr.into_stmt()) - } + Expr::Ident(quote_ident!("__export")).as_call(DUMMY_SP, vec![import_expr.as_arg()]) + }) + .reduce(|left, right| { + BinExpr { + span: DUMMY_SP, + op: op!("&&"), + left: left.into(), + right: right.into(), + } + .into() + }) + .map(|expr| { + BinExpr { + span: DUMMY_SP, + op: op!("&&"), + left: 0.into(), + right: expr.into(), + } + .into_stmt() + }) } fn pure_span(&self) -> Span { diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/input.js b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/input.js new file mode 100644 index 000000000000..d687c5868ab9 --- /dev/null +++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/input.js @@ -0,0 +1,2 @@ +export * from "./fn.js"; +export * from "./fn2.js"; diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/module.json b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/module.json new file mode 100644 index 000000000000..2923d83b417c --- /dev/null +++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/module.json @@ -0,0 +1,3 @@ +{ + "importInterop": "node" +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/output.amd.js b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/output.amd.js new file mode 100644 index 000000000000..bffc7f407bfe --- /dev/null +++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/output.amd.js @@ -0,0 +1,13 @@ +define([ + "require", + "exports", + "./fn.js", + "./fn2.js" +], function(require, exports, _fn, _fn2) { + "use strict"; + Object.defineProperty(exports, "__esModule", { + value: true + }); + _export_star(_fn, exports); + _export_star(_fn2, exports); +}); diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/output.cjs b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/output.cjs new file mode 100644 index 000000000000..8d6f49cdd5bf --- /dev/null +++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/output.cjs @@ -0,0 +1,7 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { + value: true +}); +0 && __export(require("./fn.js")) && __export(require("./fn2.js")); +_export_star(require("./fn.js"), exports); +_export_star(require("./fn2.js"), exports); diff --git a/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/output.umd.js b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/output.umd.js new file mode 100644 index 000000000000..c1aa067096ff --- /dev/null +++ b/crates/swc_ecma_transforms_module/tests/fixture/common/issue-7180/output.umd.js @@ -0,0 +1,16 @@ +(function(global, factory) { + if (typeof module === "object" && typeof module.exports === "object") factory(exports, require("./fn.js"), require("./fn2.js")); + else if (typeof define === "function" && define.amd) define([ + "exports", + "./fn.js", + "./fn2.js" + ], factory); + else if (global = typeof globalThis !== "undefined" ? globalThis : global || self) factory(global.input = {}, global.fnJs, global.fn2Js); +})(this, function(exports, _fn, _fn2) { + "use strict"; + Object.defineProperty(exports, "__esModule", { + value: true + }); + _export_star(_fn, exports); + _export_star(_fn2, exports); +});