Skip to content

Commit dc5cd8d

Browse files
committed
always migrate the js config in the Tailwind Root file
We already computed the correct Tailwind root file (and linked the JS config to it). This means that we can remove the logic in the migrate-config migration to find the correct location.
1 parent a8a66a4 commit dc5cd8d

File tree

1 file changed

+4
-37
lines changed

1 file changed

+4
-37
lines changed

packages/@tailwindcss-upgrade/src/codemods/migrate-config.ts

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'node:path'
2-
import postcss, { AtRule, type Plugin, Root } from 'postcss'
2+
import postcss, { AtRule, type Plugin } from 'postcss'
33
import { normalizePath } from '../../../@tailwindcss-node/src/normalize-path'
44
import type { JSConfigMigration } from '../migrate-js-config'
55
import type { Stylesheet } from '../stylesheet'
@@ -13,7 +13,9 @@ export function migrateConfig(
1313
jsConfigMigration,
1414
}: { configFilePath: string; jsConfigMigration: JSConfigMigration },
1515
): Plugin {
16-
function injectInto(sheet: Stylesheet) {
16+
function migrate() {
17+
if (!sheet.isTailwindRoot) return
18+
1719
let alreadyInjected = ALREADY_INJECTED.get(sheet)
1820
if (alreadyInjected && alreadyInjected.includes(configFilePath)) {
1921
return
@@ -82,41 +84,6 @@ export function migrateConfig(
8284
root.append(cssConfig.nodes)
8385
}
8486

85-
function migrate(root: Root) {
86-
// We can only migrate if there is an `@import "tailwindcss"` (or sub-import)
87-
let hasTailwindImport = false
88-
let hasFullTailwindImport = false
89-
root.walkAtRules('import', (node) => {
90-
if (node.params.match(/['"]tailwindcss['"]/)) {
91-
hasTailwindImport = true
92-
hasFullTailwindImport = true
93-
return false
94-
} else if (node.params.match(/['"]tailwindcss\/.*?['"]/)) {
95-
hasTailwindImport = true
96-
}
97-
})
98-
99-
if (!hasTailwindImport) return
100-
101-
// If a full `@import "tailwindcss"` is present or this is the root
102-
// stylesheet, we can inject the `@config` directive directly into this
103-
// file.
104-
if (hasFullTailwindImport || sheet.parents.size <= 0) {
105-
injectInto(sheet)
106-
return
107-
}
108-
109-
// Otherwise, if we are not the root file, we need to inject the `@config`
110-
// into the root file.
111-
if (sheet.parents.size > 0) {
112-
for (let parent of sheet.ancestors()) {
113-
if (parent.parents.size === 0) {
114-
injectInto(parent)
115-
}
116-
}
117-
}
118-
}
119-
12087
return {
12188
postcssPlugin: '@tailwindcss/upgrade/migrate-config',
12289
OnceExit: migrate,

0 commit comments

Comments
 (0)