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(transforms): var conflicts in cjs-to-esm require-to-import transform #52

Merged
merged 2 commits into from
May 2, 2023

Conversation

js07
Copy link
Owner

@js07 js07 commented May 1, 2023

When name of imported module is same as name of variable in scope, it causes a conflict. This PR fixes that by renaming the imported module to ${pckgName}Module, if it's a default import.

For example:

  • Input:
  require("foo-bar")(5);
  const fooBar = "hello world";
  console.log(fooBar);
  • Output before ❌:
  import fooBar from "foo-bar";
  fooBar(5);
  const fooBar = "hello world";
  console.log(fooBar);
  • Output after ✅:
  import fooBarModule from "foo-bar";
  fooBarModule(5);
  const fooBar = "hello world";
  console.log(fooBar);

When the imported module is a named import, it's renamed to ${pckgName}${importedName}.

For example:

  • Input:
  const bar = "hello world";
  require("foo").bar(5);
  console.log(bar);
  • Output before ❌:
  import { bar } from "foo";
  const bar = "hello world";
  bar(5);
  console.log(bar);
  • Output after ✅:
  import { bar as fooBar } from "foo";
  const bar = "hello world";
  fooBar(5);
  console.log(bar);

Part of #50

When name of imported module is same as name of variable in scope, it
causes a conflict. This commit fixes that by renaming the imported
module to `${pckgName}Module`, if it's a default import.

When the imported module is a named import, it's renamed to
`${pckgName}${importedName}`.
@js07 js07 merged commit e2f4a08 into main May 2, 2023
@js07 js07 deleted the fix/cjs-to-esm-var-conflicts branch May 2, 2023 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant