11import type { CreateTailwindcssPatcherOptions } from '@/tailwindcss/patcher'
22import type { InternalUserDefinedOptions , TailwindcssPatcherLike } from '@/types'
3+ import { mkdirSync , mkdtempSync , rmSync , writeFileSync } from 'node:fs'
4+ import os from 'node:os'
35import path from 'node:path'
46import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
57
@@ -63,7 +65,7 @@ describe('createTailwindcssPatcherFromContext', () => {
6365 vi . resetModules ( )
6466 } )
6567
66- it ( 'creates multiple patchers when css entries resolve to different directories ' , async ( ) => {
68+ it ( 'creates multiple patchers when css entries belong to different package roots ' , async ( ) => {
6769 const calls : CreateTailwindcssPatcherOptions [ ] = [ ]
6870 const createdPatchers : TailwindcssPatcherLike [ ] = [ ]
6971 const classSets = [ [ 'foo' ] , [ 'bar' ] ]
@@ -77,7 +79,6 @@ describe('createTailwindcssPatcherFromContext', () => {
7779 const stub : TailwindcssPatcherLike = {
7880 packageInfo : { version : '4.1.0' } as any ,
7981 majorVersion : 4 ,
80- // 测试仅校验结构传递,避免在此处施加过严的类型约束
8182 options : options as any ,
8283 patch : vi . fn ( async ( ) => ( { } ) ) ,
8384 getClassSet : vi . fn ( async ( ) => new Set ( classes ) ) ,
@@ -93,39 +94,57 @@ describe('createTailwindcssPatcherFromContext', () => {
9394 } )
9495
9596 const { createTailwindcssPatcherFromContext } = await import ( '@/context/tailwindcss' )
96- const workspace = path . resolve ( '/workspace/project' )
97+
98+ const workspaceTemp = mkdtempSync ( path . join ( os . tmpdir ( ) , 'weapp-tw-workspace-' ) )
99+ const workspace = path . join ( workspaceTemp , 'project' )
100+ mkdirSync ( path . join ( workspace , 'apps' , 'alpha' , 'src' ) , { recursive : true } )
101+ writeFileSync ( path . join ( workspace , 'package.json' ) , JSON . stringify ( { name : 'workspace-root' } ) )
102+ writeFileSync ( path . join ( workspace , 'pnpm-workspace.yaml' ) , 'packages:\n - apps/*\n' )
103+ writeFileSync ( path . join ( workspace , 'apps' , 'alpha' , 'package.json' ) , JSON . stringify ( { name : 'alpha' } ) )
97104 const entryA = path . join ( workspace , 'apps' , 'alpha' , 'src' , 'app.css' )
98- const entryB = path . join ( workspace , 'apps' , 'beta' , 'src' , 'app.css' )
99- const ctx = {
100- tailwindcssBasedir : workspace ,
101- supportCustomLengthUnitsPatch : undefined ,
102- tailwindcss : undefined ,
103- tailwindcssPatcherOptions : undefined ,
104- cssEntries : [ entryA , entryB ] ,
105- appType : 'taro' ,
106- } as unknown as InternalUserDefinedOptions
107-
108- const patcher = createTailwindcssPatcherFromContext ( ctx )
109-
110- expect ( calls ) . toHaveLength ( 2 )
111- expect ( calls . map ( call => call . basedir ) ) . toEqual ( [
112- path . dirname ( entryA ) ,
113- path . dirname ( entryB ) ,
114- ] )
115-
116- await patcher . patch ( )
117- expect ( createdPatchers [ 0 ] . patch ) . toHaveBeenCalledTimes ( 1 )
118- expect ( createdPatchers [ 1 ] . patch ) . toHaveBeenCalledTimes ( 1 )
119-
120- const classSet = await patcher . getClassSet ( )
121- expect ( Array . from ( classSet ) ) . toEqual ( [ 'foo' , 'bar' ] )
122-
123- const extracted = await patcher . extract ( { } )
124- expect ( Array . from ( extracted . classSet ) ) . toEqual ( [ 'foo' , 'bar' ] )
125- expect ( extracted . classList ) . toEqual ( [ 'foo' , 'bar' ] )
105+
106+ const externalTemp = mkdtempSync ( path . join ( os . tmpdir ( ) , 'weapp-tw-external-' ) )
107+ const externalRoot = path . join ( externalTemp , 'external' )
108+ mkdirSync ( path . join ( externalRoot , 'src' ) , { recursive : true } )
109+ writeFileSync ( path . join ( externalRoot , 'package.json' ) , JSON . stringify ( { name : 'external-app' } ) )
110+ const entryB = path . join ( externalRoot , 'src' , 'app.css' )
111+
112+ try {
113+ const ctx = {
114+ tailwindcssBasedir : workspace ,
115+ supportCustomLengthUnitsPatch : undefined ,
116+ tailwindcss : undefined ,
117+ tailwindcssPatcherOptions : undefined ,
118+ cssEntries : [ entryA , entryB ] ,
119+ appType : 'taro' ,
120+ } as unknown as InternalUserDefinedOptions
121+
122+ const patcher = createTailwindcssPatcherFromContext ( ctx )
123+
124+ expect ( calls ) . toHaveLength ( 2 )
125+ expect ( calls . map ( call => call . basedir ) ) . toEqual ( [
126+ workspace ,
127+ externalRoot ,
128+ ] )
129+
130+ await patcher . patch ( )
131+ expect ( createdPatchers [ 0 ] . patch ) . toHaveBeenCalledTimes ( 1 )
132+ expect ( createdPatchers [ 1 ] . patch ) . toHaveBeenCalledTimes ( 1 )
133+
134+ const classSet = await patcher . getClassSet ( )
135+ expect ( Array . from ( classSet ) ) . toEqual ( [ 'foo' , 'bar' ] )
136+
137+ const extracted = await patcher . extract ( { } )
138+ expect ( Array . from ( extracted . classSet ) ) . toEqual ( [ 'foo' , 'bar' ] )
139+ expect ( extracted . classList ) . toEqual ( [ 'foo' , 'bar' ] )
140+ }
141+ finally {
142+ rmSync ( workspaceTemp , { recursive : true , force : true } )
143+ rmSync ( externalTemp , { recursive : true , force : true } )
144+ }
126145 } )
127146
128- it ( 'returns a single patcher when css entries share the same base directory ' , async ( ) => {
147+ it ( 'returns a single patcher when css entries share the same workspace base ' , async ( ) => {
129148 const createdPatchers : TailwindcssPatcherLike [ ] = [ ]
130149 const calls : CreateTailwindcssPatcherOptions [ ] = [ ]
131150 const createTailwindcssPatcher = vi . fn ( ( options : CreateTailwindcssPatcherOptions ) => {
@@ -150,27 +169,44 @@ describe('createTailwindcssPatcherFromContext', () => {
150169 vi . doMock ( '@/tailwindcss' , ( ) => ( { createTailwindcssPatcher } ) )
151170
152171 const { createTailwindcssPatcherFromContext } = await import ( '@/context/tailwindcss' )
153- const workspace = path . resolve ( '/workspace/project' )
154- const baseDir = path . join ( workspace , 'apps' , 'alpha' , 'src' )
155- const ctx = {
156- tailwindcssBasedir : undefined ,
157- supportCustomLengthUnitsPatch : undefined ,
158- tailwindcss : undefined ,
159- tailwindcssPatcherOptions : undefined ,
160- cssEntries : [
161- path . join ( baseDir , 'app.css' ) ,
162- path . join ( baseDir , 'other.css' ) ,
163- ] ,
164- appType : 'taro' ,
165- } as unknown as InternalUserDefinedOptions
166-
167- const patcher = createTailwindcssPatcherFromContext ( ctx )
168-
169- expect ( createTailwindcssPatcher ) . toHaveBeenCalledTimes ( 1 )
170- expect ( patcher ) . toBe ( createdPatchers [ 0 ] )
171- expect ( calls [ 0 ] . tailwindcss ?. v4 ?. cssEntries ) . toEqual ( [
172- path . join ( baseDir , 'app.css' ) ,
173- path . join ( baseDir , 'other.css' ) ,
174- ] )
172+ const workspaceTemp = mkdtempSync ( path . join ( os . tmpdir ( ) , 'weapp-tw-single-' ) )
173+ const workspace = path . join ( workspaceTemp , 'project' )
174+ mkdirSync ( path . join ( workspace , 'apps' , 'alpha' , 'src' ) , { recursive : true } )
175+ mkdirSync ( path . join ( workspace , 'apps' , 'beta' , 'src' ) , { recursive : true } )
176+ writeFileSync ( path . join ( workspace , 'package.json' ) , JSON . stringify ( { name : 'workspace-root' } ) )
177+ writeFileSync ( path . join ( workspace , 'pnpm-workspace.yaml' ) , 'packages:\n - apps/*\n' )
178+ writeFileSync ( path . join ( workspace , 'apps' , 'alpha' , 'package.json' ) , JSON . stringify ( { name : 'alpha' } ) )
179+ writeFileSync ( path . join ( workspace , 'apps' , 'beta' , 'package.json' ) , JSON . stringify ( { name : 'beta' } ) )
180+ const entryA = path . join ( workspace , 'apps' , 'alpha' , 'src' , 'app.css' )
181+ const entryB = path . join ( workspace , 'apps' , 'beta' , 'src' , 'other.css' )
182+
183+ try {
184+ const ctx = {
185+ tailwindcssBasedir : workspace ,
186+ supportCustomLengthUnitsPatch : undefined ,
187+ tailwindcss : undefined ,
188+ tailwindcssPatcherOptions : undefined ,
189+ cssEntries : [ entryA , entryB ] ,
190+ appType : 'taro' ,
191+ } as unknown as InternalUserDefinedOptions
192+
193+ const patcher = createTailwindcssPatcherFromContext ( ctx )
194+
195+ expect ( createTailwindcssPatcher ) . toHaveBeenCalledTimes ( 1 )
196+ expect ( patcher ) . toBe ( createdPatchers [ 0 ] )
197+ expect ( calls [ 0 ] . basedir ) . toBe ( workspace )
198+ expect ( calls [ 0 ] . tailwindcss ?. v4 ?. base ) . toBe ( workspace )
199+ expect ( calls [ 0 ] . tailwindcss ?. v4 ?. cssEntries ) . toEqual ( [
200+ entryA ,
201+ entryB ,
202+ ] )
203+ expect ( ctx . cssEntries ) . toEqual ( [
204+ entryA ,
205+ entryB ,
206+ ] )
207+ }
208+ finally {
209+ rmSync ( workspaceTemp , { recursive : true , force : true } )
210+ }
175211 } )
176212} )
0 commit comments