Skip to content

Commit 27d198e

Browse files
committed
chore: bump version
1 parent e73b989 commit 27d198e

File tree

5 files changed

+112
-12
lines changed

5 files changed

+112
-12
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'weapp-tailwindcss': patch
3+
---
4+
5+
修复 `tailwindcss-patch` 默认缓存目录,确保其写入包级 `node_modules/.cache/tailwindcss-patch`

packages/weapp-tailwindcss/src/tailwindcss/patcher.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,24 +312,29 @@ function normalizeTailwindcssPatcherOptions(
312312
export function createTailwindcssPatcher(options?: CreateTailwindcssPatcherOptions): TailwindcssPatcherLike {
313313
const { basedir, cacheDir, supportCustomLengthUnitsPatch, tailwindcss, tailwindcssPatcherOptions } = options || {}
314314
const cache: TailwindCacheOptions = {}
315+
const normalizedBasedir = basedir ? path.resolve(basedir) : undefined
316+
const cacheRoot = findNearestPackageRoot(normalizedBasedir) ?? normalizedBasedir ?? process.cwd()
315317

316318
if (cacheDir) {
317319
if (path.isAbsolute(cacheDir)) {
318320
cache.dir = cacheDir
319321
}
320-
else if (basedir) {
321-
cache.dir = path.resolve(basedir, cacheDir)
322+
else if (normalizedBasedir) {
323+
cache.dir = path.resolve(normalizedBasedir, cacheDir)
322324
}
323325
else {
324326
cache.dir = path.resolve(process.cwd(), cacheDir)
325327
}
326328
}
329+
else {
330+
cache.dir = path.join(cacheRoot, 'node_modules', '.cache', 'tailwindcss-patch')
331+
}
327332

328-
if (basedir) {
329-
cache.cwd = basedir
333+
if (normalizedBasedir) {
334+
cache.cwd = normalizedBasedir
330335
}
331336

332-
const resolvePaths = createDefaultResolvePaths(cache.cwd ?? basedir ?? process.cwd())
337+
const resolvePaths = createDefaultResolvePaths(cache.cwd ?? normalizedBasedir ?? process.cwd())
333338

334339
const normalizedUserOptions = normalizeTailwindcssPatcherOptions(tailwindcssPatcherOptions)
335340

@@ -338,7 +343,7 @@ export function createTailwindcssPatcher(options?: CreateTailwindcssPatcherOptio
338343
const baseTailwindOptions = defuOverrideArray<TailwindUserOptions, Partial<TailwindUserOptions>[]>(
339344
(tailwindcss ?? {}) as TailwindUserOptions,
340345
{
341-
cwd: basedir,
346+
cwd: normalizedBasedir,
342347
resolve: {
343348
paths: resolvePaths,
344349
},
@@ -371,7 +376,7 @@ export function createTailwindcssPatcher(options?: CreateTailwindcssPatcherOptio
371376
}
372377

373378
const baseOptions: TailwindcssPatchOptions = {
374-
cwd: basedir,
379+
cwd: normalizedBasedir,
375380
cache,
376381
tailwind: baseTailwindOptions,
377382
features: {

packages/weapp-tailwindcss/test/options.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'node:path'
12
import { getCompilerContext } from '@/context'
23
import { defu } from '@/utils'
34

@@ -127,12 +128,15 @@ describe('get options', () => {
127128
})
128129
let cacheOptions = config.twPatcher.options?.cache
129130
expect(cacheOptions?.enabled).toBe(true)
130-
expect(cacheOptions?.dir?.includes('node_modules/.cache/tailwindcss-patch')).toBe(true)
131+
const expectedDefaultCacheDir = path.join(process.cwd(), 'node_modules', '.cache', 'tailwindcss-patch')
132+
expect(cacheOptions?.dir).toBe(expectedDefaultCacheDir)
131133
config = getCompilerContext({
132134
appType: 'mpx',
133135
})
134136
cacheOptions = config.twPatcher.options?.cache
135-
expect(cacheOptions?.dir?.includes('node_modules/tailwindcss-patch/.cache')).toBe(true)
137+
expect(
138+
cacheOptions?.dir?.endsWith(path.normalize('node_modules/tailwindcss-patch/.cache')),
139+
).toBe(true)
136140
})
137141

138142
// it('customAttributes map defu merge', () => {

packages/weapp-tailwindcss/test/tailwindcss/helpers.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ describe('tailwindcss helpers', () => {
114114
cwdSpy.mockRestore()
115115
})
116116

117+
it('defaults cache directory to package root node_modules cache', async () => {
118+
const { createTailwindcssPatcher } = await import('@/tailwindcss')
119+
const repoRoot = path.resolve(__dirname, '../../../..')
120+
const basedir = path.join(repoRoot, 'packages', 'weapp-tailwindcss', 'src')
121+
122+
createTailwindcssPatcher({ basedir })
123+
124+
const lastCall = tailwindcssPatcherMock.mock.calls[tailwindcssPatcherMock.mock.calls.length - 1]
125+
const callArgs = lastCall?.[0] as any
126+
expect(callArgs.cache?.dir).toBe(
127+
path.join(repoRoot, 'packages', 'weapp-tailwindcss', 'node_modules', '.cache', 'tailwindcss-patch'),
128+
)
129+
})
130+
117131
it('gracefully falls back when tailwindcss is missing', async () => {
118132
tailwindcssPatcherMock.mockImplementationOnce(() => {
119133
throw new Error('tailwindcss not found')

scripts/sync-template-deps.ts

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ import { execa } from 'execa'
1111
import { minVersion } from 'semver'
1212
import { ROOT } from './template-utils'
1313

14+
type PnpmConfig = Record<string, unknown> & {
15+
onlyBuiltDependencies?: unknown
16+
}
17+
1418
type PackageJson = Record<string, unknown> & {
1519
dependencies?: Record<string, string>
1620
devDependencies?: Record<string, string>
1721
packageManager?: string
22+
pnpm?: PnpmConfig
1823
}
1924

2025
type Manager = 'pnpm' | 'yarn' | 'npm'
@@ -30,6 +35,7 @@ interface TargetPackage {
3035
range: string
3136
addIfMissing?: 'dependencies' | 'devDependencies'
3237
forceInclude?: boolean
38+
ensurePnpmOnlyBuilt?: boolean
3339
}
3440

3541
const TEMPLATES_DIR = path.join(ROOT, 'templates')
@@ -96,6 +102,7 @@ async function resolveBaseTargets(): Promise<TargetPackage[]> {
96102
range: `^${mergeVersion}`,
97103
addIfMissing: 'dependencies',
98104
forceInclude: true,
105+
ensurePnpmOnlyBuilt: true,
99106
},
100107
{
101108
name: 'weapp-ide-cli',
@@ -171,6 +178,70 @@ function ensureSection(pkg: PackageJson, field: 'dependencies' | 'devDependencie
171178
return created
172179
}
173180

181+
function ensurePnpmOnlyBuiltDependencies(
182+
pkg: PackageJson,
183+
targets: TargetPackage[],
184+
manager: ManagerInfo,
185+
): boolean {
186+
if (manager.name !== 'pnpm') {
187+
return false
188+
}
189+
190+
const required = Array.from(
191+
new Set(
192+
targets
193+
.filter(target => target.ensurePnpmOnlyBuilt)
194+
.map(target => target.name),
195+
),
196+
)
197+
198+
if (required.length === 0) {
199+
return false
200+
}
201+
202+
let changed = false
203+
let pnpmConfig = pkg.pnpm
204+
if (!pnpmConfig || typeof pnpmConfig !== 'object') {
205+
pnpmConfig = {} as PnpmConfig
206+
pkg.pnpm = pnpmConfig
207+
changed = true
208+
}
209+
210+
const config = pnpmConfig as PnpmConfig
211+
const rawOnlyBuilt = config.onlyBuiltDependencies
212+
213+
let onlyBuilt: string[]
214+
if (Array.isArray(rawOnlyBuilt)) {
215+
const allStrings = rawOnlyBuilt.every(item => typeof item === 'string')
216+
if (allStrings) {
217+
onlyBuilt = rawOnlyBuilt as string[]
218+
}
219+
else {
220+
onlyBuilt = rawOnlyBuilt.filter((item): item is string => typeof item === 'string')
221+
changed = true
222+
}
223+
}
224+
else {
225+
onlyBuilt = []
226+
if (rawOnlyBuilt !== undefined) {
227+
changed = true
228+
}
229+
}
230+
231+
for (const name of required) {
232+
if (!onlyBuilt.includes(name)) {
233+
onlyBuilt.push(name)
234+
changed = true
235+
}
236+
}
237+
238+
if (changed) {
239+
config.onlyBuiltDependencies = onlyBuilt
240+
}
241+
242+
return changed
243+
}
244+
174245
function updateDependencyRange(pkg: PackageJson, targets: TargetPackage[]): boolean {
175246
let updated = false
176247
for (const field of ['dependencies', 'devDependencies'] as const) {
@@ -483,14 +554,15 @@ async function processTemplate(
483554
const tailwindTargets = await resolveTailwindTargets(pkg)
484555
const relevantTargets = dedupeTargets([...filterTargetsForPackage(pkg, baseTargets), ...tailwindTargets])
485556
const manager = detectManager(templateDir, pkg)
486-
const changed = updateDependencyRange(pkg, relevantTargets)
557+
const depsChanged = updateDependencyRange(pkg, relevantTargets)
558+
const pnpmChanged = ensurePnpmOnlyBuiltDependencies(pkg, relevantTargets, manager)
487559
const needLock = lockNeedsUpdate(templateDir, manager, relevantTargets)
488560

489-
if (!changed && !needLock) {
561+
if (!depsChanged && !pnpmChanged && !needLock) {
490562
return false
491563
}
492564

493-
if (changed) {
565+
if (depsChanged || pnpmChanged) {
494566
writeFileSync(pkgPath, `${JSON.stringify(pkg, null, indent)}\n`)
495567
}
496568

0 commit comments

Comments
 (0)