Skip to content

Commit

Permalink
fix for canary releases
Browse files Browse the repository at this point in the history
  • Loading branch information
bradlc committed Jun 4, 2021
1 parent 52ae09b commit cf7bb68
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions packages/tailwindcss-language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import { getDocumentColors } from 'tailwindcss-language-service/src/documentColo
import { fromRatio, names as namedColors } from '@ctrl/tinycolor'
import { debounce } from 'debounce'
import { getModuleDependencies } from './util/getModuleDependencies'
import assert from 'assert'
// import postcssLoadConfig from 'postcss-load-config'

const CONFIG_FILE_GLOB = '{tailwind,tailwind.config}.{js,cjs}'
Expand Down Expand Up @@ -132,6 +133,19 @@ function getConfigId(configPath: string, configDependencies: string[]): string {
)
}

function first<T>(...options: Array<() => T>): T {
for (let i = 0; i < options.length; i++) {
let option = options[i]
if (i === options.length - 1) {
return option()
} else {
try {
return option()
} catch (_) {}
}
}
}

interface ProjectService {
state: State
tryInit: () => Promise<void>
Expand Down Expand Up @@ -442,23 +456,38 @@ async function createProjectService(
let tailwindDirectives = new Set()
let root = postcss.root()
let result = { opts: {}, messages: [] }
let registerDependency = () => {}

try {
let createContext

try {
let createContextFn = __non_webpack_require__(
resolveFrom(configDir, 'tailwindcss/lib/jit/lib/setupContextUtils')
).createContext
createContext = (state) => createContextFn(state.config)
} catch (_) {
// TODO: only for canary releases so can probably remove
let setupContext = __non_webpack_require__(
resolveFrom(configDir, 'tailwindcss/lib/jit/lib/setupContext')
).default
createContext = (state) =>
setupContext(state.configPath, tailwindDirectives)(result, root)
}
let createContext = first(
() => {
let createContextFn = __non_webpack_require__(
resolveFrom(configDir, 'tailwindcss/lib/jit/lib/setupContextUtils')
).createContext
assert.strictEqual(typeof createContextFn, 'function')
return (state) => createContextFn(state.config)
},
// TODO: the next two are canary releases only so can probably be removed
() => {
let setupTrackingContext = __non_webpack_require__(
resolveFrom(configDir, 'tailwindcss/lib/jit/lib/setupTrackingContext')
).default
assert.strictEqual(typeof setupTrackingContext, 'function')
return (state) =>
setupTrackingContext(
state.configPath,
tailwindDirectives,
registerDependency
)(result, root)
},
() => {
let setupContext = __non_webpack_require__(
resolveFrom(configDir, 'tailwindcss/lib/jit/lib/setupContext')
).default
assert.strictEqual(typeof setupContext, 'function')
return (state) => setupContext(state.configPath, tailwindDirectives)(result, root)
}
)

jitModules = {
generateRules: {
Expand Down

0 comments on commit cf7bb68

Please sign in to comment.