Skip to content

Commit 321098d

Browse files
committed
merge
2 parents 99febcd + 7ba8b4d commit 321098d

File tree

5 files changed

+197
-109
lines changed

5 files changed

+197
-109
lines changed

packages/tailwindcss-intellisense/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
"dset": "^2.0.1",
186186
"esm": "^3.2.25",
187187
"fast-glob": "^3.2.4",
188+
"find-up": "^5.0.0",
188189
"glob-exec": "^0.1.1",
189190
"import-from": "^3.0.0",
190191
"jest": "^25.5.4",
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+
}

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

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@ 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, resolve }) => {
9+
const tailwindBase = path.dirname(resolve('tailwindcss/package.json'))
10+
try {
11+
return require('./lib/corePlugins.js', tailwindBase).default({
12+
corePlugins: resolvedConfig.corePlugins,
13+
})
14+
} catch (_) {
15+
return []
16+
}
17+
})
2118
}
2219

2320
export default function getPlugins(config) {
@@ -34,19 +31,12 @@ export default function getPlugins(config) {
3431
}
3532

3633
let contributes = {
37-
theme: isObject(pluginConfig.theme)
38-
? Object.keys(pluginConfig.theme)
39-
: [],
40-
variants: isObject(pluginConfig.variants)
41-
? Object.keys(pluginConfig.variants)
42-
: [],
34+
theme: isObject(pluginConfig.theme) ? Object.keys(pluginConfig.theme) : [],
35+
variants: isObject(pluginConfig.variants) ? Object.keys(pluginConfig.variants) : [],
4336
}
4437

4538
const fn = plugin.handler || plugin
46-
const fnName =
47-
typeof fn.name === 'string' && fn.name !== 'handler' && fn.name !== ''
48-
? fn.name
49-
: null
39+
const fnName = typeof fn.name === 'string' && fn.name !== 'handler' && fn.name !== '' ? fn.name : null
5040

5141
try {
5242
fn()

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

Lines changed: 98 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import extractClassNames from './extractClassNames'
22
import Hook from './hook'
33
import dlv from 'dlv'
44
import dset from 'dset'
5-
import resolveFrom from 'resolve-from'
6-
import importFrom from 'import-from'
75
import chokidar from 'chokidar'
86
import semver from 'semver'
97
import invariant from 'tiny-invariant'
@@ -15,6 +13,7 @@ import * as fs from 'fs'
1513
import { getUtilityConfigMap } from './getUtilityConfigMap'
1614
import glob from 'fast-glob'
1715
import normalizePath from 'normalize-path'
16+
import { withUserEnvironment } from './environment'
1817

1918
function arraysEqual(arr1, arr2) {
2019
return (
@@ -31,12 +30,6 @@ export default async function getClassNames(
3130
{ onChange = () => {} } = {}
3231
) {
3332
async function run() {
34-
let postcss
35-
let tailwindcss
36-
let browserslistModule
37-
let version
38-
let featureFlags = { future: [], experimental: [] }
39-
4033
const configPaths = (
4134
await glob(CONFIG_GLOB, {
4235
cwd,
@@ -53,21 +46,10 @@ export default async function getClassNames(
5346
invariant(configPaths.length > 0, 'No Tailwind CSS config found.')
5447
const configPath = configPaths[0]
5548
const configDir = path.dirname(configPath)
56-
const tailwindBase = path.dirname(
57-
resolveFrom(configDir, 'tailwindcss/package.json')
58-
)
59-
postcss = importFrom(tailwindBase, 'postcss')
60-
tailwindcss = importFrom(configDir, 'tailwindcss')
61-
version = importFrom(configDir, 'tailwindcss/package.json').version
62-
63-
try {
64-
// this is not required
65-
browserslistModule = importFrom(tailwindBase, 'browserslist')
66-
} catch (_) {}
67-
68-
try {
69-
featureFlags = importFrom(tailwindBase, './lib/featureFlags.js').default
70-
} catch (_) {}
49+
const {
50+
version,
51+
featureFlags = { future: [], experimental: [] },
52+
} = loadMeta(configDir)
7153

7254
const sepLocation = semver.gte(version, '0.99.0')
7355
? ['separator']
@@ -94,45 +76,68 @@ export default async function getClassNames(
9476

9577
hook.unwatch()
9678

97-
let postcssResult
98-
99-
try {
100-
postcssResult = await Promise.all(
101-
[
102-
semver.gte(version, '0.99.0') ? 'base' : 'preflight',
103-
'components',
104-
'utilities',
105-
].map((group) =>
106-
postcss([tailwindcss(configPath)]).process(`@tailwind ${group};`, {
107-
from: undefined,
108-
})
109-
)
110-
)
111-
} catch (error) {
112-
throw error
113-
} finally {
114-
hook.unhook()
115-
}
116-
117-
const [base, components, utilities] = postcssResult
118-
119-
if (typeof userSeperator !== 'undefined') {
120-
dset(config, sepLocation, userSeperator)
121-
} else {
122-
delete config[sepLocation]
123-
}
124-
if (typeof userPurge !== 'undefined') {
125-
config.purge = userPurge
126-
} else {
127-
delete config.purge
128-
}
129-
130-
const resolvedConfig = resolveConfig({ cwd: configDir, config })
131-
const browserslist = browserslistModule
132-
? browserslistModule(undefined, {
133-
path: configDir,
134-
})
135-
: []
79+
const {
80+
base,
81+
components,
82+
utilities,
83+
resolvedConfig,
84+
browserslist,
85+
postcss,
86+
} = await withPackages(
87+
configDir,
88+
async ({ postcss, tailwindcss, browserslistModule }) => {
89+
let postcssResult
90+
try {
91+
postcssResult = await Promise.all(
92+
[
93+
semver.gte(version, '0.99.0') ? 'base' : 'preflight',
94+
'components',
95+
'utilities',
96+
].map((group) =>
97+
postcss([tailwindcss(configPath)]).process(
98+
`@tailwind ${group};`,
99+
{
100+
from: undefined,
101+
}
102+
)
103+
)
104+
)
105+
} catch (error) {
106+
throw error
107+
} finally {
108+
hook.unhook()
109+
}
110+
111+
const [base, components, utilities] = postcssResult
112+
113+
if (typeof userSeperator !== 'undefined') {
114+
dset(config, sepLocation, userSeperator)
115+
} else {
116+
delete config[sepLocation]
117+
}
118+
if (typeof userPurge !== 'undefined') {
119+
config.purge = userPurge
120+
} else {
121+
delete config.purge
122+
}
123+
124+
const resolvedConfig = resolveConfig({ cwd: configDir, config })
125+
const browserslist = browserslistModule
126+
? browserslistModule(undefined, {
127+
path: configDir,
128+
})
129+
: []
130+
131+
return {
132+
base,
133+
components,
134+
utilities,
135+
resolvedConfig,
136+
postcss,
137+
browserslist,
138+
}
139+
}
140+
)
136141

137142
return {
138143
version,
@@ -154,7 +159,6 @@ export default async function getClassNames(
154159
browserslist,
155160
}),
156161
modules: {
157-
tailwindcss,
158162
postcss,
159163
},
160164
featureFlags,
@@ -193,11 +197,41 @@ export default async function getClassNames(
193197
let result
194198
try {
195199
result = await run()
196-
} catch (_) {
200+
} catch (e) {
201+
console.log(e)
197202
return null
198203
}
199204

200205
watch([result.configPath, ...result.dependencies])
201206

202207
return result
203208
}
209+
210+
function loadMeta(configDir) {
211+
return withUserEnvironment(configDir, ({ require, resolve }) => {
212+
const version = require('tailwindcss/package.json').version
213+
let featureFlags
214+
215+
try {
216+
const tailwindBase = path.dirname(resolve('tailwindcss/package.json'))
217+
featureFlags = require('./lib/featureFlags.js', tailwindBase).default
218+
} catch (_) {}
219+
220+
return { version, featureFlags }
221+
})
222+
}
223+
224+
function withPackages(configDir, cb) {
225+
return withUserEnvironment(configDir, async ({ require, resolve }) => {
226+
const tailwindBase = path.dirname(resolve('tailwindcss/package.json'))
227+
const postcss = require('postcss', tailwindBase)
228+
const tailwindcss = require('tailwindcss')
229+
let browserslistModule
230+
try {
231+
// this is not required
232+
browserslistModule = require('browserslist', tailwindBase)
233+
} catch (_) {}
234+
235+
return cb({ postcss, tailwindcss, browserslistModule })
236+
})
237+
}

0 commit comments

Comments
 (0)