Skip to content

Commit 36d8bb7

Browse files
committed
Setup PnP runtime API when available
A call to `setup()` is required for resolution to function at all
1 parent 70a7aaa commit 36d8bb7

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

packages/tailwindcss-language-server/src/resolver/index.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,6 @@ export interface ResolverOptions {
4343
}
4444

4545
export interface Resolver {
46-
/**
47-
* Sets up the PnP API if it is available such that globals like `require`
48-
* have been monkey-patched to use PnP resolution.
49-
*
50-
* This function does nothing if PnP resolution is not enabled or if the PnP
51-
* API is not available.
52-
*/
53-
setupPnP(): Promise<void>
54-
5546
/**
5647
* Resolves a JavaScript module to a file path.
5748
*
@@ -98,6 +89,11 @@ export interface Resolver {
9889
*/
9990
child(opts: Partial<ResolverOptions>): Promise<Resolver>
10091

92+
/**
93+
* Whether or not the PnP API is being used by the resolver
94+
*/
95+
hasPnP(): Promise<boolean>
96+
10197
/**
10298
* Refresh information the resolver may have cached
10399
*
@@ -107,17 +103,18 @@ export interface Resolver {
107103
}
108104

109105
export async function createResolver(opts: ResolverOptions): Promise<Resolver> {
110-
let fileSystem = opts.fileSystem ? opts.fileSystem : new CachedInputFileSystem(fs, 4000)
111-
112106
let pnpApi: PnpApi | null = null
113107

114108
// Load PnP API if requested
109+
// This MUST be done before `CachedInputFileSystem` is created
115110
if (typeof opts.pnp === 'object') {
116111
pnpApi = opts.pnp
117112
} else if (opts.pnp) {
118113
pnpApi = await loadPnPApi(opts.root)
119114
}
120115

116+
let fileSystem = opts.fileSystem ? opts.fileSystem : new CachedInputFileSystem(fs, 4000)
117+
121118
let tsconfig: TSConfigApi | null = null
122119

123120
// Load TSConfig path mappings
@@ -217,10 +214,6 @@ export async function createResolver(opts: ResolverOptions): Promise<Resolver> {
217214
return (await tsconfig?.substituteId(id, base)) ?? id
218215
}
219216

220-
async function setupPnP() {
221-
pnpApi?.setup()
222-
}
223-
224217
async function aliases(base: string) {
225218
if (!tsconfig) return {}
226219

@@ -231,12 +224,16 @@ export async function createResolver(opts: ResolverOptions): Promise<Resolver> {
231224
await tsconfig?.refresh()
232225
}
233226

227+
async function hasPnP() {
228+
return !!pnpApi
229+
}
230+
234231
return {
235-
setupPnP,
236232
resolveJsId,
237233
resolveCssId,
238234
substituteId,
239235
refresh,
236+
hasPnP,
240237

241238
aliases,
242239

packages/tailwindcss-language-server/src/resolver/pnp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as path from 'node:path'
33
import { pathToFileURL } from '../utils'
44

55
export interface PnpApi {
6-
setup(): void
76
resolveToUnqualified: (arg0: string, arg1: string, arg2: object) => null | string
87
}
98

@@ -29,6 +28,7 @@ export async function loadPnPApi(root: string): Promise<PnpApi | null> {
2928
let pnpUrl = pathToFileURL(pnpPath).href
3029
let mod = await import(pnpUrl)
3130
let api = mod.default
31+
api.setup()
3232
cache.set(root, api)
3333
return api
3434
}

0 commit comments

Comments
 (0)