Skip to content

Commit f48862b

Browse files
authored
fix(vite): fix asset emit (#1926)
* fix(vite): fix asset emit * chore: rename plugin
1 parent daba01c commit f48862b

File tree

4 files changed

+72
-12
lines changed

4 files changed

+72
-12
lines changed

examples/vite-vue3/public/uno.svg

Lines changed: 11 additions & 0 deletions
Loading

examples/vite-vue3/src/App.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
</div>
77
<div i-custom-icon />
88
<div i-custom-multi-line-attr />
9+
<div hidden class="bg-[url(../src/uno.svg)] bg-[url(/uno.svg)]" />
910
</div>
1011
</template>

examples/vite-vue3/src/uno.svg

Lines changed: 11 additions & 0 deletions
Loading

packages/vite/src/modes/global/build.ts

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { resolve } from 'path'
22
import type { Plugin, ResolvedConfig } from 'vite'
33
import type { GenerateResult, UnocssPluginContext } from '@unocss/core'
4+
import type { PluginContext } from 'rollup'
45
import {
56
HASH_PLACEHOLDER_RE, LAYER_MARK_ALL, LAYER_PLACEHOLDER_RE,
67
RESOLVED_ID_RE,
@@ -24,14 +25,14 @@ export function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, get
2425
const cssPostPlugins = new Map<string | undefined, Plugin | undefined>()
2526
const cssPlugins = new Map<string | undefined, Plugin | undefined>()
2627

27-
async function applyCssTransform(css: string, id: string, dir: string | undefined) {
28+
async function applyCssTransform(css: string, id: string, dir: string | undefined, ctx: PluginContext) {
2829
const {
2930
postcss = true,
3031
} = await getConfig()
3132
if (!cssPlugins.get(dir) || !postcss)
3233
return css
33-
// @ts-expect-error no this context
34-
const result = await cssPlugins.get(dir).transform(css, id)
34+
// @ts-expect-error without this context absolute assets will throw an error
35+
const result = await cssPlugins.get(dir).transform.call(ctx, css, id)
3536
if (!result)
3637
return css
3738
if (typeof result === 'string')
@@ -122,7 +123,7 @@ export function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, get
122123

123124
let { css } = await generateAll()
124125
const fakeCssId = `${chunk.fileName}-unocss-hash.css`
125-
css = await applyCssTransform(css, fakeCssId, options.dir)
126+
css = await applyCssTransform(css, fakeCssId, options.dir, this)
126127

127128
const hash = getHash(css)
128129
const transformHandler = 'handler' in cssPost.transform!
@@ -145,6 +146,32 @@ export function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, get
145146
{
146147
name: 'unocss:global:build:generate',
147148
apply: 'build',
149+
async renderChunk(code, chunk, options) {
150+
const cssPost = cssPostPlugins.get(options.dir)
151+
if (!cssPost) {
152+
this.warn('[unocss] failed to find vite:css-post plugin. It might be an internal bug of UnoCSS')
153+
return null
154+
}
155+
const result = await generateAll()
156+
const cssWithLayers = Array.from(vfsLayers).map(layer =>
157+
`#--unocss-layer-start--${layer}--{start:${layer}} ${
158+
layer === LAYER_MARK_ALL
159+
? result.getLayers(undefined, Array.from(vfsLayers))
160+
: result.getLayer(layer) || ''
161+
} #--unocss-layer-end--${layer}--{end:${layer}}`,
162+
).join('')
163+
164+
const fakeCssId = `${chunk.fileName}-unocss-hash.css`
165+
const css = await applyCssTransform(cssWithLayers, fakeCssId, options.dir, this)
166+
const transformHandler = 'handler' in cssPost.transform!
167+
? cssPost.transform.handler
168+
: cssPost.transform!
169+
await transformHandler.call({} as unknown as any, css, fakeCssId)
170+
},
171+
},
172+
{
173+
name: 'unocss:global:build:bundle',
174+
apply: 'build',
148175
configResolved(config) {
149176
viteConfig = config
150177
},
@@ -164,34 +191,44 @@ export function GlobalModeBuildPlugin({ uno, ready, extract, tokens, filter, get
164191
return
165192
}
166193

167-
const result = await generateAll()
168194
let replaced = false
195+
const getLayer = (layer: string, input: string, replace = false) => {
196+
const re = new RegExp(`#--unocss-layer-start--${layer}--\\{start:${layer}\\}([\\s\\S]*?)#--unocss-layer-end--${layer}--\\{end:${layer}\\}`, 'g')
197+
if (replace)
198+
return input.replace(re, '')
199+
200+
const match = re.exec(input)
201+
if (match)
202+
return match[1]
203+
return ''
204+
}
169205

170206
for (const file of files) {
171207
const chunk = bundle[file]
172-
173208
if (chunk.type === 'asset' && typeof chunk.source === 'string') {
174209
const css = chunk.source
175210
.replace(HASH_PLACEHOLDER_RE, '')
176211
chunk.source = await replaceAsync(css, LAYER_PLACEHOLDER_RE, async (_, __, layer) => {
177212
replaced = true
178-
return layer === LAYER_MARK_ALL
179-
? result.getLayers(undefined, Array.from(vfsLayers))
180-
: result.getLayer(layer) || ''
213+
return getLayer(layer, css)
214+
})
215+
Array.from(vfsLayers).forEach((layer) => {
216+
chunk.source = getLayer(layer, chunk.source as string, true)
181217
})
182218
}
183219
else if (chunk.type === 'chunk' && typeof chunk.code === 'string') {
184220
const js = chunk.code
185221
.replace(HASH_PLACEHOLDER_RE, '')
186222
chunk.code = await replaceAsync(js, LAYER_PLACEHOLDER_RE, async (_, __, layer) => {
187223
replaced = true
188-
const css = layer === LAYER_MARK_ALL
189-
? result.getLayers(undefined, Array.from(vfsLayers))
190-
: result.getLayer(layer) || ''
224+
const css = getLayer(layer, js)
191225
return css
192226
.replace(/\n/g, '')
193227
.replace(/(?<!\\)(['"])/g, '\\$1')
194228
})
229+
Array.from(vfsLayers).forEach((layer) => {
230+
chunk.code = getLayer(layer, chunk.code, true)
231+
})
195232
}
196233
}
197234

0 commit comments

Comments
 (0)