Skip to content

Commit

Permalink
fix: import name conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Dec 19, 2023
1 parent f23fdef commit 44e42b4
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/webcrack/src/unpack/webpack/esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,25 @@ export function convertESM(module: WebpackModule): void {
...new Set(references.map((p) => p.node.property.name)),
];
const localNames = importNames.map((name) => {
const binding = path.scope.getBinding(name);
const hasNameConflict = binding?.referencePaths.some(
(p) => p.scope.getBinding(name) !== binding,
const hasNameConflict = binding.referencePaths.some((ref) =>
ref.scope.hasBinding(name),
);
return hasNameConflict ? path.scope.generateUid(name) : name;
});

path.replaceWith(
const [importDeclaration] = path.replaceWith(
buildNamedImport(
localNames,
importNames,
String(requiredModuleId.current),
),
);
importDeclaration.scope.crawl();

[...references].forEach((ref) => {
ref.replaceWith(ref.node.property);
const localName =
localNames[importNames.indexOf(ref.node.property.name)];
ref.replaceWith(t.identifier(localName));
if (zeroSequenceMatcher.match(ref.parent)) {
ref.parentPath.replaceWith(ref);
}
Expand Down

0 comments on commit 44e42b4

Please sign in to comment.