@@ -3,7 +3,7 @@ import type { ResolvedUnpluginOptions, UnpluginOptions } from 'unplugin'
33import type { WebpackPluginOptions } from '.'
44import { isAbsolute , normalize } from 'node:path'
55import process from 'node:process'
6- import { LAYER_MARK_ALL , RESOLVED_ID_RE } from '#integration/constants'
6+ import { LAYER_MARK_ALL } from '#integration/constants'
77import { setupContentExtractor } from '#integration/content'
88import { createContext } from '#integration/context'
99import { getHash } from '#integration/hash'
@@ -47,10 +47,11 @@ export function unplugin<Theme extends object>(configOrPath?: WebpackPluginOptio
4747 const plugin = {
4848 name : 'unocss:webpack' ,
4949 enforce : 'pre' ,
50- transformInclude ( id ) {
51- return filter ( '' , id ) && ! id . endsWith ( '.html' ) && ! RESOLVED_ID_RE . test ( id )
52- } ,
5350 async transform ( code , id ) {
51+ const { RESOLVED_ID_RE } = await ctx . getVMPRegexes ( )
52+ if ( RESOLVED_ID_RE . test ( id ) || ! filter ( '' , id ) || id . endsWith ( '.html' ) )
53+ return
54+
5455 const result = await applyTransformers ( ctx , code , id , 'pre' )
5556 if ( isCssId ( id ) )
5657 return result
@@ -60,8 +61,8 @@ export function unplugin<Theme extends object>(configOrPath?: WebpackPluginOptio
6061 tasks . push ( extract ( result . code , id ) )
6162 return result
6263 } ,
63- resolveId ( id ) {
64- const entry = resolveId ( id )
64+ async resolveId ( id ) {
65+ const entry = await resolveId ( ctx , id )
6566 if ( entry === id )
6667 return
6768 if ( entry ) {
@@ -74,16 +75,14 @@ export function unplugin<Theme extends object>(configOrPath?: WebpackPluginOptio
7475 return entry + query
7576 }
7677 } ,
77- loadInclude ( id ) {
78- const layer = getLayer ( id )
79- return ! ! layer
80- } ,
8178 // serve the placeholders in virtual module
82- load ( id ) {
83- const layer = getLayer ( id )
79+ async load ( id ) {
80+ const layer = await getLayer ( ctx , id )
81+ if ( ! layer )
82+ return
83+
8484 const hash = hashes . get ( id )
85- if ( layer )
86- return ( hash ? getHashPlaceholder ( hash ) : '' ) + getLayerPlaceholder ( layer )
85+ return ( hash ? getHashPlaceholder ( hash ) : '' ) + getLayerPlaceholder ( layer )
8786 } ,
8887 webpack ( compiler ) {
8988 compiler . hooks . beforeCompile . tapPromise ( PLUGIN_NAME , async ( ) => {
@@ -110,6 +109,9 @@ export function unplugin<Theme extends object>(configOrPath?: WebpackPluginOptio
110109
111110 await flushTasks ( )
112111 const result = await ctx . uno . generate ( tokens , { minify : true } )
112+ const resolvedLayers = ( await Promise . all ( Array . from ( entries )
113+ . map ( i => resolveLayer ( ctx , i ) ) ) )
114+ . filter ( ( i ) : i is string => ! ! i )
113115
114116 for ( const file of files ) {
115117 // https://github.com/unocss/unocss/pull/1428
@@ -123,8 +125,7 @@ export function unplugin<Theme extends object>(configOrPath?: WebpackPluginOptio
123125 code = code . replace ( LAYER_PLACEHOLDER_RE , ( _ , layer , escapeView ) => {
124126 replaced = true
125127 const css = layer . trim ( ) === LAYER_MARK_ALL
126- ? result . getLayers ( undefined , Array . from ( entries )
127- . map ( i => resolveLayer ( i ) ) . filter ( ( i ) : i is string => ! ! i ) )
128+ ? result . getLayers ( undefined , resolvedLayers )
128129 : ( result . getLayer ( layer ) || '' )
129130
130131 escapeCss = escapeCss ?? getCssEscaperForJsContent ( escapeView . trim ( ) )
@@ -161,36 +162,38 @@ export function unplugin<Theme extends object>(configOrPath?: WebpackPluginOptio
161162 virtualModules = Array . from ( plugin . __vfsModules )
162163 }
163164
164- virtualModules
165- . forEach ( ( id ) => {
166- let path = decodeURIComponent ( id . startsWith ( plugin . __virtualModulePrefix ) ? id . slice ( plugin . __virtualModulePrefix . length ) : id )
167- // unplugin changes the id in the `load` hook, follow it
168- // https://github.com/unjs/unplugin/pull/145/files#diff-2b106437404a793ee5b8f3823344656ce880f698d3d8cb6a7cf785e36fb4bf5cR27
169- path = normalizeAbsolutePath ( path )
170- const layer = resolveLayer ( path )
171- if ( ! layer )
172- return
173- const code = layer === LAYER_MARK_ALL
174- ? result . getLayers ( undefined , Array . from ( entries )
175- . map ( i => resolveLayer ( i ) ) . filter ( ( i ) : i is string => ! ! i ) )
176- : ( result . getLayer ( layer ) || '' )
177-
178- const hash = getHash ( code )
179- hashes . set ( path , hash )
180- plugin . __vfs . writeModule ( id , code )
181- } )
165+ const resolvedLayers = ( await Promise . all ( Array . from ( entries )
166+ . map ( i => resolveLayer ( ctx , i ) ) ) )
167+ . filter ( ( i ) : i is string => ! ! i )
168+
169+ for ( const id of virtualModules ) {
170+ let path = decodeURIComponent ( id . startsWith ( plugin . __virtualModulePrefix ) ? id . slice ( plugin . __virtualModulePrefix . length ) : id )
171+ // unplugin changes the id in the `load` hook, follow it
172+ // https://github.com/unjs/unplugin/pull/145/files#diff-2b106437404a793ee5b8f3823344656ce880f698d3d8cb6a7cf785e36fb4bf5cR27
173+ path = normalizeAbsolutePath ( path )
174+ const layer = await resolveLayer ( ctx , path )
175+ if ( ! layer )
176+ continue
177+ const code = layer === LAYER_MARK_ALL
178+ ? result . getLayers ( undefined , resolvedLayers )
179+ : ( result . getLayer ( layer ) || '' )
180+
181+ const hash = getHash ( code )
182+ hashes . set ( path , hash )
183+ plugin . __vfs . writeModule ( id , code )
184+ }
182185 }
183186
184187 return plugin
185188 } )
186189}
187190
188- function getLayer ( id : string ) {
189- let layer = resolveLayer ( getPath ( id ) )
191+ async function getLayer ( ctx : any , id : string ) {
192+ let layer = await resolveLayer ( ctx , getPath ( id ) )
190193 if ( ! layer ) {
191- const entry = resolveId ( id )
194+ const entry = await resolveId ( ctx , id )
192195 if ( entry )
193- layer = resolveLayer ( entry )
196+ layer = await resolveLayer ( ctx , entry )
194197 }
195198 return layer
196199}
0 commit comments