diff --git a/crates/swc/tests/fixture/issue-1714/case1/output/index.js b/crates/swc/tests/fixture/issue-1714/case1/output/index.js index b6625cc9bd7..26fb3e02c4b 100644 --- a/crates/swc/tests/fixture/issue-1714/case1/output/index.js +++ b/crates/swc/tests/fixture/issue-1714/case1/output/index.js @@ -2,6 +2,9 @@ Object.defineProperty(exports, "__esModule", { value: true }); +var _exportNames = { + render: true +}; Object.defineProperty(exports, "render", { enumerable: true, get: function() { @@ -12,6 +15,7 @@ var _customRender = require("./customRender"); var _react = _interopRequireWildcard(require("@testing-library/react")); Object.keys(_react).forEach(function(key) { if (key === "default" || key === "__esModule") return; + if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; if (key in exports && exports[key] === _react[key]) return; Object.defineProperty(exports, key, { enumerable: true, diff --git a/crates/swc_ecma_transforms_module/src/common_js.rs b/crates/swc_ecma_transforms_module/src/common_js.rs index 5375892cc2c..b48d480fd42 100644 --- a/crates/swc_ecma_transforms_module/src/common_js.rs +++ b/crates/swc_ecma_transforms_module/src/common_js.rs @@ -89,6 +89,31 @@ where // Used only if export * exists let mut exported_names: Option = None; + // make a preliminary pass through to collect exported names ahead of time + for item in &items { + match item { + ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(NamedExport { + ref specifiers, + .. + })) => { + 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()); + } + } + } + + _ => {} + } + } + for item in items { self.in_top_level = true; @@ -158,21 +183,9 @@ 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()); } + _ => {} } drop(scope); diff --git a/crates/swc_ecma_transforms_module/tests/common_js.rs b/crates/swc_ecma_transforms_module/tests/common_js.rs index 5949b4ecaa8..c4c456e3ff3 100644 --- a/crates/swc_ecma_transforms_module/tests/common_js.rs +++ b/crates/swc_ecma_transforms_module/tests/common_js.rs @@ -4161,6 +4161,9 @@ test!( Object.defineProperty(exports, "__esModule", { value: true }); +var _exportNames = { + Scope: true +}; Object.defineProperty(exports, "Scope", { enumerable: true, get: function() { @@ -4170,6 +4173,7 @@ Object.defineProperty(exports, "Scope", { var _http = require("./http"); 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, @@ -4200,15 +4204,15 @@ export * from './pipes'; Object.defineProperty(exports, "__esModule", { value: true }); + var _exportNames = { + id: true + }; Object.defineProperty(exports, "id", { enumerable: true, get: function() { return _interfaces.id; } }); - var _exportNames = { - id: true - }; require("reflect-metadata"); var _http = require("./http"); Object.keys(_http).forEach(function(key) { diff --git a/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2101/1/input.js b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2101/1/input.js new file mode 100644 index 00000000000..d9a316de4b9 --- /dev/null +++ b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2101/1/input.js @@ -0,0 +1,7 @@ +import { customRender } from './customRender'; + +// re-export everything +export * from '@testing-library/react'; + +// override render method +export { customRender as render }; diff --git a/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2101/1/output.js b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2101/1/output.js new file mode 100644 index 00000000000..3a8e1443e2f --- /dev/null +++ b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2101/1/output.js @@ -0,0 +1,26 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { + value: true +}); +var _exportNames = { + render: true +}; +Object.defineProperty(exports, "render", { + enumerable: true, + get: function() { + return _customRender.customRender; + } +}); +var _customRender = require("./customRender"); +var _react = require("@testing-library/react"); +Object.keys(_react).forEach(function(key) { + if (key === "default" || key === "__esModule") return; + if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; + if (key in exports && exports[key] === _react[key]) return; + Object.defineProperty(exports, key, { + enumerable: true, + get: function() { + return _react[key]; + } + }); +}); diff --git a/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2101/2/input.js b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2101/2/input.js new file mode 100644 index 00000000000..a13caef5c37 --- /dev/null +++ b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2101/2/input.js @@ -0,0 +1,9 @@ +import { customRender } from './customRender'; + +// override render method +export { customRender as render }; + +// re-ordering the export wildcard from case 1 should make no difference to output + +// re-export everything +export * from '@testing-library/react'; diff --git a/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2101/2/output.js b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2101/2/output.js new file mode 100644 index 00000000000..f9f5c89040e --- /dev/null +++ b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2101/2/output.js @@ -0,0 +1,26 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { + value: true +}); +Object.defineProperty(exports, "render", { + enumerable: true, + get: function() { + return _customRender.customRender; + } +}); +var _exportNames = { + render: true + }; +var _customRender = require("./customRender"); +var _react = require("@testing-library/react"); +Object.keys(_react).forEach(function(key) { + if (key === "default" || key === "__esModule") return; + if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; + if (key in exports && exports[key] === _react[key]) return; + Object.defineProperty(exports, key, { + enumerable: true, + get: function() { + return _react[key]; + } + }); +}); diff --git a/crates/swc/tests/fixture/issue-2548/case1/input/.swcrc b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case1/input/.swcrc similarity index 100% rename from crates/swc/tests/fixture/issue-2548/case1/input/.swcrc rename to crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case1/input/.swcrc diff --git a/crates/swc/tests/fixture/issue-2548/case1/input/index.js b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case1/input/index.js similarity index 100% rename from crates/swc/tests/fixture/issue-2548/case1/input/index.js rename to crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case1/input/index.js diff --git a/crates/swc/tests/fixture/issue-2548/case1/output/index.js b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case1/output/index.js similarity index 100% rename from crates/swc/tests/fixture/issue-2548/case1/output/index.js rename to crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case1/output/index.js diff --git a/crates/swc/tests/fixture/issue-2548/case2/input/.swcrc b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case2/input/.swcrc similarity index 100% rename from crates/swc/tests/fixture/issue-2548/case2/input/.swcrc rename to crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case2/input/.swcrc diff --git a/crates/swc/tests/fixture/issue-2548/case2/input/index.js b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case2/input/index.js similarity index 100% rename from crates/swc/tests/fixture/issue-2548/case2/input/index.js rename to crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case2/input/index.js diff --git a/crates/swc/tests/fixture/issue-2548/case2/output/index.js b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case2/output/index.js similarity index 100% rename from crates/swc/tests/fixture/issue-2548/case2/output/index.js rename to crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case2/output/index.js diff --git a/crates/swc/tests/fixture/issue-2548/case3/input/.swcrc b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case3/input/.swcrc similarity index 100% rename from crates/swc/tests/fixture/issue-2548/case3/input/.swcrc rename to crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case3/input/.swcrc diff --git a/crates/swc/tests/fixture/issue-2548/case3/input/index.js b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case3/input/index.js similarity index 100% rename from crates/swc/tests/fixture/issue-2548/case3/input/index.js rename to crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case3/input/index.js diff --git a/crates/swc/tests/fixture/issue-2548/case3/output/index.js b/crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case3/output/index.js similarity index 100% rename from crates/swc/tests/fixture/issue-2548/case3/output/index.js rename to crates/swc_ecma_transforms_module/tests/fixture/commonjs/issue-2548/case3/output/index.js