1+ import { createRequire } from 'node:module'
12import os from 'node:os'
23import fs from 'fs-extra'
34import path from 'pathe'
@@ -11,6 +12,7 @@ import * as contextRegistry from '@/runtime/context-registry'
1112import * as runtimeBuild from '@/runtime/process-tailwindcss'
1213
1314const fixturesRoot = path . resolve ( __dirname , 'fixtures/v4' )
15+ const require = createRequire ( import . meta. url )
1416let tempDir : string
1517
1618beforeEach ( async ( ) => {
@@ -276,6 +278,9 @@ describe('TailwindcssPatcher', () => {
276278 dir : tempDir ,
277279 file : 'cache.json' ,
278280 } ,
281+ tailwind : {
282+ versionHint : 3 ,
283+ } ,
279284 } )
280285
281286 const classCache = new Map < string , any > ( [
@@ -312,6 +317,9 @@ describe('TailwindcssPatcher', () => {
312317 file : 'cache.json' ,
313318 strategy : 'overwrite' ,
314319 } ,
320+ tailwind : {
321+ versionHint : 3 ,
322+ } ,
315323 } )
316324
317325 vi . spyOn ( patcher , 'getContexts' ) . mockReturnValue ( [
@@ -333,6 +341,9 @@ describe('TailwindcssPatcher', () => {
333341 const patcher = new TailwindcssPatcher ( {
334342 overwrite : false ,
335343 cache : false ,
344+ tailwind : {
345+ versionHint : 3 ,
346+ } ,
336347 } )
337348
338349 vi . spyOn ( patcher , 'getContexts' ) . mockImplementation ( ( ) => contexts )
@@ -658,6 +669,9 @@ describe('TailwindcssPatcher', () => {
658669 dir : tempDir ,
659670 file : 'cache.json' ,
660671 } ,
672+ tailwind : {
673+ versionHint : 3 ,
674+ } ,
661675 } )
662676
663677 vi . spyOn ( patcher , 'getContexts' ) . mockReturnValue ( [
@@ -691,6 +705,9 @@ describe('TailwindcssPatcher', () => {
691705 file : 'cache.json' ,
692706 strategy : 'overwrite' ,
693707 } ,
708+ tailwind : {
709+ versionHint : 3 ,
710+ } ,
694711 } )
695712
696713 vi . spyOn ( patcher , 'getContexts' ) . mockReturnValue ( [
@@ -714,6 +731,9 @@ describe('TailwindcssPatcher', () => {
714731 dir : tempDir ,
715732 file : 'cache.json' ,
716733 } ,
734+ tailwind : {
735+ versionHint : 3 ,
736+ } ,
717737 } )
718738
719739 const getContextsSpy = vi . spyOn ( patcher , 'getContexts' )
@@ -738,4 +758,74 @@ describe('TailwindcssPatcher', () => {
738758 expect ( second ?. has ( 'second-pass' ) ) . toBe ( true )
739759 expect ( second ?. has ( 'first-pass' ) ) . toBe ( false )
740760 } )
761+
762+ it ( 'drops removed v3 content classes after recreating the patcher' , async ( ) => {
763+ const projectRoot = await fs . mkdtemp ( path . join ( tempDir , 'v3-refresh-' ) )
764+ const tailwindRoot = path . dirname ( require . resolve ( 'tailwindcss-3/package.json' ) )
765+ const postcssPlugin = require . resolve ( 'tailwindcss-3' )
766+ const wxmlFile = path . join ( projectRoot , 'src/index.wxml' )
767+ const marker = 'text-red-500'
768+
769+ try {
770+ await fs . ensureDir ( path . join ( projectRoot , 'src' ) )
771+ await fs . writeFile (
772+ path . join ( projectRoot , 'tailwind.config.js' ) ,
773+ [
774+ 'const path = require(\'path\')' ,
775+ 'module.exports = {' ,
776+ ' content: [path.resolve(__dirname, "./src/*.{js,wxml}")],' ,
777+ ' theme: { extend: {} },' ,
778+ ' plugins: [],' ,
779+ ' corePlugins: { preflight: false },' ,
780+ '}' ,
781+ ] . join ( '\n' ) ,
782+ 'utf8' ,
783+ )
784+ const original = '<view class="font-bold">baseline</view>\n'
785+ await fs . writeFile ( wxmlFile , original , 'utf8' )
786+
787+ const createPatcher = ( ) => new TailwindcssPatcher ( {
788+ projectRoot,
789+ cache : false ,
790+ output : {
791+ enabled : false ,
792+ } ,
793+ tailwind : {
794+ packageName : 'tailwindcss-3' ,
795+ version : 3 ,
796+ resolve : {
797+ paths : [ path . dirname ( tailwindRoot ) ] ,
798+ } ,
799+ cwd : projectRoot ,
800+ config : path . join ( projectRoot , 'tailwind.config.js' ) ,
801+ postcssPlugin,
802+ v3 : {
803+ cwd : projectRoot ,
804+ config : path . join ( projectRoot , 'tailwind.config.js' ) ,
805+ postcssPlugin,
806+ } ,
807+ } ,
808+ } )
809+
810+ const baselinePatcher = createPatcher ( )
811+ await baselinePatcher . patch ( )
812+ const baseline = await baselinePatcher . extract ( { write : false } )
813+ expect ( baseline . classSet . has ( marker ) ) . toBe ( false )
814+
815+ await fs . writeFile ( wxmlFile , `${ original } <view class="${ marker } ">hmr</view>\n` , 'utf8' )
816+ const updatedPatcher = createPatcher ( )
817+ await updatedPatcher . patch ( )
818+ const updated = await updatedPatcher . extract ( { write : false } )
819+ expect ( updated . classSet . has ( marker ) ) . toBe ( true )
820+
821+ await fs . writeFile ( wxmlFile , original , 'utf8' )
822+ const restoredPatcher = createPatcher ( )
823+ await restoredPatcher . patch ( )
824+ const restored = await restoredPatcher . extract ( { write : false } )
825+ expect ( restored . classSet . has ( marker ) ) . toBe ( false )
826+ }
827+ finally {
828+ await fs . remove ( projectRoot )
829+ }
830+ } )
741831} )
0 commit comments