Skip to content

Commit 68abb56

Browse files
committed
Update readme
1 parent 1ac3aee commit 68abb56

File tree

5 files changed

+65
-54
lines changed

5 files changed

+65
-54
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ jobs:
99
strategy:
1010
matrix:
1111
node:
12-
- 16
1312
- 18
1413
- 20
1514
os:

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ Add the following to your `.eslintrc` config:
129129

130130
## Options from [`enhanced-resolve`][]
131131

132-
#### `conditionNames` - [See default](src/default.ts)
132+
#### `conditionNames` - [See default](src/default.ts#1)
133133

134-
#### `extensions` - [See default](src/default.ts)
134+
#### `extensions` - [See default](src/default.ts#20)
135135

136-
#### `extensionAlias` - [See default](src/default.ts)
136+
#### `extensionAlias` - [See default](src/default.ts#22)
137137

138-
### `mainFields` - [See default](src/default.ts)
138+
### `mainFields` - [See default](src/default.ts#35)
139139

140140
### Other options
141141

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@
7272
},
7373
"packageManager": "[email protected]+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee",
7474
"engines": {
75-
"node": ">=16"
75+
"node": ">=18"
7676
}
7777
}

src/resolveImport.ts

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import { logger } from './logger'
1313

1414
import type { InternalResolverOptions, Matcher } from './types'
1515
import type { Resolver } from 'enhanced-resolve'
16+
import type { TsConfigResult } from 'get-tsconfig'
1617
import type { Version } from 'is-bun-module'
1718

18-
const fileTsConfigCache = new Map()
19+
const fileTsJsConfigCache: Map<string, TsConfigResult> = new Map()
20+
const possiblePathsBySpecifier: Map<{ specifier: string; configPath: TsConfigResult['path'] }, string[]> = new Map()
1921

2022
/**
2123
* Resolve an import specifier to a path
@@ -122,68 +124,74 @@ function getMappedPath(
122124

123125
extensions = ['', ...extensions]
124126

125-
let paths: string[] = []
126-
127127
if (RELATIVE_PATH_PATTERN.test(specifier)) {
128128
const resolved = path.resolve(path.dirname(file), specifier)
129129
if (isFile(resolved)) {
130-
paths = [resolved]
130+
return resolved
131131
}
132-
} else {
133-
let projectConfig
134-
const isJsconfig = currentMatchersOfCwd.every((mapper) => mapper.path.includes('jsconfig'))
132+
}
135133

136-
const fromCache = fileTsConfigCache.get(file)
134+
let paths: string[] = []
137135

138-
if (fromCache) {
139-
projectConfig = fromCache
140-
} else {
141-
projectConfig = getTsconfig(file, isJsconfig ? 'jsconfig.json' : 'tsconfig.json')
142-
}
136+
let projectConfig: TsConfigResult | null = null
143137

144-
if (projectConfig) logger('project config by file path:', projectConfig?.path)
138+
const { ext } = path.parse(file)
145139

146-
const closestMapper = currentMatchersOfCwd?.find((mapper) => {
147-
return mapper.path === projectConfig?.path
148-
})
140+
const fromCache = fileTsJsConfigCache.get(file)
149141

150-
if (closestMapper) {
151-
logger('found closest mapper by config path:', closestMapper.path)
152-
}
142+
if (fromCache) {
143+
projectConfig = fromCache
144+
} else {
145+
projectConfig = getTsconfig(file, ext.includes('js') ? 'jsconfig.json' : 'tsconfig.json')
146+
if (projectConfig) fileTsJsConfigCache.set(file, projectConfig)
147+
}
153148

154-
const possiblePaths = (
155-
closestMapper
156-
? closestMapper.matcher(specifier)
157-
: currentMatchersOfCwd.map((mapper) => mapper?.matcher(specifier)).flat()
158-
)
159-
.map((item) => [
160-
...extensions.map((ext) => `${item}${ext}`),
161-
...originalExtensions.map((ext) => `${item}/index${ext}`),
162-
])
163-
.flat()
164-
165-
logger('possible paths:', possiblePaths)
166-
167-
paths = possiblePaths.filter((mappedPath) => {
168-
try {
169-
const stat = fs.statSync(mappedPath, { throwIfNoEntry: false })
170-
if (stat === undefined) return false
171-
if (stat.isFile()) return true
172-
if (stat.isDirectory()) {
173-
return isModule(mappedPath)
174-
}
175-
} catch {
176-
return false
177-
}
149+
if (projectConfig) logger('project config by file path:', projectConfig?.path)
178150

179-
return false
180-
})
151+
const closestMapper = currentMatchersOfCwd?.find((mapper) => {
152+
return mapper.path === projectConfig?.path
153+
})
181154

182-
logger('paths', paths)
155+
if (closestMapper) {
156+
logger('found closest mapper by config path:', closestMapper.path)
183157
}
184158

159+
const possiblePaths = (
160+
closestMapper
161+
? closestMapper.matcher(specifier)
162+
: currentMatchersOfCwd.map((mapper) => mapper?.matcher(specifier)).flat()
163+
)
164+
.map((item) => [
165+
...extensions.map((ext) => `${item}${ext}`),
166+
...originalExtensions.map((ext) => `${item}/index${ext}`),
167+
])
168+
.flat()
169+
170+
logger('possible paths:', possiblePaths)
171+
172+
paths = projectConfig?.path
173+
? (possiblePathsBySpecifier.get({ specifier, configPath: projectConfig.path }) ?? [])
174+
: possiblePaths.filter((mappedPath) => {
175+
try {
176+
const stat = fs.statSync(mappedPath, { throwIfNoEntry: false })
177+
if (stat === undefined) return false
178+
if (stat.isFile()) return true
179+
if (stat.isDirectory()) {
180+
return isModule(mappedPath)
181+
}
182+
} catch {
183+
return false
184+
}
185+
186+
return false
187+
})
188+
189+
if (projectConfig?.path) possiblePathsBySpecifier.set({ specifier, configPath: projectConfig.path }, paths)
190+
185191
if (paths.length > 1) {
186-
logger('found multiple matching ts paths:', paths)
192+
logger('found multiple matching paths:', paths)
193+
} else {
194+
logger('found matching paths:', paths)
187195
}
188196

189197
return paths[0]

tools/postbuild.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ newPackageJson.publishConfig = {
1717
access: 'public',
1818
}
1919

20+
newPackageJson.engines = {
21+
node: '>=16',
22+
}
23+
2024
newPackageJson = sortPackageJson(newPackageJson)
2125

2226
fs.writeFileSync('dist/package.json', JSON.stringify(newPackageJson, null, 2))

0 commit comments

Comments
 (0)