Skip to content

Commit 2b1b7d5

Browse files
committed
Load Tailwind CSS through require() when using Yarn PnP
It does not intercept calls to `await import()`. Manually registering the ESM hook also results in failed fs.stat calls.
1 parent 36d8bb7 commit 2b1b7d5

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

packages/tailwindcss-language-server/src/projects.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,16 +442,24 @@ export async function createProjectService(
442442
let applyComplexClasses: any
443443

444444
try {
445-
let tailwindcssPath = await resolver.resolveJsId('tailwindcss', configDir)
446-
let tailwindcssPkgPath = await resolver.resolveJsId('tailwindcss/package.json', configDir)
445+
let tailwindcssPkgPath = await resolver.resolveCjsId('tailwindcss/package.json', configDir)
447446
let tailwindDir = path.dirname(tailwindcssPkgPath)
448447
tailwindcssVersion = require(tailwindcssPkgPath).version
449448

450449
let features = supportedFeatures(tailwindcssVersion)
451450
log(`supported features: ${JSON.stringify(features)}`)
452451

453-
tailwindcssPath = pathToFileURL(tailwindcssPath).href
454-
tailwindcss = await import(tailwindcssPath)
452+
// Loading via `await import(…)` with the Yarn PnP API is not possible
453+
if (await resolver.hasPnP()) {
454+
let tailwindcssPath = await resolver.resolveCjsId('tailwindcss', configDir)
455+
456+
tailwindcss = require(tailwindcssPath)
457+
} else {
458+
let tailwindcssPath = await resolver.resolveJsId('tailwindcss', configDir)
459+
let tailwindcssURL = pathToFileURL(tailwindcssPath).href
460+
461+
tailwindcss = await import(tailwindcssURL)
462+
}
455463

456464
if (!features.includes('css-at-theme')) {
457465
tailwindcss = tailwindcss.default ?? tailwindcss

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ export interface Resolver {
5555
*/
5656
resolveJsId(id: string, base: string): Promise<string>
5757

58+
/**
59+
* Resolves a CJS module to a file path.
60+
*
61+
* Assumes ESM-captable mechanisms are not available.
62+
*
63+
* @param id The module or file to resolve
64+
* @param base The base directory to resolve the module from
65+
*/
66+
resolveCjsId(id: string, base: string): Promise<string>
67+
5868
/**
5969
* Resolves a CSS module to a file path.
6070
*
@@ -204,6 +214,10 @@ export async function createResolver(opts: ResolverOptions): Promise<Resolver> {
204214
}
205215
}
206216

217+
async function resolveCjsId(id: string, base: string): Promise<string> {
218+
return (await resolveId(cjsResolver, id, base)) || id
219+
}
220+
207221
async function resolveCssId(id: string, base: string): Promise<string> {
208222
return (await resolveId(cssResolver, id, base)) || id
209223
}
@@ -230,6 +244,7 @@ export async function createResolver(opts: ResolverOptions): Promise<Resolver> {
230244

231245
return {
232246
resolveJsId,
247+
resolveCjsId,
233248
resolveCssId,
234249
substituteId,
235250
refresh,

packages/tailwindcss-language-server/src/tw.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import normalizePath from 'normalize-path'
3636
import * as path from 'node:path'
3737
import type * as chokidar from 'chokidar'
3838
import picomatch from 'picomatch'
39-
import { resolveFrom } from './util/resolveFrom'
4039
import * as parcel from './watcher/index.js'
4140
import { equal } from '@tailwindcss/language-service/src/util/array'
4241
import { CONFIG_GLOB, CSS_GLOB, PACKAGE_LOCK_GLOB, TSCONFIG_GLOB } from './lib/constants'
@@ -321,9 +320,9 @@ export class TW {
321320
let twVersion = require('tailwindcss/package.json').version
322321
try {
323322
let v = require(
324-
resolveFrom(
325-
path.dirname(project.projectConfig.configPath),
323+
await resolver.resolveCjsId(
326324
'tailwindcss/package.json',
325+
path.dirname(project.projectConfig.configPath),
327326
),
328327
).version
329328
if (typeof v === 'string') {

0 commit comments

Comments
 (0)