Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/transforms/cjs): Allow mixing named exports and export stars. #2583

Merged
merged 9 commits into from
Oct 31, 2021
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];
}
});
});