Skip to content

Commit daacc5e

Browse files
Fix Astro by adding the componentns folder instead of falling to full fs scanning
1 parent f4a3c61 commit daacc5e

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

integrations/vite/astro.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ test(
9292
// https://astro.build/config
9393
export default defineConfig({ vite: { plugins: [tailwindcss()] }, integrations: [react()] })
9494
`,
95+
// prettier-ignore
9596
'src/pages/index.astro': html`
9697
---
97-
import ClientOnly from './client-only';
98+
import ClientOnly from '../components/client-only';
9899
---
99100
100101
<div class="underline">Hello, world!</div>
@@ -105,7 +106,7 @@ test(
105106
@import 'tailwindcss';
106107
</style>
107108
`,
108-
'src/pages/client-only.jsx': js`
109+
'src/components/client-only.jsx': js`
109110
export default function ClientOnly() {
110111
return <div className="overline">Hello, world!</div>
111112
}

packages/@tailwindcss-vite/src/index.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ const INLINE_STYLE_ID_RE = /[?&]index\=\d+\.css$/
1313

1414
const IGNORED_DEPENDENCIES = ['tailwind-merge']
1515

16-
type ScannerMode = 'automatic' | 'module-graph' | 'file-system'
16+
type ScannerMode = 'module-graph' | 'file-system'
1717

1818
export default function tailwindcss(
19-
{ scanner: scannerMode = 'automatic' }: { scanner: ScannerMode } = {
20-
scanner: 'automatic',
19+
{ scanner: scannerMode = 'module-graph' }: { scanner: ScannerMode } = {
20+
scanner: 'module-graph',
2121
},
2222
): Plugin[] {
2323
let servers: ViteDevServer[] = []
@@ -26,6 +26,8 @@ export default function tailwindcss(
2626
let isSSR = false
2727
let minify = false
2828

29+
let additionalFileSystemSources: string[] = []
30+
2931
// The Vite extension has two types of sources for candidates:
3032
//
3133
// 1. The module graph: These are all modules that vite transforms and we want
@@ -67,6 +69,7 @@ export default function tailwindcss(
6769
() => moduleGraphCandidates,
6870
scannerMode,
6971
config!.root,
72+
additionalFileSystemSources,
7073
customCssResolver,
7174
customJsResolver,
7275
)
@@ -209,13 +212,8 @@ export default function tailwindcss(
209212
minify = config.build.cssMinify !== false
210213
isSSR = config.build.ssr !== false && config.build.ssr !== undefined
211214

212-
if (scannerMode === 'automatic') {
213-
if (shouldDisableModuleGraph(config)) {
214-
console.warn('Detected an Astro.js build and opted-out of using the Vite module graph.')
215-
scannerMode = 'file-system'
216-
return
217-
}
218-
scannerMode = 'module-graph'
215+
if (isAstro(config)) {
216+
additionalFileSystemSources.push(path.join(config.root, 'src', 'components'))
219217
}
220218
},
221219

@@ -438,6 +436,7 @@ class Root {
438436
private getSharedCandidates: () => Map<string, Set<string>>,
439437
private scannerMode: ScannerMode,
440438
private base: string,
439+
private additionalFileSystemSources: string[],
441440

442441
private customCssResolver: (id: string, base: string) => Promise<string | false | undefined>,
443442
private customJsResolver: (id: string, base: string) => Promise<string | false | undefined>,
@@ -492,6 +491,15 @@ class Root {
492491
return [this.compiler.root]
493492
})().concat(this.compiler.globs)
494493

494+
if (this.additionalFileSystemSources) {
495+
sources = sources.concat(
496+
this.additionalFileSystemSources.map((source) => ({
497+
base: source,
498+
pattern: '**/*',
499+
})),
500+
)
501+
}
502+
495503
this.scanner = new Scanner({ sources })
496504
}
497505

@@ -608,6 +616,6 @@ class Root {
608616
}
609617
}
610618

611-
function shouldDisableModuleGraph(config: ResolvedConfig) {
619+
function isAstro(config: ResolvedConfig) {
612620
return config.plugins.some((p) => p.name === 'astro:scripts:page-ssr')
613621
}

0 commit comments

Comments
 (0)