Skip to content

Commit e963d68

Browse files
committed
fix pnp config module resolution
1 parent 4b10581 commit e963d68

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

packages/tailwindcss-intellisense/src/class-names/environment.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
import * as path from 'path'
2-
import Module from 'module'
32
import findUp from 'find-up'
43
import resolveFrom from 'resolve-from'
54
import importFrom from 'import-from'
65

6+
let isPnp
7+
let pnpApi
8+
79
export function withUserEnvironment(base, root, cb) {
10+
if (isPnp === true) {
11+
return withPnpEnvironment(base, cb)
12+
}
13+
14+
if (isPnp === false) {
15+
return withNonPnpEnvironment(base, cb)
16+
}
17+
818
const pnpPath = findUp.sync(
919
(dir) => {
10-
const pnpFile = path.join(dir, '.pnp.js')
20+
let pnpFile = path.join(dir, '.pnp.js')
21+
if (findUp.sync.exists(pnpFile)) {
22+
return pnpFile
23+
}
24+
pnpFile = path.join(dir, '.pnp.cjs')
1125
if (findUp.sync.exists(pnpFile)) {
1226
return pnpFile
1327
}
@@ -17,62 +31,47 @@ export function withUserEnvironment(base, root, cb) {
1731
},
1832
{ cwd: base }
1933
)
34+
2035
if (pnpPath) {
21-
return withPnpEnvironment(pnpPath, cb)
36+
isPnp = true
37+
pnpApi = __non_webpack_require__(pnpPath)
38+
pnpApi.setup()
39+
} else {
40+
isPnp = false
2241
}
23-
return withNonPnpEnvironment(base, cb)
24-
}
25-
26-
function withPnpEnvironment(pnpPath, cb) {
27-
const basePath = path.dirname(pnpPath)
28-
29-
// pnp will patch `module` and `fs` to load package in pnp environment
30-
// backup the functions which will be patched here
31-
const originalModule = Object.create(null)
32-
originalModule._load = Module._load
33-
originalModule._resolveFilename = Module._resolveFilename
34-
originalModule._findPath = Module._findPath
3542

36-
const pnpapi = __non_webpack_require__(pnpPath)
37-
38-
// get into pnp environment
39-
pnpapi.setup()
40-
41-
// restore the patched function, we can not load any package after called this
42-
const restore = () => Object.assign(Module, originalModule)
43+
return withUserEnvironment(base, root, cb)
44+
}
4345

44-
const pnpResolve = (request, from = basePath) => {
45-
return pnpapi.resolveRequest(request, from + '/')
46+
function withPnpEnvironment(base, cb) {
47+
const pnpResolve = (request, from = base) => {
48+
return pnpApi.resolveRequest(request, from.replace(/\/$/, '') + '/')
4649
}
4750

4851
const pnpRequire = (request, from) => {
4952
return __non_webpack_require__(pnpResolve(request, from))
5053
}
5154

52-
const res = cb({ isPnP: true, resolve: pnpResolve, require: pnpRequire })
55+
const res = cb({ isPnp: true, resolve: pnpResolve, require: pnpRequire })
5356

5457
// check if it return a thenable
5558
if (res != null && res.then) {
5659
return res.then(
5760
(x) => {
58-
restore()
5961
return x
6062
},
6163
(err) => {
62-
restore()
6364
throw err
6465
}
6566
)
6667
}
6768

68-
restore()
69-
7069
return res
7170
}
7271

7372
function withNonPnpEnvironment(base, cb) {
7473
return cb({
75-
isPnP: false,
74+
isPnp: false,
7675
require(request, from = base) {
7776
return importFrom(from, request)
7877
},

packages/tailwindcss-intellisense/src/class-names/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ function withPackages(configDir, root, cb) {
259259
return withUserEnvironment(
260260
configDir,
261261
root,
262-
async ({ isPnP, require, resolve }) => {
262+
async ({ isPnp, require, resolve }) => {
263263
const tailwindBase = path.dirname(resolve('tailwindcss/package.json'))
264264
const postcss = require('postcss', tailwindBase)
265265
const tailwindcss = require('tailwindcss')
@@ -274,7 +274,7 @@ function withPackages(configDir, root, cb) {
274274
),
275275
tailwindBase
276276
)
277-
if (isPnP) {
277+
if (isPnp) {
278278
browserslistCommand = 'yarn'
279279
browserslistArgs = ['node', browserslistBin]
280280
} else {

0 commit comments

Comments
 (0)