Skip to content

Commit 15d387e

Browse files
author
Brad Cornes
committed
show config errors
1 parent 568e078 commit 15d387e

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ import invariant from 'tiny-invariant'
1111
import getPlugins from './getPlugins'
1212
import getVariants from './getVariants'
1313
import resolveConfig from './resolveConfig'
14+
import * as util from 'util'
15+
16+
function TailwindConfigError(error) {
17+
Error.call(this)
18+
Error.captureStackTrace(this, this.constructor)
19+
20+
this.name = this.constructor.name
21+
this.message = error.message
22+
this.stack = error.stack
23+
}
24+
25+
util.inherits(TailwindConfigError, Error)
1426

1527
function glob(pattern, options = {}) {
1628
return new Promise((resolve, reject) => {
@@ -73,7 +85,12 @@ export default async function getClassNames(
7385
})
7486

7587
hook.watch()
76-
const config = __non_webpack_require__(configPath)
88+
let config
89+
try {
90+
config = __non_webpack_require__(configPath)
91+
} catch (error) {
92+
throw new TailwindConfigError(error)
93+
}
7794
hook.unwatch()
7895

7996
const ast = await postcss([tailwindcss(configPath)]).process(
@@ -116,8 +133,12 @@ export default async function getClassNames(
116133
const prevDeps = result ? result.dependencies : []
117134
try {
118135
result = await run()
119-
} catch (_) {
120-
onChange(null)
136+
} catch (error) {
137+
if (error instanceof TailwindConfigError) {
138+
onChange({ error })
139+
} else {
140+
onChange(null)
141+
}
121142
return
122143
}
123144
if (!arraysEqual(prevDeps, result.dependencies)) {

packages/tailwindcss-language-server/src/server.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ connection.onInitialize(
6363
params.rootPath || URI.parse(params.rootUri).path,
6464
{
6565
onChange: (newState: State): void => {
66-
if (newState) {
66+
if (newState && !newState.error) {
6767
state = { ...newState, enabled: true, editor: editorState }
6868
connection.sendNotification('tailwindcss/configUpdated', [
6969
state.configPath,
@@ -72,6 +72,20 @@ connection.onInitialize(
7272
])
7373
} else {
7474
state = { enabled: false, editor: editorState }
75+
if (newState && newState.error) {
76+
const payload: {
77+
message: string
78+
file?: string
79+
line?: number
80+
} = { message: newState.error.message }
81+
const lines = newState.error.stack.toString().split('\n')
82+
const match = /^(?<file>.*?):(?<line>[0-9]+)$/.exec(lines[0])
83+
if (match) {
84+
payload.file = match.groups.file
85+
payload.line = parseInt(match.groups.line, 10)
86+
}
87+
connection.sendNotification('tailwindcss/configError', [payload])
88+
}
7589
// TODO
7690
// connection.sendNotification('tailwindcss/configUpdated', [null])
7791
}

packages/tailwindcss-language-server/src/util/state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export type State = null | {
3737
classNames?: ClassNames
3838
dependencies?: string[]
3939
editor?: EditorState
40+
error?: Error
4041
}
4142

4243
export type DocumentClassList = {

packages/tailwindcss-vscode/src/extension.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import {
1212
WorkspaceFolder,
1313
Uri,
1414
} from 'vscode'
15-
1615
import {
1716
LanguageClient,
1817
LanguageClientOptions,
1918
TransportKind,
2019
} from 'vscode-languageclient'
20+
import { registerConfigErrorHandler } from './lib/registerConfigErrorHandler'
2121

2222
let defaultClient: LanguageClient
2323
let clients: Map<string, LanguageClient> = new Map()
@@ -138,6 +138,10 @@ export function activate(context: ExtensionContext) {
138138
clientOptions
139139
)
140140

141+
client.onReady().then(() => {
142+
registerConfigErrorHandler(client)
143+
})
144+
141145
client.start()
142146
clients.set(folder.uri.toString(), client)
143147
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { LanguageClient } from 'vscode-languageclient'
2+
import { window, Uri, Range, Position } from 'vscode'
3+
4+
export function registerConfigErrorHandler(client: LanguageClient) {
5+
client.onNotification(
6+
'tailwindcss/configError',
7+
async ({ message, file, line }) => {
8+
const actions: string[] = file ? ['View'] : []
9+
const action = await window.showErrorMessage(
10+
`Tailwind CSS: ${message}`,
11+
...actions
12+
)
13+
if (action === 'View') {
14+
window.showTextDocument(Uri.file(file), {
15+
selection: new Range(
16+
new Position(line - 1, 0),
17+
new Position(line - 1, 0)
18+
),
19+
})
20+
}
21+
}
22+
)
23+
}

0 commit comments

Comments
 (0)