Skip to content

Commit

Permalink
fix(es/transforms/cjs): Allow mixing named exports and export stars. (#…
Browse files Browse the repository at this point in the history
…2583)

swc_ecma_transforms_module:
 - `common_js`: Fix _exportNames when exporting aliases and using `export *`s . (#2548)
  • Loading branch information
JeremyGrieshop authored Oct 31, 2021
1 parent 306cf98 commit 7e3fb0a
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ecmascript/transforms/module/src/common_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
_ => {}
Expand Down
5 changes: 5 additions & 0 deletions ecmascript/transforms/module/tests/common_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -4206,6 +4210,7 @@ export * from './pipes';
}
});
var _exportNames = {
id: true
};
require("reflect-metadata");
var _http = require("./http");
Expand Down
5 changes: 5 additions & 0 deletions tests/fixture/issue-2548/case1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"module": {
"type": "commonjs"
}
}
1 change: 1 addition & 0 deletions tests/fixture/issue-2548/case1/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as X } from "./Z";
16 changes: 16 additions & 0 deletions tests/fixture/issue-2548/case1/output/index.js
Original file line number Diff line number Diff line change
@@ -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
};
}
5 changes: 5 additions & 0 deletions tests/fixture/issue-2548/case2/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"module": {
"type": "commonjs"
}
}
2 changes: 2 additions & 0 deletions tests/fixture/issue-2548/case2/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as X } from "./Z";
export * from "./Z";
48 changes: 48 additions & 0 deletions tests/fixture/issue-2548/case2/output/index.js
Original file line number Diff line number Diff line change
@@ -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];
}
});
});
5 changes: 5 additions & 0 deletions tests/fixture/issue-2548/case3/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"module": {
"type": "commonjs"
}
}
2 changes: 2 additions & 0 deletions tests/fixture/issue-2548/case3/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as X, Y, X2 } from "./Z";
export * from "./Z";
62 changes: 62 additions & 0 deletions tests/fixture/issue-2548/case3/output/index.js
Original file line number Diff line number Diff line change
@@ -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];
}
});
});

1 comment on commit 7e3fb0a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 7e3fb0a Previous: 306cf98 Ratio
base_tr_fixer 33527 ns/iter (± 4320) 23637 ns/iter (± 273) 1.42
base_tr_resolver_and_hygiene 185793 ns/iter (± 33015) 127831 ns/iter (± 9050) 1.45
codegen_es2015 64492 ns/iter (± 8349) 50716 ns/iter (± 249) 1.27
codegen_es2016 67470 ns/iter (± 6376) 50810 ns/iter (± 268) 1.33
codegen_es2017 64223 ns/iter (± 6694) 50902 ns/iter (± 329) 1.26
codegen_es2018 64585 ns/iter (± 6510) 50806 ns/iter (± 440) 1.27
codegen_es2019 65325 ns/iter (± 5806) 50792 ns/iter (± 368) 1.29
codegen_es2020 64512 ns/iter (± 5637) 50924 ns/iter (± 484) 1.27
codegen_es3 65133 ns/iter (± 7149) 51019 ns/iter (± 278) 1.28
codegen_es5 64711 ns/iter (± 6999) 50854 ns/iter (± 263) 1.27
config_for_file 19953 ns/iter (± 5847) 13393 ns/iter (± 4153) 1.49
full_es2015 223909007 ns/iter (± 15395732) 181802089 ns/iter (± 4459166) 1.23
full_es2016 172234824 ns/iter (± 19084508) 144126467 ns/iter (± 5698050) 1.20
full_es2017 190429499 ns/iter (± 11469692) 147885602 ns/iter (± 13619321) 1.29
full_es2018 184936283 ns/iter (± 14801689) 147737699 ns/iter (± 9881223) 1.25
full_es2019 184965280 ns/iter (± 12804072) 144739291 ns/iter (± 10593084) 1.28
full_es2020 182459167 ns/iter (± 13023832) 144520231 ns/iter (± 12754440) 1.26
full_es3 292555956 ns/iter (± 42002125) 209975583 ns/iter (± 21969787) 1.39
full_es5 268231801 ns/iter (± 35165635) 195794888 ns/iter (± 17827869) 1.37
parser 822745 ns/iter (± 90698) 626487 ns/iter (± 16175) 1.31
transforms_es2015 1579082 ns/iter (± 111984) 969951 ns/iter (± 31346) 1.63
transforms_es2016 924053 ns/iter (± 191852) 573229 ns/iter (± 21390) 1.61
transforms_es2017 916545 ns/iter (± 185637) 562024 ns/iter (± 29592) 1.63
transforms_es2018 907745 ns/iter (± 210743) 553656 ns/iter (± 20855) 1.64
transforms_es2019 897137 ns/iter (± 128786) 541336 ns/iter (± 17764) 1.66
transforms_es2020 905688 ns/iter (± 178610) 533421 ns/iter (± 11989) 1.70
transforms_es3 2013976 ns/iter (± 577203) 1100217 ns/iter (± 56868) 1.83
transforms_es5 1690132 ns/iter (± 385372) 966826 ns/iter (± 68467) 1.75
ser_ast_node 196 ns/iter (± 64) 148 ns/iter (± 4) 1.32
ser_serde 209 ns/iter (± 25) 165 ns/iter (± 1) 1.27
emit_colors 10438500 ns/iter (± 7631756) 14499271 ns/iter (± 18794848) 0.72
emit_large 104033094 ns/iter (± 160376738) 97192152 ns/iter (± 154513286) 1.07
base_clone 2997047 ns/iter (± 340820) 2262246 ns/iter (± 10267) 1.32
fold_span 5078887 ns/iter (± 614587) 3780464 ns/iter (± 21428) 1.34
fold_span_panic 5769081 ns/iter (± 1543502) 4001316 ns/iter (± 12108) 1.44
visit_mut_span 3614047 ns/iter (± 392296) 2772012 ns/iter (± 12413) 1.30
visit_mut_span_panic 3668768 ns/iter (± 565342) 2822342 ns/iter (± 14096) 1.30
boxing_boxed 188 ns/iter (± 14) 143 ns/iter (± 0) 1.31
boxing_boxed_clone 81 ns/iter (± 6) 68 ns/iter (± 0) 1.19
boxing_unboxed 178 ns/iter (± 8) 130 ns/iter (± 0) 1.37
boxing_unboxed_clone 79 ns/iter (± 3) 63 ns/iter (± 0) 1.25

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.