diff --git a/ecmascript/transforms/module/src/common_js.rs b/ecmascript/transforms/module/src/common_js.rs index 3e4476c4cdf9..ef61117d9854 100644 --- a/ecmascript/transforms/module/src/common_js.rs +++ b/ecmascript/transforms/module/src/common_js.rs @@ -155,6 +155,19 @@ where ref specifiers, .. })) => { + // handle: export {sym as alias1, alias2, ...} from "x" + for ExportNamedSpecifier { orig, exported, .. } in + specifiers.into_iter().filter_map(|e| match e { + ExportSpecifier::Named(e) => Some(e), + _ => None, + }) + { + if let Some(exported) = &exported { + exports.push(exported.sym.clone()); + } else { + exports.push(orig.sym.clone()); + } + } scope.import_to_export(&src, !specifiers.is_empty()); } _ => {} diff --git a/ecmascript/transforms/module/tests/common_js.rs b/ecmascript/transforms/module/tests/common_js.rs index f3eab5b23157..0fe24849c7f6 100644 --- a/ecmascript/transforms/module/tests/common_js.rs +++ b/ecmascript/transforms/module/tests/common_js.rs @@ -4166,10 +4166,14 @@ Object.defineProperty(exports, "Scope", { return _interfaces.Scope; } }); +var _exportNames = { + Scope: true +}; var _http = require("./http"); var _interfaces = require("./interfaces"); Object.keys(_http).forEach(function(key) { if (key === "default" || key === "__esModule") return; + if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; if (key in exports && exports[key] === _http[key]) return; Object.defineProperty(exports, key, { enumerable: true, @@ -4206,6 +4210,7 @@ export * from './pipes'; } }); var _exportNames = { + id: true }; require("reflect-metadata"); var _http = require("./http"); diff --git a/tests/fixture/issue-2548/case1/input/.swcrc b/tests/fixture/issue-2548/case1/input/.swcrc new file mode 100644 index 000000000000..3f013e729084 --- /dev/null +++ b/tests/fixture/issue-2548/case1/input/.swcrc @@ -0,0 +1,5 @@ +{ + "module": { + "type": "commonjs" + } +} \ No newline at end of file diff --git a/tests/fixture/issue-2548/case1/input/index.js b/tests/fixture/issue-2548/case1/input/index.js new file mode 100644 index 000000000000..e5bddae419b7 --- /dev/null +++ b/tests/fixture/issue-2548/case1/input/index.js @@ -0,0 +1 @@ +export { default as X } from "./Z"; diff --git a/tests/fixture/issue-2548/case1/output/index.js b/tests/fixture/issue-2548/case1/output/index.js new file mode 100644 index 000000000000..c2e99752f5d3 --- /dev/null +++ b/tests/fixture/issue-2548/case1/output/index.js @@ -0,0 +1,16 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "X", { + enumerable: true, + get: function() { + return _z.default; + } +}); +var _z = _interopRequireDefault(require("./Z")); +function _interopRequireDefault(obj) { + return obj && obj.__esModule ? obj : { + default: obj + }; +} diff --git a/tests/fixture/issue-2548/case2/input/.swcrc b/tests/fixture/issue-2548/case2/input/.swcrc new file mode 100644 index 000000000000..3f013e729084 --- /dev/null +++ b/tests/fixture/issue-2548/case2/input/.swcrc @@ -0,0 +1,5 @@ +{ + "module": { + "type": "commonjs" + } +} \ No newline at end of file diff --git a/tests/fixture/issue-2548/case2/input/index.js b/tests/fixture/issue-2548/case2/input/index.js new file mode 100644 index 000000000000..18b9eb11b1ae --- /dev/null +++ b/tests/fixture/issue-2548/case2/input/index.js @@ -0,0 +1,2 @@ +export { default as X } from "./Z"; +export * from "./Z"; diff --git a/tests/fixture/issue-2548/case2/output/index.js b/tests/fixture/issue-2548/case2/output/index.js new file mode 100644 index 000000000000..744f3f90fe97 --- /dev/null +++ b/tests/fixture/issue-2548/case2/output/index.js @@ -0,0 +1,48 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "X", { + enumerable: true, + get: function() { + return _z.default; + } +}); +var _exportNames = { + X: true +}; +var _z = _interopRequireWildcard(require("./Z")); +function _interopRequireWildcard(obj) { + if (obj && obj.__esModule) { + return obj; + } else { + var newObj = { + }; + if (obj != null) { + for(var key in obj){ + if (Object.prototype.hasOwnProperty.call(obj, key)) { + var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : { + }; + if (desc.get || desc.set) { + Object.defineProperty(newObj, key, desc); + } else { + newObj[key] = obj[key]; + } + } + } + } + newObj.default = obj; + return newObj; + } +} +Object.keys(_z).forEach(function(key) { + if (key === "default" || key === "__esModule") return; + if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; + if (key in exports && exports[key] === _z[key]) return; + Object.defineProperty(exports, key, { + enumerable: true, + get: function() { + return _z[key]; + } + }); +}); diff --git a/tests/fixture/issue-2548/case3/input/.swcrc b/tests/fixture/issue-2548/case3/input/.swcrc new file mode 100644 index 000000000000..3f013e729084 --- /dev/null +++ b/tests/fixture/issue-2548/case3/input/.swcrc @@ -0,0 +1,5 @@ +{ + "module": { + "type": "commonjs" + } +} \ No newline at end of file diff --git a/tests/fixture/issue-2548/case3/input/index.js b/tests/fixture/issue-2548/case3/input/index.js new file mode 100644 index 000000000000..4987d8909c31 --- /dev/null +++ b/tests/fixture/issue-2548/case3/input/index.js @@ -0,0 +1,2 @@ +export { default as X, Y, X2 } from "./Z"; +export * from "./Z"; diff --git a/tests/fixture/issue-2548/case3/output/index.js b/tests/fixture/issue-2548/case3/output/index.js new file mode 100644 index 000000000000..f7aba41fae3f --- /dev/null +++ b/tests/fixture/issue-2548/case3/output/index.js @@ -0,0 +1,62 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "X", { + enumerable: true, + get: function() { + return _z.default; + } +}); +Object.defineProperty(exports, "Y", { + enumerable: true, + get: function() { + return _z.Y; + } +}); +Object.defineProperty(exports, "X2", { + enumerable: true, + get: function() { + return _z.X2; + } +}); +var _exportNames = { + X: true, + Y: true, + X2: true +}; +var _z = _interopRequireWildcard(require("./Z")); +function _interopRequireWildcard(obj) { + if (obj && obj.__esModule) { + return obj; + } else { + var newObj = { + }; + if (obj != null) { + for(var key in obj){ + if (Object.prototype.hasOwnProperty.call(obj, key)) { + var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : { + }; + if (desc.get || desc.set) { + Object.defineProperty(newObj, key, desc); + } else { + newObj[key] = obj[key]; + } + } + } + } + newObj.default = obj; + return newObj; + } +} +Object.keys(_z).forEach(function(key) { + if (key === "default" || key === "__esModule") return; + if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; + if (key in exports && exports[key] === _z[key]) return; + Object.defineProperty(exports, key, { + enumerable: true, + get: function() { + return _z[key]; + } + }); +});