Home

tailwind-ctp-intellisense @master - refs - log -
-
https://git.jolheiser.com/tailwind-ctp-intellisense.git
Tailwind intellisense + Catppuccin
tree log patch
merge
Brad Cornes <bradlc41@gmail.com>
4 years ago
5 changed files, 193 additions(+), 105 deletions(-)
packages/tailwindcss-intellisense/package.jsonpackages/tailwindcss-intellisense/src/class-names/environment.jspackages/tailwindcss-intellisense/src/class-names/getPlugins.jspackages/tailwindcss-intellisense/src/class-names/index.jspackages/tailwindcss-intellisense/src/class-names/resolveConfig.js
M packages/tailwindcss-intellisense/package.jsonpackages/tailwindcss-intellisense/package.json
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
diff --git a/packages/tailwindcss-intellisense/package.json b/packages/tailwindcss-intellisense/package.json
index 2d8991006d495334c5abd0e29dd71db034a5eeed..70370d29a649a99efbf78d0f152b0f753031de18 100755
--- a/packages/tailwindcss-intellisense/package.json
+++ b/packages/tailwindcss-intellisense/package.json
@@ -185,6 +185,7 @@     "dlv": "^1.1.3",
     "dset": "^2.0.1",
     "esm": "^3.2.25",
     "fast-glob": "^3.2.4",
+    "find-up": "^5.0.0",
     "glob-exec": "^0.1.1",
     "import-from": "^3.0.0",
     "jest": "^25.5.4",
I packages/tailwindcss-intellisense/src/class-names/environment.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
diff --git a/packages/tailwindcss-intellisense/src/class-names/environment.js b/packages/tailwindcss-intellisense/src/class-names/environment.js
new file mode 100644
index 0000000000000000000000000000000000000000..6364d551ae005289c06af55d771f0b9bb16156dd
--- /dev/null
+++ b/packages/tailwindcss-intellisense/src/class-names/environment.js
@@ -0,0 +1,71 @@
+import * as path from 'path'
+import Module from 'module'
+import findUp from 'find-up'
+import resolveFrom from 'resolve-from'
+import importFrom from 'import-from'
+
+export function withUserEnvironment(base, cb) {
+  const pnpPath = findUp.sync('.pnp.js', { cwd: base })
+  if (pnpPath) {
+    return withPnpEnvironment(pnpPath, cb)
+  }
+  return withNonPnpEnvironment(base, cb)
+}
+
+function withPnpEnvironment(pnpPath, cb) {
+  const basePath = path.dirname(pnpPath)
+
+  // pnp will patch `module` and `fs` to load package in pnp environment
+  // backup the functions which will be patched here
+  const originalModule = Object.create(null)
+  originalModule._load = Module._load
+  originalModule._resolveFilename = Module._resolveFilename
+  originalModule._findPath = Module._findPath
+
+  const pnpapi = __non_webpack_require__(pnpPath)
+
+  // get into pnp environment
+  pnpapi.setup()
+
+  // restore the patched function, we can not load any package after called this
+  const restore = () => Object.assign(Module, originalModule)
+
+  const pnpResolve = (request, from = basePath) => {
+    return pnpapi.resolveRequest(request, from + '/')
+  }
+
+  const pnpRequire = (request, from) => {
+    return __non_webpack_require__(pnpResolve(request, from))
+  }
+
+  const res = cb({ resolve: pnpResolve, require: pnpRequire })
+
+  // check if it return a thenable
+  if (res != null && res.then) {
+    return res.then(
+      (x) => {
+        restore()
+        return x
+      },
+      (err) => {
+        restore()
+        throw err
+      }
+    )
+  }
+
+  restore()
+
+  return res
+}
+
+function withNonPnpEnvironment(base, cb) {
+  return cb({
+    require(request, from = base) {
+      return importFrom(from, request)
+    },
+    resolve(request, from = base) {
+      return resolveFrom(from, request)
+    },
+  })
+}
M packages/tailwindcss-intellisense/src/class-names/getPlugins.jspackages/tailwindcss-intellisense/src/class-names/getPlugins.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
diff --git a/packages/tailwindcss-intellisense/src/class-names/getPlugins.js b/packages/tailwindcss-intellisense/src/class-names/getPlugins.js
index 1fa3e50458f82284ed466a4f27251857d2dc76f1..5f3bf80954a7e610f14c2506b93415fcfb372feb 100644
--- a/packages/tailwindcss-intellisense/src/class-names/getPlugins.js
+++ b/packages/tailwindcss-intellisense/src/class-names/getPlugins.js
@@ -2,22 +2,19 @@ import * as path from 'path'
 import stackTrace from 'stack-trace'
 import pkgUp from 'pkg-up'
 import { isObject } from './isObject'
-import resolveFrom from 'resolve-from'
-import importFrom from 'import-from'
+import { withUserEnvironment } from './environment'
 
 export async function getBuiltInPlugins({ cwd, resolvedConfig }) {
-  const tailwindBase = path.dirname(
-    resolveFrom(cwd, 'tailwindcss/package.json')
-  )
-
-  try {
-    // TODO: add v0 support ("generators")
-    return importFrom(tailwindBase, './lib/corePlugins.js').default({
-      corePlugins: resolvedConfig.corePlugins,
-    })
-  } catch (_) {
-    return []
-  }
+  return withUserEnvironment(cwd, ({ require, resolve }) => {
+    const tailwindBase = path.dirname(resolve('tailwindcss/package.json'))
+    try {
+      return require('./lib/corePlugins.js', tailwindBase).default({
+        corePlugins: resolvedConfig.corePlugins,
+      })
+    } catch (_) {
+      return []
+    }
+  })
 }
 
 export default function getPlugins(config) {
@@ -34,19 +31,12 @@       pluginConfig = {}
     }
 
     let contributes = {
-      theme: isObject(pluginConfig.theme)
-        ? Object.keys(pluginConfig.theme)
-        : [],
-      variants: isObject(pluginConfig.variants)
-        ? Object.keys(pluginConfig.variants)
-        : [],
+      theme: isObject(pluginConfig.theme) ? Object.keys(pluginConfig.theme) : [],
+      variants: isObject(pluginConfig.variants) ? Object.keys(pluginConfig.variants) : [],
     }
 
     const fn = plugin.handler || plugin
-    const fnName =
-      typeof fn.name === 'string' && fn.name !== 'handler' && fn.name !== ''
-        ? fn.name
-        : null
+    const fnName = typeof fn.name === 'string' && fn.name !== 'handler' && fn.name !== '' ? fn.name : null
 
     try {
       fn()
M packages/tailwindcss-intellisense/src/class-names/index.jspackages/tailwindcss-intellisense/src/class-names/index.js
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
diff --git a/packages/tailwindcss-intellisense/src/class-names/index.js b/packages/tailwindcss-intellisense/src/class-names/index.js
index 773faee0927bf1e835a0949695888b6a5200aa63..29a06fd988ff44ec906c1e79f60f2cac7ee00402 100644
--- a/packages/tailwindcss-intellisense/src/class-names/index.js
+++ b/packages/tailwindcss-intellisense/src/class-names/index.js
@@ -2,8 +2,6 @@ import extractClassNames from './extractClassNames'
 import Hook from './hook'
 import dlv from 'dlv'
 import dset from 'dset'
-import resolveFrom from 'resolve-from'
-import importFrom from 'import-from'
 import chokidar from 'chokidar'
 import semver from 'semver'
 import invariant from 'tiny-invariant'
@@ -15,6 +13,7 @@ import * as fs from 'fs'
 import { getUtilityConfigMap } from './getUtilityConfigMap'
 import glob from 'fast-glob'
 import normalizePath from 'normalize-path'
+import { withUserEnvironment } from './environment'
 
 function arraysEqual(arr1, arr2) {
   return (
@@ -31,12 +30,6 @@   cwd = process.cwd(),
   { onChange = () => {} } = {}
 ) {
   async function run() {
-    let postcss
-    let tailwindcss
-    let browserslistModule
-    let version
-    let featureFlags = { future: [], experimental: [] }
-
     const configPaths = (
       await glob(CONFIG_GLOB, {
         cwd,
@@ -53,21 +46,10 @@
     invariant(configPaths.length > 0, 'No Tailwind CSS config found.')
     const configPath = configPaths[0]
     const configDir = path.dirname(configPath)
-    const tailwindBase = path.dirname(
-      resolveFrom(configDir, 'tailwindcss/package.json')
-    )
-    postcss = importFrom(tailwindBase, 'postcss')
-    tailwindcss = importFrom(configDir, 'tailwindcss')
-    version = importFrom(configDir, 'tailwindcss/package.json').version
-
-    try {
-      // this is not required
-      browserslistModule = importFrom(tailwindBase, 'browserslist')
-    } catch (_) {}
-
-    try {
-      featureFlags = importFrom(tailwindBase, './lib/featureFlags.js').default
-    } catch (_) {}
+    const {
+      version,
+      featureFlags = { future: [], experimental: [] },
+    } = loadMeta(configDir)
 
     const sepLocation = semver.gte(version, '0.99.0')
       ? ['separator']
@@ -94,45 +76,68 @@     }
 
     hook.unwatch()
 
-    let postcssResult
+    const {
+      base,
+      components,
+      utilities,
+      resolvedConfig,
+      browserslist,
+      postcss,
+    } = await withPackages(
+      configDir,
+      async ({ postcss, tailwindcss, browserslistModule }) => {
+        let postcssResult
+        try {
+          postcssResult = await Promise.all(
+            [
+              semver.gte(version, '0.99.0') ? 'base' : 'preflight',
+              'components',
+              'utilities',
+            ].map((group) =>
+              postcss([tailwindcss(configPath)]).process(
+                `@tailwind ${group};`,
+                {
+                  from: undefined,
+                }
+              )
+            )
+          )
+        } catch (error) {
+          throw error
+        } finally {
+          hook.unhook()
+        }
 
-    try {
-      postcssResult = await Promise.all(
-        [
-          semver.gte(version, '0.99.0') ? 'base' : 'preflight',
-          'components',
-          'utilities',
-        ].map((group) =>
-          postcss([tailwindcss(configPath)]).process(`@tailwind ${group};`, {
-            from: undefined,
-          })
-        )
-      )
-    } catch (error) {
-      throw error
-    } finally {
-      hook.unhook()
-    }
+        const [base, components, utilities] = postcssResult
 
-    const [base, components, utilities] = postcssResult
+        if (typeof userSeperator !== 'undefined') {
+          dset(config, sepLocation, userSeperator)
+        } else {
+          delete config[sepLocation]
+        }
+        if (typeof userPurge !== 'undefined') {
+          config.purge = userPurge
+        } else {
+          delete config.purge
+        }
 
-    if (typeof userSeperator !== 'undefined') {
-      dset(config, sepLocation, userSeperator)
-    } else {
-      delete config[sepLocation]
-    }
-    if (typeof userPurge !== 'undefined') {
-      config.purge = userPurge
-    } else {
-      delete config.purge
-    }
+        const resolvedConfig = resolveConfig({ cwd: configDir, config })
+        const browserslist = browserslistModule
+          ? browserslistModule(undefined, {
+              path: configDir,
+            })
+          : []
 
-    const resolvedConfig = resolveConfig({ cwd: configDir, config })
-    const browserslist = browserslistModule
-      ? browserslistModule(undefined, {
-          path: configDir,
-        })
-      : []
+        return {
+          base,
+          components,
+          utilities,
+          resolvedConfig,
+          postcss,
+          browserslist,
+        }
+      }
+    )
 
     return {
       version,
@@ -154,7 +159,6 @@         postcss,
         browserslist,
       }),
       modules: {
-        tailwindcss,
         postcss,
       },
       featureFlags,
@@ -193,7 +197,8 @@
   let result
   try {
     result = await run()
-  } catch (_) {
+  } catch (e) {
+    console.log(e)
     return null
   }
 
@@ -201,3 +206,32 @@   watch([result.configPath, ...result.dependencies])
 
   return result
 }
+
+function loadMeta(configDir) {
+  return withUserEnvironment(configDir, ({ require, resolve }) => {
+    const version = require('tailwindcss/package.json').version
+    let featureFlags
+
+    try {
+      const tailwindBase = path.dirname(resolve('tailwindcss/package.json'))
+      featureFlags = require('./lib/featureFlags.js', tailwindBase).default
+    } catch (_) {}
+
+    return { version, featureFlags }
+  })
+}
+
+function withPackages(configDir, cb) {
+  return withUserEnvironment(configDir, async ({ require, resolve }) => {
+    const tailwindBase = path.dirname(resolve('tailwindcss/package.json'))
+    const postcss = require('postcss', tailwindBase)
+    const tailwindcss = require('tailwindcss')
+    let browserslistModule
+    try {
+      // this is not required
+      browserslistModule = require('browserslist', tailwindBase)
+    } catch (_) {}
+
+    return cb({ postcss, tailwindcss, browserslistModule })
+  })
+}
M packages/tailwindcss-intellisense/src/class-names/resolveConfig.jspackages/tailwindcss-intellisense/src/class-names/resolveConfig.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
diff --git a/packages/tailwindcss-intellisense/src/class-names/resolveConfig.js b/packages/tailwindcss-intellisense/src/class-names/resolveConfig.js
index 49a31546ef78ffbbec10b5d55dabb74b798a15e8..6085a00b9366ac34fc3fca64ea9102823e7cd2e3 100644
--- a/packages/tailwindcss-intellisense/src/class-names/resolveConfig.js
+++ b/packages/tailwindcss-intellisense/src/class-names/resolveConfig.js
@@ -1,14 +1,8 @@
-import resolveFrom from 'resolve-from'
-import importFrom from 'import-from'
 import * as path from 'path'
 import decache from './decache'
+import { withUserEnvironment } from './environment'
 
 export default function resolveConfig({ cwd, config }) {
-  const tailwindBase = path.dirname(
-    resolveFrom(cwd, 'tailwindcss/package.json')
-  )
-  let resolve = (x) => x
-
   if (typeof config === 'string') {
     if (!cwd) {
       cwd = path.dirname(config)
@@ -17,21 +11,19 @@     decache(config)
     config = __non_webpack_require__(config)
   }
 
-  try {
-    resolve = importFrom(tailwindBase, './resolveConfig.js')
-  } catch (_) {
+  let resolve = (x) => x
+  withUserEnvironment(cwd, ({ require, resolve }) => {
+    const tailwindBase = path.dirname(resolve('tailwindcss/package.json'))
     try {
-      const resolveConfig = importFrom(
-        tailwindBase,
-        './lib/util/resolveConfig.js'
-      )
-      const defaultConfig = importFrom(
-        tailwindBase,
-        './stubs/defaultConfig.stub.js'
-      )
-      resolve = (config) => resolveConfig([config, defaultConfig])
-    } catch (_) {}
-  }
+      resolve = require('./resolveConfig.js', tailwindBase)
+    } catch (_) {
+      try {
+        const resolveConfig = require('./lib/util/resolveConfig.js', tailwindBase)
+        const defaultConfig = require('./stubs/defaultConfig.stub.js', tailwindBase)
+        resolve = (config) => resolveConfig([config, defaultConfig])
+      } catch (_) {}
+    }
+  })
 
   return resolve(config)
 }