Skip to content

Commit 6efc62f

Browse files
committed
migrate source files based on Tailwind root stylesheets
1 parent cb555e1 commit 6efc62f

File tree

2 files changed

+70
-20
lines changed

2 files changed

+70
-20
lines changed

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

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env node
22

3+
import { Scanner } from '@tailwindcss/oxide'
34
import { globby } from 'globby'
45
import fs from 'node:fs/promises'
56
import path from 'node:path'
@@ -19,7 +20,6 @@ import { help } from './commands/help'
1920
import { Stylesheet } from './stylesheet'
2021
import { args, type Arg } from './utils/args'
2122
import { isRepoDirty } from './utils/git'
22-
import { hoistStaticGlobParts } from './utils/hoist-static-glob-parts'
2323
import { pkg } from './utils/packages'
2424
import { eprintln, error, header, highlight, info, relative, success } from './utils/renderer'
2525
import * as version from './utils/version'
@@ -227,38 +227,67 @@ async function run() {
227227
}
228228
}
229229

230+
let tailwindRootStylesheets = stylesheets.filter((sheet) => sheet.isTailwindRoot && sheet.file)
231+
230232
// Migrate source files
231-
if (configBySheet.size > 0) {
233+
if (tailwindRootStylesheets.length > 0) {
232234
info('Migrating templates…')
233235
}
234236
{
237+
let seenFiles = new Set()
238+
235239
// Template migrations
236-
for (let config of configBySheet.values()) {
237-
let set = new Set<string>()
238-
for (let globEntry of config.sources.flatMap((entry) => hoistStaticGlobParts(entry))) {
239-
let files = await globby([globEntry.pattern], {
240-
absolute: true,
241-
gitignore: true,
242-
cwd: globEntry.base,
243-
})
240+
for (let sheet of tailwindRootStylesheets) {
241+
let compiler = await sheet.compiler()
242+
if (!compiler) continue
243+
let designSystem = await sheet.designSystem()
244+
if (!designSystem) continue
245+
246+
// Figure out the source files to migrate
247+
let sources = (() => {
248+
// Disable auto source detection
249+
if (compiler.root === 'none') {
250+
return []
251+
}
244252

245-
for (let file of files) {
246-
set.add(file)
253+
// No root specified, use the base directory
254+
if (compiler.root === null) {
255+
return [{ base, pattern: '**/*', negated: false }]
247256
}
248-
}
249257

250-
let files = Array.from(set)
251-
files.sort()
258+
// Use the specified root
259+
return [{ ...compiler.root, negated: false }]
260+
})().concat(compiler.sources)
261+
262+
let config = configBySheet.get(sheet)
263+
let scanner = new Scanner({ sources })
264+
let filesToMigrate = []
265+
for (let file of scanner.files) {
266+
if (seenFiles.has(file)) continue
267+
seenFiles.add(file)
268+
filesToMigrate.push(file)
269+
}
252270

253271
// Migrate each file
254272
await Promise.allSettled(
255-
files.map((file) => migrateTemplate(config.designSystem, config.userConfig, file)),
273+
filesToMigrate.map((file) =>
274+
migrateTemplate(designSystem, config?.userConfig ?? null, file),
275+
),
256276
)
257277

258-
success(
259-
`Migrated templates for configuration file: ${highlight(relative(config.configFilePath, base))}`,
260-
{ prefix: '↳ ' },
261-
)
278+
if (config?.configFilePath) {
279+
success(
280+
`Migrated templates for configuration file: ${highlight(relative(config.configFilePath, base))}`,
281+
{ prefix: '↳ ' },
282+
)
283+
} else {
284+
success(
285+
`Migrated templates for: ${highlight(relative(sheet.file ?? '<unknown>', base))}`,
286+
{
287+
prefix: '↳ ',
288+
},
289+
)
290+
}
262291
}
263292
}
264293
}

packages/@tailwindcss-upgrade/src/stylesheet.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { __unstable__loadDesignSystem, compileAst } from '@tailwindcss/node'
12
import * as fsSync from 'node:fs'
23
import * as fs from 'node:fs/promises'
34
import * as path from 'node:path'
45
import * as util from 'node:util'
56
import * as postcss from 'postcss'
7+
import { postCssAstToCssAst } from '../../@tailwindcss-postcss/src/ast'
68

79
export type StylesheetId = string
810

@@ -263,6 +265,25 @@ export class Stylesheet {
263265
return false
264266
}
265267

268+
async compiler(): Promise<Awaited<ReturnType<typeof compileAst>> | null> {
269+
if (!this.isTailwindRoot) return null
270+
if (!this.file) return null
271+
272+
return compileAst(postCssAstToCssAst(this.root), {
273+
base: path.dirname(this.file),
274+
onDependency() {},
275+
})
276+
}
277+
278+
async designSystem(): Promise<Awaited<ReturnType<typeof __unstable__loadDesignSystem>> | null> {
279+
if (!this.isTailwindRoot) return null
280+
if (!this.file) return null
281+
282+
return __unstable__loadDesignSystem(this.root.toString(), {
283+
base: path.dirname(this.file),
284+
})
285+
}
286+
266287
[util.inspect.custom]() {
267288
return {
268289
...this,

0 commit comments

Comments
 (0)