Skip to content

Commit

Permalink
fix(ts-transform-paths): import with extension #21
Browse files Browse the repository at this point in the history
  • Loading branch information
Marty Mcfly committed Dec 2, 2019
1 parent ea49f32 commit 46f4933
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
17 changes: 1 addition & 16 deletions packages/ts-transform-paths/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default {
})
}
```
## Setup For [ts-loader](https://github.com/TypeStrong/ts-loader)
## Setup For webpack [ts-loader](https://github.com/TypeStrong/ts-loader)

```js
const tsTransformPaths = require('@zerollup/ts-transform-paths');
Expand Down Expand Up @@ -145,18 +145,3 @@ interface Config {
exclude?: string[] | void
}
```

## Limitations

Only first element in paths substitutions array used.

my-lib/tsconfig.json:
```json
{
"compilerOptions": {
"paths": {
"my-lib/*": ["src/*", "not_used_substitution/*"]
}
}
}
```
31 changes: 16 additions & 15 deletions packages/ts-transform-paths/src/ImportPathInternalResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Config, EmitHost, TransformationContext } from './Types'

const jsExts = ['min.js', 'js'] as const

const tsParts = ['d.ts', 'd.tsx','.ts', '.tsx', '/index.ts', '/index.tsx', '/index.d.ts', '/index.d.tsx'] as const
const tsParts = ['d.ts', 'd.tsx','.ts', '.tsx', '/index.ts', '/index.tsx', '/index.d.ts', '/index.d.tsx', ''] as const

export class ImportPathInternalResolver {
protected resolver: ImportPathsResolver
Expand All @@ -30,24 +30,25 @@ export class ImportPathInternalResolver {
resolveImport(oldImport: string, currentDir: string): string | undefined {
const { emitHost } = this
const newImports = this.resolver.getImportSuggestions(oldImport, currentDir)
if (!newImports || newImports.length === 0) return
const newImport = newImports[0]
if (this.config.tryLoadJs && emitHost && emitHost.fileExists) {
for (let ext of jsExts) {
const importWithExtension = `${newImport}.${ext}`
if (emitHost.fileExists(path.join(currentDir, importWithExtension))) {
return importWithExtension
if (!newImports) return
for (let newImport of newImports) {
if (this.config.tryLoadJs && emitHost && emitHost.fileExists) {
for (let ext of jsExts) {
const importWithExtension = `${newImport}.${ext}`
if (emitHost.fileExists(path.join(currentDir, importWithExtension))) {
return importWithExtension
}
}
}
}

let newImportPath = path.join(currentDir, newImport)
if (newImportPath[0] === '.') newImportPath = newImportPath.substring(2)
let newImportPath = path.join(currentDir, newImport)
if (newImportPath[0] === '.') newImportPath = newImportPath.substring(2)

const host = this.program || emitHost
if (!host) return newImport
for (let part of tsParts) {
if (host.getSourceFile(`${newImportPath}${part}`)) return newImport
const host = this.program || emitHost
if (!host) return newImport
for (let part of tsParts) {
if (host.getSourceFile(`${newImportPath}${part}`)) return newImport
}
}
}
}
Expand Down

0 comments on commit 46f4933

Please sign in to comment.