Skip to content

Handle negated sources during project discovery #1288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import type { Plugin } from 'postcss'
import type { SourcePattern } from '../project-locator'

export function extractSourceDirectives(sources: string[]): Plugin {
export function extractSourceDirectives(sources: SourcePattern[]): Plugin {
return {
postcssPlugin: 'extract-at-rules',
AtRule: {
source: ({ params }) => {
let negated = /^not\s+/.test(params)

if (negated) params = params.slice(4).trimStart()

if (params[0] !== '"' && params[0] !== "'") return
sources.push(params.slice(1, -1))

sources.push({
pattern: params.slice(1, -1),
negated,
})
},
},
}
Expand Down
34 changes: 34 additions & 0 deletions packages/tailwindcss-language-server/src/project-locator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,40 @@ testLocator({
],
})

testLocator({
// TODO: Enable once v4.1 is released
options: { skip: true },
name: 'automatic content detection with negative custom sources',
fs: {
'package.json': json`
{
"dependencies": {
"tailwindcss": "0.0.0-insiders.3e53e25",
"@tailwindcss/oxide": "0.0.0-insiders.3e53e25"
}
}
`,
'src/app.css': css`
@import 'tailwindcss';
@source './**/*.html';
@source not './ignored.html';
`,
'src/index.html': html`<div class="underline"></div>`,
'src/ignored.html': html`<div class="flex"></div>`,
},
expected: [
{
config: '/src/app.css',
content: [
'/*',
'/package.json',
'/src/index.html',
'/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}}',
],
},
],
})

testFixture('v4/missing-files', [
//
{
Expand Down
14 changes: 10 additions & 4 deletions packages/tailwindcss-language-server/src/project-locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ async function* contentSelectorsFromCssConfig(
async function* detectContentFiles(
base: string,
inputFile: string,
sources: string[],
sources: SourcePattern[],
resolver: Resolver,
): AsyncIterable<string> {
try {
Expand All @@ -636,9 +636,10 @@ async function* detectContentFiles(
oxidePath,
oxideVersion: oxidePackageJson.version,
basePath: base,
sources: sources.map((pattern) => ({
sources: sources.map((source) => ({
base: path.dirname(inputFile),
pattern,
pattern: source.pattern,
negated: source.negated,
})),
})

Expand Down Expand Up @@ -672,11 +673,16 @@ type ConfigEntry = {
content: ContentItem[]
}

export interface SourcePattern {
pattern: string
negated: boolean
}

class FileEntry {
content: string | null
deps: FileEntry[] = []
realpath: string | null
sources: string[] = []
sources: SourcePattern[] = []
meta: TailwindStylesheet | null = null

constructor(
Expand Down