Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
make migrations async
  • Loading branch information
RobinMalfait committed Nov 14, 2024
commit e300259f3994fc1604c1aee347a411657dad2345
23 changes: 16 additions & 7 deletions packages/@tailwindcss-upgrade/src/codemods/migrate-at-apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,26 @@ export function migrateAtApply({
return [...variants, utility].join(':')
})

// If we have a valid designSystem and config setup, we can run all
// candidate migrations on each utility
params = params.map((param) => migrateCandidate(designSystem, userConfig, param))

atRule.params = params.join('').trim()
return async () => {
// If we have a valid designSystem and config setup, we can run all
// candidate migrations on each utility
params = await Promise.all(
params.map(async (param) => await migrateCandidate(designSystem, userConfig, param)),
)

atRule.params = params.join('').trim()
}
}

return {
postcssPlugin: '@tailwindcss/upgrade/migrate-at-apply',
OnceExit(root) {
root.walkAtRules('apply', migrate)
async OnceExit(root) {
let migrations: (() => void)[] = []
root.walkAtRules('apply', (atRule) => {
migrations.push(migrate(atRule))
})

await Promise.allSettled(migrations.map((m) => m()))
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ test.each([
base: __dirname,
})

expect(simpleLegacyClasses(designSystem, {}, candidate)).toEqual(result)
expect(await simpleLegacyClasses(designSystem, {}, candidate)).toEqual(result)
})
10 changes: 5 additions & 5 deletions packages/@tailwindcss-upgrade/src/template/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type Migration = (
start: number
end: number
},
) => string
) => string | Promise<string>

export const DEFAULT_MIGRATIONS: Migration[] = [
prefix,
Expand All @@ -41,7 +41,7 @@ export const DEFAULT_MIGRATIONS: Migration[] = [
modernizeArbitraryValues,
]

export function migrateCandidate(
export async function migrateCandidate(
designSystem: DesignSystem,
userConfig: Config,
rawCandidate: string,
Expand All @@ -51,9 +51,9 @@ export function migrateCandidate(
start: number
end: number
},
): string {
): Promise<string> {
for (let migration of DEFAULT_MIGRATIONS) {
rawCandidate = migration(designSystem, userConfig, rawCandidate, location)
rawCandidate = await migration(designSystem, userConfig, rawCandidate, location)
}
return rawCandidate
}
Expand All @@ -69,7 +69,7 @@ export default async function migrateContents(
let changes: StringChange[] = []

for (let { rawCandidate, start, end } of candidates) {
let migratedCandidate = migrateCandidate(designSystem, userConfig, rawCandidate, {
let migratedCandidate = await migrateCandidate(designSystem, userConfig, rawCandidate, {
contents,
start,
end,
Expand Down