Skip to content

Commit 379e06e

Browse files
committed
feat: Yarn PnP support
1 parent b3fef94 commit 379e06e

File tree

6 files changed

+281
-143
lines changed

6 files changed

+281
-143
lines changed

package-lock.json

Lines changed: 99 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
"dset": "^2.0.1",
190190
"esm": "^3.2.25",
191191
"fast-glob": "^3.2.4",
192+
"find-up": "^5.0.0",
192193
"glob-exec": "^0.1.1",
193194
"globalyzer": "^0.1.4",
194195
"globrex": "^0.1.2",
@@ -216,5 +217,6 @@
216217
"vscode-languageclient": "^5.2.1",
217218
"vscode-languageserver": "^5.2.1",
218219
"vscode-uri": "^2.1.1"
219-
}
220+
},
221+
"dependencies": {}
220222
}

src/class-names/environment.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import * as path from 'path'
2+
import Module from 'module'
3+
import findUp from 'find-up'
4+
import resolveFrom from 'resolve-from'
5+
import importFrom from 'import-from'
6+
7+
export function withUserEnvironment(base, cb) {
8+
const pnpPath = findUp.sync('.pnp.js', { cwd: base })
9+
if (pnpPath) {
10+
return withPnpEnvironment(pnpPath, cb)
11+
}
12+
return withNonPnpEnvironment(base, cb)
13+
}
14+
15+
function withPnpEnvironment(pnpPath, cb) {
16+
const basePath = path.dirname(pnpPath)
17+
18+
// pnp will patch `module` and `fs` to load package in pnp environment
19+
// backup the functions which will be patched here
20+
const originalModule = Object.create(null)
21+
originalModule._load = Module._load
22+
originalModule._resolveFilename = Module._resolveFilename
23+
originalModule._findPath = Module._findPath
24+
25+
const pnpapi = __non_webpack_require__(pnpPath)
26+
27+
// get into pnp environment
28+
pnpapi.setup()
29+
30+
// restore the patched function, we can not load any package after called this
31+
const restore = () => Object.assign(Module, originalModule)
32+
33+
const pnpResolve = (request, from = basePath) => {
34+
return pnpapi.resolveRequest(request, from + '/')
35+
}
36+
37+
const pnpRequire = (request, from) => {
38+
return __non_webpack_require__(pnpResolve(request, from))
39+
}
40+
41+
const res = cb({ resolve: pnpResolve, require: pnpRequire })
42+
43+
// check if it return a thenable
44+
if (res != null && res.then) {
45+
return res.then(
46+
(x) => {
47+
restore()
48+
return x
49+
},
50+
(err) => {
51+
restore()
52+
throw err
53+
}
54+
)
55+
}
56+
57+
restore()
58+
59+
return res
60+
}
61+
62+
function withNonPnpEnvironment(base, cb) {
63+
return cb({
64+
require(request, from = base) {
65+
return importFrom(from, request)
66+
},
67+
resolve(request, from = base) {
68+
return resolveFrom(from, request)
69+
},
70+
})
71+
}

src/class-names/getPlugins.js

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@ import * as path from 'path'
22
import stackTrace from 'stack-trace'
33
import pkgUp from 'pkg-up'
44
import { isObject } from './isObject'
5-
import resolveFrom from 'resolve-from'
6-
import importFrom from 'import-from'
5+
import { withUserEnvironment } from './environment'
76

87
export async function getBuiltInPlugins({ cwd, resolvedConfig }) {
9-
const tailwindBase = path.dirname(
10-
resolveFrom(cwd, 'tailwindcss/package.json')
11-
)
12-
13-
try {
14-
// TODO: add v0 support ("generators")
15-
return importFrom(tailwindBase, './lib/corePlugins.js').default({
16-
corePlugins: resolvedConfig.corePlugins,
17-
})
18-
} catch (_) {
19-
return []
20-
}
8+
return withUserEnvironment(cwd, ({ require }) => {
9+
try {
10+
return require('tailwindcss/lib/corePlugins.js').default({
11+
corePlugins: resolvedConfig.corePlugins,
12+
})
13+
} catch (_) {
14+
return []
15+
}
16+
})
2117
}
2218

2319
export default function getPlugins(config) {
@@ -34,19 +30,12 @@ export default function getPlugins(config) {
3430
}
3531

3632
let contributes = {
37-
theme: isObject(pluginConfig.theme)
38-
? Object.keys(pluginConfig.theme)
39-
: [],
40-
variants: isObject(pluginConfig.variants)
41-
? Object.keys(pluginConfig.variants)
42-
: [],
33+
theme: isObject(pluginConfig.theme) ? Object.keys(pluginConfig.theme) : [],
34+
variants: isObject(pluginConfig.variants) ? Object.keys(pluginConfig.variants) : [],
4335
}
4436

4537
const fn = plugin.handler || plugin
46-
const fnName =
47-
typeof fn.name === 'string' && fn.name !== 'handler' && fn.name !== ''
48-
? fn.name
49-
: null
38+
const fnName = typeof fn.name === 'string' && fn.name !== 'handler' && fn.name !== '' ? fn.name : null
5039

5140
try {
5241
fn()

0 commit comments

Comments
 (0)