fix(es/modules/cjs): FIx detection of exported names (#4737)

This commit is contained in:
Donny/강동윤 2022-05-22 04:55:35 +09:00 committed by GitHub
parent f7e000383c
commit d9bb59a8cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 62 additions and 2 deletions

View File

@ -0,0 +1,12 @@
{
"module": {
"type": "commonjs",
"noInterop": true
},
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": true
}
}
}

View File

@ -0,0 +1,6 @@
export * from "another-module";
export function whatever(notExportName: string) {
const shouldNotBeExportNameAsWell = 123;
return shouldNotBeExportNameAsWell + notExportName;
}

View File

@ -207,8 +207,20 @@ impl Fold for CommonJs {
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { decl, .. })) => {
let mut found: Vec<Ident> = vec![];
let mut v = DestructuringFinder { found: &mut found };
decl.visit_with(&mut v);
match decl {
Decl::Class(d) => {
found.push(d.ident.clone());
}
Decl::Fn(d) => {
found.push(d.ident.clone());
}
Decl::Var(v) => {
found.extend(find_pat_ids(&v.decls));
}
_ => {
unreachable!()
}
}
for ident in found {
exports.push(ident.sym.clone());

View File

@ -0,0 +1,6 @@
export * from "another-module";
export function whatever(notExportName) {
const shouldNotBeExportNameAsWell = 123;
return shouldNotBeExportNameAsWell + notExportName;
}

View File

@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _exportNames = {
whatever: true
};
exports.whatever = whatever;
var _anotherModule = require("another-module");
Object.keys(_anotherModule).forEach(function(key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _anotherModule[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function() {
return _anotherModule[key];
}
});
});
function whatever(notExportName) {
const shouldNotBeExportNameAsWell = 123;
return shouldNotBeExportNameAsWell + notExportName;
}