@@ -3,9 +3,11 @@ import type { Plugin, ResolvedConfig } from 'vite'
33import type { CreateJsHandlerOptions } from '@/types'
44import { readFile } from 'node:fs/promises'
55import path from 'node:path'
6+ import { MappingChars2String } from '@weapp-core/escape'
67import { beforeEach , describe , expect , it , vi } from 'vitest'
78import { createJsHandler } from '@/js'
89import { replaceWxml } from '@/wxml'
10+ import { createTemplateHandler } from '@/wxml/utils'
911import {
1012 createContext ,
1113 createRollupAsset ,
@@ -41,6 +43,7 @@ async function loadIssue814Fixture() {
4143describe ( 'bundlers/vite UnifiedViteWeappTailwindcssPlugin bundle' , ( ) => {
4244 beforeEach ( ( ) => {
4345 vi . resetModules ( )
46+ vi . doUnmock ( '@/bundlers/vite/incremental-runtime-class-set' )
4447 resetVitePluginTestContext ( )
4548 } )
4649
@@ -187,6 +190,83 @@ const trace = "at App.vue:4"
187190 expect ( currentContext . twPatcher . getClassSetSync ) . toHaveBeenCalledTimes ( 2 )
188191 } , TEST_TIMEOUT_MS )
189192
193+ it ( 'warns and falls back to full runtime set when bundle runtime set misses dynamic arbitrary candidates in wxml' , async ( ) => {
194+ const fallbackRuntimeSet = new Set ( [
195+ 'bg-[#68c828]' ,
196+ 'text-[100px]' ,
197+ 'text-[#123456]' ,
198+ 'w-[323px]' ,
199+ 'h-[30px]' ,
200+ 'h-[45px]' ,
201+ ] )
202+ const incompleteRuntimeSet = new Set ( [
203+ 'bg-[#68c828]' ,
204+ 'text-[100px]' ,
205+ 'text-[#123456]' ,
206+ 'w-[323px]' ,
207+ ] )
208+ const syncMock = vi . fn ( async ( ) => incompleteRuntimeSet )
209+ const resetMock = vi . fn ( async ( ) => undefined )
210+ vi . doMock ( '@/bundlers/vite/incremental-runtime-class-set' , ( ) => ( {
211+ createBundleRuntimeClassSetManager : ( ) => ( {
212+ sync : syncMock ,
213+ reset : resetMock ,
214+ } ) ,
215+ } ) )
216+
217+ const jsHandler = createJsHandler ( {
218+ escapeMap : MappingChars2String ,
219+ staleClassNameFallback : false ,
220+ tailwindcssMajorVersion : 4 ,
221+ } )
222+ const templateHandler = createTemplateHandler ( {
223+ escapeMap : MappingChars2String ,
224+ jsHandler,
225+ } )
226+
227+ setCurrentContext ( createContext ( {
228+ templateHandler,
229+ jsHandler,
230+ twPatcher : {
231+ patch : vi . fn ( ) ,
232+ getClassSet : vi . fn ( async ( ) => fallbackRuntimeSet ) ,
233+ getClassSetSync : vi . fn ( ( ) => fallbackRuntimeSet ) ,
234+ extract : vi . fn ( async ( ) => ( { classSet : fallbackRuntimeSet } ) ) ,
235+ majorVersion : 4 ,
236+ } ,
237+ } ) )
238+
239+ const UnifiedViteWeappTailwindcssPlugin = await loadUnifiedVitePlugin ( )
240+ const plugins = UnifiedViteWeappTailwindcssPlugin ( )
241+ const postPlugin = plugins ?. find ( plugin => plugin . name === 'weapp-tailwindcss:adaptor:post' ) as Plugin
242+ expect ( postPlugin ) . toBeTruthy ( )
243+
244+ await ( postPlugin . configResolved as any ) ?. call ( postPlugin , {
245+ command : 'build' ,
246+ root : process . cwd ( ) ,
247+ css : { postcss : { plugins : [ ] } } ,
248+ build : { outDir : 'dist' } ,
249+ } as ResolvedConfig )
250+
251+ const rawWxml = '<view class="bg-[#68c828] text-[100px] text-[#123456] w-[323px] {{true?\'h-[30px]\':\'h-[45px]\'}}">111</view>'
252+ const bundle = {
253+ 'pages/index/index.wxml' : {
254+ ...createRollupAsset ( rawWxml ) ,
255+ fileName : 'pages/index/index.wxml' ,
256+ } satisfies OutputAsset ,
257+ }
258+
259+ const generateBundle = postPlugin . generateBundle as any
260+ await generateBundle ?. call ( postPlugin , { } as any , bundle )
261+
262+ expect ( syncMock ) . toHaveBeenCalledTimes ( 1 )
263+ const transformed = ( bundle [ 'pages/index/index.wxml' ] as OutputAsset ) . source . toString ( )
264+ expect ( transformed ) . toContain ( 'h-_b30px_B' )
265+ expect ( transformed ) . toContain ( 'h-_b45px_B' )
266+ expect ( transformed ) . not . toContain ( 'h-[30px]' )
267+ expect ( transformed ) . not . toContain ( 'h-[45px]' )
268+ } , TEST_TIMEOUT_MS )
269+
190270 it ( 'refreshes runtime class set when only comment-carried class candidates change' , async ( ) => {
191271 const UnifiedViteWeappTailwindcssPlugin = await loadUnifiedVitePlugin ( )
192272 const runtimeSets = [
0 commit comments