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

Support for barrel files (index.ts) #11

Closed
fox1t opened this issue Jun 19, 2024 · 1 comment
Closed

Support for barrel files (index.ts) #11

fox1t opened this issue Jun 19, 2024 · 1 comment

Comments

@fox1t
Copy link
Contributor

fox1t commented Jun 19, 2024

Hey, thanks for putting together this simple yet powerful package. It is helping us migrate from commonjs to esm in our huge monorepo.

While using it, I noticed the fix for imports from barrel files is ignored, even if the barrel file itself is correctly resolved.
You can see in the following example:

[scanImport] {
  srcFile: './src/index.ts',
  importCode: "import { NodeEnv, envVariables } from './config';",
  importName: 'src/config',
  importFile: 'src/config/index.ts'
}

This happens specifically due to this line: https://github.com/beenotung/fix-esm-import-path/blob/master/fix-esm-import-path.js#L150

Removing it and changing the below code from:

        importCode = fixImport({
          srcFile,
          importCode,
          from: name,
          to: name + '.js',
        })

to

        importCode = fixImport({
          srcFile,
          importCode,
          from: name,
          to:
            name +
            (importFile.startsWith(importName + '/index')
              ? '/index.js'
              : '.js'),
        })

fixed the issue.

Would you happen to know if the original behavior is the intended one? If so, why did you choose explicitly not to support barrel files?
I can open a PR if you want to support this specific use case.

beenotung added a commit that referenced this issue Jun 19, 2024
@beenotung
Copy link
Owner

You suggestion works, I didn't think of determining the to value by checking the importName + "/index". This can prevent it from adding "/index.js" instead of ".js" when import name is already ending with "/index".

This patch is released in fix-esm-import-path@1.8.0 🚀

Please feel free to re-open the issue if the issue is not resolved

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

No branches or pull requests

2 participants