Skip to content

Commit 98c4812

Browse files
committed
Use enhanced-resolve to load files
1 parent 2d46805 commit 98c4812

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

package-lock.json

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"ast-types": "^0.14.2",
4444
"clear-module": "^4.1.2",
4545
"cpy-cli": "^5.0.0",
46+
"enhanced-resolve": "^5.17.1",
4647
"esbuild": "^0.19.8",
4748
"escalade": "^3.1.1",
4849
"import-sort-style-module": "^6.0.0",

src/config.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import loadConfigFallback from 'tailwindcss/loadConfig'
1717
import resolveConfigFallback from 'tailwindcss/resolveConfig'
1818
import type { RequiredConfig } from 'tailwindcss/types/config.js'
1919
import { expiringMap } from './expiring-map.js'
20-
import { resolveFrom, resolveIn } from './resolve'
20+
import { resolveJsFrom } from './resolve'
2121
import type { ContextContainer } from './types'
2222

2323
let sourceToPathMap = new Map<string, string | null>()
@@ -104,7 +104,7 @@ async function loadTailwindConfig(
104104
let tailwindConfig: RequiredConfig = { content: [] }
105105

106106
try {
107-
let pkgFile = resolveIn('tailwindcss/package.json', [baseDir])
107+
let pkgFile = resolveJsFrom(baseDir, 'tailwindcss/package.json')
108108
let pkgDir = path.dirname(pkgFile)
109109

110110
try {
@@ -160,7 +160,8 @@ function createLoader<T>({
160160

161161
async function loadFile(id: string, base: string) {
162162
try {
163-
let resolved = resolveFrom(base, id)
163+
let resolved = resolveJsFrom(base, id)
164+
164165
let url = pathToFileURL(resolved)
165166
url.searchParams.append('t', cacheKey)
166167

@@ -180,7 +181,8 @@ async function loadV4(
180181
entryPoint: string | null,
181182
) {
182183
// Import Tailwind — if this is v4 it'll have APIs we can use directly
183-
let pkgPath = resolveIn('tailwindcss', [baseDir])
184+
let pkgPath = resolveJsFrom(baseDir, 'tailwindcss')
185+
184186
let tw = await import(pathToFileURL(pkgPath).toString())
185187

186188
// This is not Tailwind v4

src/resolve.ts

+24-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
import { createRequire as req } from 'node:module'
2-
import resolveFrom from 'resolve-from'
1+
import fs from 'node:fs'
2+
import { fileURLToPath } from 'node:url'
3+
import { CachedInputFileSystem, ResolverFactory } from 'enhanced-resolve'
34
import { expiringMap } from './expiring-map'
45

5-
const localRequire = req(import.meta.url)
6+
const fileSystem = new CachedInputFileSystem(fs, 30_000)
7+
8+
const esmResolver = ResolverFactory.createResolver({
9+
fileSystem,
10+
useSyncFileSystemCalls: true,
11+
extensions: ['.mjs', '.js'],
12+
mainFields: ['module'],
13+
conditionNames: ['node', 'import'],
14+
})
15+
16+
const cjsResolver = ResolverFactory.createResolver({
17+
fileSystem,
18+
useSyncFileSystemCalls: true,
19+
extensions: ['.js', '.cjs'],
20+
mainFields: ['main'],
21+
conditionNames: ['node', 'require'],
22+
})
623

724
// This is a long-lived cache for resolved modules whether they exist or not
825
// Because we're compatible with a large number of plugins, we need to check
@@ -11,17 +28,11 @@ const localRequire = req(import.meta.url)
1128
// failed module resolutions making repeated checks very expensive.
1229
const resolveCache = expiringMap<string, string | null>(30_000)
1330

14-
export function resolveIn(id: string, paths: string[]) {
15-
return localRequire.resolve(id, {
16-
paths,
17-
})
18-
}
19-
2031
export function maybeResolve(name: string) {
2132
let modpath = resolveCache.get(name)
2233

2334
if (modpath === undefined) {
24-
modpath = freshMaybeResolve(name)
35+
modpath = resolveJsFrom(fileURLToPath(import.meta.url), name)
2536
resolveCache.set(name, modpath)
2637
}
2738

@@ -39,12 +50,10 @@ export async function loadIfExists<T>(name: string): Promise<T | null> {
3950
return null
4051
}
4152

42-
function freshMaybeResolve(name: string) {
53+
export function resolveJsFrom(base: string, id: string): string {
4354
try {
44-
return localRequire.resolve(name)
55+
return esmResolver.resolveSync({}, base, id) || id
4556
} catch (err) {
46-
return null
57+
return cjsResolver.resolveSync({}, base, id) || id
4758
}
4859
}
49-
50-
export { resolveFrom }

0 commit comments

Comments
 (0)