Skip to content

Commit

Permalink
Fix glob pattern hoisting on Windows (#14904)
Browse files Browse the repository at this point in the history
This ensures our glob hoisting mechanism (see #14896) works on Windows
when performing an upgrade.

---------

Co-authored-by: Jordan Pittman <[email protected]>
  • Loading branch information
RobinMalfait and thecrypticace authored Nov 7, 2024
1 parent 95c4877 commit 99c4c04
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
5 changes: 4 additions & 1 deletion integrations/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface SpawnedProcess {

interface ChildProcessOptions {
cwd?: string
env?: Record<string, string>
}

interface ExecOptions {
Expand Down Expand Up @@ -109,6 +110,7 @@ export function test(
{
cwd,
...childProcessOptions,
env: childProcessOptions.env,
},
(error, stdout, stderr) => {
if (error) {
Expand Down Expand Up @@ -145,10 +147,11 @@ export function test(
let child = spawn(command, {
cwd,
shell: true,
...childProcessOptions,
env: {
...process.env,
...childProcessOptions.env,
},
...childProcessOptions,
})

function dispose() {
Expand Down
6 changes: 5 additions & 1 deletion integrations/vite/nuxt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ test('dev mode', SETUP, async ({ fs, spawn, getFreePort }) => {
test('build', SETUP, async ({ spawn, getFreePort, exec }) => {
let port = await getFreePort()
await exec(`pnpm nuxt build`)
await spawn(`PORT=${port} pnpm nuxt preview`)
await spawn(`pnpm nuxt preview`, {
env: {
PORT: `${port}`,
},
})

await retryAssertion(async () => {
let css = await fetchStyles(port)
Expand Down
26 changes: 13 additions & 13 deletions packages/@tailwindcss-upgrade/src/migrate-js-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Scanner } from '@tailwindcss/oxide'
import fs from 'node:fs/promises'
import { dirname, resolve } from 'node:path'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { type Config } from 'tailwindcss'
import defaultTheme from 'tailwindcss/defaultTheme'
Expand All @@ -21,7 +21,7 @@ import { findStaticPlugins, type StaticPluginOptions } from './utils/extract-sta
import { info } from './utils/renderer'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const __dirname = path.dirname(__filename)

export type JSConfigMigration =
// Could not convert the config file, need to inject it as-is in a @config directive
Expand Down Expand Up @@ -195,21 +195,21 @@ async function migrateContent(
return unresolvedConfig.future?.relativeContentPathsByDefault ?? false
})()

let contentFiles = Array.isArray(unresolvedConfig.content)
? unresolvedConfig.content
: (unresolvedConfig.content?.files ?? []).map((content) => {
if (typeof content === 'string' && contentIsRelative) {
return resolve(dirname(configPath), content)
let sourceGlobs = Array.isArray(unresolvedConfig.content)
? unresolvedConfig.content.map((pattern) => ({ base, pattern }))
: (unresolvedConfig.content?.files ?? []).map((pattern) => {
if (typeof pattern === 'string' && contentIsRelative) {
return { base: path.dirname(configPath), pattern: pattern }
}
return content
return { base, pattern }
})

for (let content of contentFiles) {
if (typeof content !== 'string') {
throw new Error('Unsupported content value: ' + content)
for (let { base, pattern } of sourceGlobs) {
if (typeof pattern !== 'string') {
throw new Error('Unsupported content value: ' + pattern)
}

let sourceFiles = patternSourceFiles({ base, pattern: content })
let sourceFiles = patternSourceFiles({ base, pattern })

let autoContentContainsAllSourceFiles = true
for (let sourceFile of sourceFiles) {
Expand All @@ -220,7 +220,7 @@ async function migrateContent(
}

if (!autoContentContainsAllSourceFiles) {
sources.push({ base, pattern: content })
sources.push({ base, pattern })
}
}
return sources
Expand Down
2 changes: 1 addition & 1 deletion packages/@tailwindcss-upgrade/src/template/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default async function migrateContents(
}

export async function migrate(designSystem: DesignSystem, userConfig: Config, file: string) {
let fullPath = path.resolve(process.cwd(), file)
let fullPath = path.isAbsolute(file) ? file : path.resolve(process.cwd(), file)
let contents = await fs.readFile(fullPath, 'utf-8')

await fs.writeFile(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { normalizePath } from '@tailwindcss/node'
import braces from 'braces'
import path from 'node:path'

Expand All @@ -12,10 +13,12 @@ export function hoistStaticGlobParts(entry: GlobEntry): GlobEntry[] {
let [staticPart, dynamicPart] = splitPattern(pattern)

// Move static part into the `base`.
let absolutePosixPath = normalizePath(entry.base)

if (staticPart !== null) {
clone.base = path.resolve(entry.base, staticPart)
clone.base = path.posix.join(absolutePosixPath, staticPart)
} else {
clone.base = path.resolve(entry.base)
clone.base = absolutePosixPath
}

// Move dynamic part into the `pattern`.
Expand Down Expand Up @@ -56,7 +59,7 @@ function splitPattern(pattern: string): [staticPart: string | null, dynamicPart:
let lastSlashPosition: number | null = null

for (let i = 0; i < pattern.length; i++) {
let c = pattern[i];
let c = pattern[i]
if (c === '/') {
lastSlashPosition = i
}
Expand Down

0 comments on commit 99c4c04

Please sign in to comment.