Skip to content
New issue

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

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

Already on GitHub? # to your account

it could not correctly resolve paths with file extension #13

Open
taiwanhua opened this issue May 28, 2024 · 0 comments
Open

it could not correctly resolve paths with file extension #13

taiwanhua opened this issue May 28, 2024 · 0 comments

Comments

@taiwanhua
Copy link

taiwanhua commented May 28, 2024

When using babel-plugin-tsconfig-paths, I found that it could not correctly resolve paths. My tsconfig configuration is as follows:

{
  "compilerOptions": {
    "types": [],
    "module": "commonjs",
    "noEmit": true,
    "baseUrl": "./",
    "paths": {
      "@/*": ["./*"],
    }
  },
  "extends": "../tsconfig.base.json",
  "include": ["./", "../common"]
}

Then, I have a file located at '@/server/util/dbUtils' (which is actually a TypeScript file) being imported as follows:

import { toSafeInteger } from '@/server/util/dbUtils';

It is expected that after compilation, the import should be:

var _dbUtils = require("../../util/dbUtils");

// or 

var _dbUtils = require("../../util/dbUtils.js");

However, it is being resolved as:

var _dbUtils = require("../../util/dbUtils.ts");

which causes the service to crash.

For this reason, I think there should be an option to control unnecessary file extensions, or I might try to add a piece of code as follows:

In lib/resolve.js

const resolvePath = (sourceFile, importPath, basePath, extensions, aliases, relative) => {
 const resolver = getResolvers(importPath, aliases)
 if (!resolver) return

 const { alias, transformers } = resolver

 for (const transformer of transformers) {
   const transformedImport = path.join(
     basePath,
     importPath.replace(alias, transformer)
   )
   let checkImport = transformedImport
   try {
     const stat = fs.statSync(transformedImport)
     if (stat && stat.isDirectory()) {
       checkImport = path.join(transformedImport, 'index')
     }
   } catch (err) {
     // noop
   }

   // If the original import has an extension, then check for it, too, when
   // checking for existence of the file
   const checkExtensions = [''].concat(extensions)
   const fileVariations = checkExtensions.map((ext) => checkImport + ext)

   const realFile = fileVariations.find((file) => fs.existsSync(file))
   if (realFile) {
   const resolvedPath= (
       relative
         ? getRelativePath(sourceFile, realFile)
         : realFile
     ).replace(/\\/g, '/')
   
     if (resolvedPath.endsWith('.d.ts')) { // aviod remove .d.ts extension
       continue
     }
     
     return resolvedPath.replace(/\.tsx?$/, '') // here to remove ts or tsx extension
   }
 }
}
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant