Skip to content

Commit 91970d5

Browse files
Handle negated sources during project discovery (#1288)
I forgot to add support for reading `@source not` in #1284 Also realized I forgot to add the `negated` field for `@source` globs. I'd already written the test — just not tested it with the new version like I did the others 🤦‍♂️
1 parent 5f74714 commit 91970d5

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

packages/tailwindcss-language-server/src/css/extract-source-directives.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import type { Plugin } from 'postcss'
2+
import type { SourcePattern } from '../project-locator'
23

3-
export function extractSourceDirectives(sources: string[]): Plugin {
4+
export function extractSourceDirectives(sources: SourcePattern[]): Plugin {
45
return {
56
postcssPlugin: 'extract-at-rules',
67
AtRule: {
78
source: ({ params }) => {
9+
let negated = /^not\s+/.test(params)
10+
11+
if (negated) params = params.slice(4).trimStart()
12+
813
if (params[0] !== '"' && params[0] !== "'") return
9-
sources.push(params.slice(1, -1))
14+
15+
sources.push({
16+
pattern: params.slice(1, -1),
17+
negated,
18+
})
1019
},
1120
},
1221
}

packages/tailwindcss-language-server/src/project-locator.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,40 @@ testLocator({
265265
],
266266
})
267267

268+
testLocator({
269+
// TODO: Enable once v4.1 is released
270+
options: { skip: true },
271+
name: 'automatic content detection with negative custom sources',
272+
fs: {
273+
'package.json': json`
274+
{
275+
"dependencies": {
276+
"tailwindcss": "0.0.0-insiders.3e53e25",
277+
"@tailwindcss/oxide": "0.0.0-insiders.3e53e25"
278+
}
279+
}
280+
`,
281+
'src/app.css': css`
282+
@import 'tailwindcss';
283+
@source './**/*.html';
284+
@source not './ignored.html';
285+
`,
286+
'src/index.html': html`<div class="underline"></div>`,
287+
'src/ignored.html': html`<div class="flex"></div>`,
288+
},
289+
expected: [
290+
{
291+
config: '/src/app.css',
292+
content: [
293+
'/*',
294+
'/package.json',
295+
'/src/index.html',
296+
'/src/{**/*.html,**/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,json,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}}',
297+
],
298+
},
299+
],
300+
})
301+
268302
testFixture('v4/missing-files', [
269303
//
270304
{

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ async function* contentSelectorsFromCssConfig(
623623
async function* detectContentFiles(
624624
base: string,
625625
inputFile: string,
626-
sources: string[],
626+
sources: SourcePattern[],
627627
resolver: Resolver,
628628
): AsyncIterable<string> {
629629
try {
@@ -636,9 +636,10 @@ async function* detectContentFiles(
636636
oxidePath,
637637
oxideVersion: oxidePackageJson.version,
638638
basePath: base,
639-
sources: sources.map((pattern) => ({
639+
sources: sources.map((source) => ({
640640
base: path.dirname(inputFile),
641-
pattern,
641+
pattern: source.pattern,
642+
negated: source.negated,
642643
})),
643644
})
644645

@@ -672,11 +673,16 @@ type ConfigEntry = {
672673
content: ContentItem[]
673674
}
674675

676+
export interface SourcePattern {
677+
pattern: string
678+
negated: boolean
679+
}
680+
675681
class FileEntry {
676682
content: string | null
677683
deps: FileEntry[] = []
678684
realpath: string | null
679-
sources: string[] = []
685+
sources: SourcePattern[] = []
680686
meta: TailwindStylesheet | null = null
681687

682688
constructor(

0 commit comments

Comments
 (0)