|
| 1 | +import { Scanner } from '@tailwindcss/oxide' |
1 | 2 | import fs from 'node:fs/promises'
|
2 | 3 | import { dirname } from 'path'
|
3 |
| -import type { Config } from 'tailwindcss' |
| 4 | +import { type Config } from 'tailwindcss' |
4 | 5 | import defaultTheme from 'tailwindcss/defaultTheme'
|
5 | 6 | import { fileURLToPath } from 'url'
|
6 | 7 | import { loadModule } from '../../@tailwindcss-node/src/compile'
|
@@ -54,7 +55,7 @@ export async function migrateJsConfig(
|
54 | 55 | }
|
55 | 56 |
|
56 | 57 | if ('content' in unresolvedConfig) {
|
57 |
| - sources = migrateContent(unresolvedConfig as any, base) |
| 58 | + sources = await migrateContent(unresolvedConfig as any, base) |
58 | 59 | }
|
59 | 60 |
|
60 | 61 | if ('theme' in unresolvedConfig) {
|
@@ -158,16 +159,31 @@ function createSectionKey(key: string[]): string {
|
158 | 159 | return sectionSegments.join('-')
|
159 | 160 | }
|
160 | 161 |
|
161 |
| -function migrateContent( |
| 162 | +async function migrateContent( |
162 | 163 | unresolvedConfig: Config & { content: any },
|
163 | 164 | base: string,
|
164 |
| -): { base: string; pattern: string }[] { |
| 165 | +): Promise<{ base: string; pattern: string }[]> { |
| 166 | + let autoContentFiles = autodetectedSourceFiles(base) |
| 167 | + |
165 | 168 | let sources = []
|
166 | 169 | for (let content of unresolvedConfig.content) {
|
167 | 170 | if (typeof content !== 'string') {
|
168 | 171 | throw new Error('Unsupported content value: ' + content)
|
169 | 172 | }
|
170 |
| - sources.push({ base, pattern: content }) |
| 173 | + |
| 174 | + let sourceFiles = patternSourceFiles({ base, pattern: content }) |
| 175 | + |
| 176 | + let autoContentContainsAllSourceFiles = true |
| 177 | + for (let sourceFile of sourceFiles) { |
| 178 | + if (!autoContentFiles.includes(sourceFile)) { |
| 179 | + autoContentContainsAllSourceFiles = false |
| 180 | + break |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if (!autoContentContainsAllSourceFiles) { |
| 185 | + sources.push({ base, pattern: content }) |
| 186 | + } |
171 | 187 | }
|
172 | 188 | return sources
|
173 | 189 | }
|
@@ -253,3 +269,15 @@ function keyframesToCss(keyframes: Record<string, unknown>): string {
|
253 | 269 | let ast: AstNode[] = keyframesToRules({ theme: { keyframes } })
|
254 | 270 | return toCss(ast).trim() + '\n'
|
255 | 271 | }
|
| 272 | + |
| 273 | +function autodetectedSourceFiles(base: string) { |
| 274 | + let scanner = new Scanner({ detectSources: { base } }) |
| 275 | + scanner.scan() |
| 276 | + return scanner.files |
| 277 | +} |
| 278 | + |
| 279 | +function patternSourceFiles(source: { base: string; pattern: string }): string[] { |
| 280 | + let scanner = new Scanner({ sources: [source] }) |
| 281 | + scanner.scan() |
| 282 | + return scanner.files |
| 283 | +} |
0 commit comments