Skip to content

Commit

Permalink
fix(vite): keep the sequence of cssPlugins as is to prevent parallel …
Browse files Browse the repository at this point in the history
…issue when transforming (unocss#1065)
  • Loading branch information
Cubelrti committed Jun 7, 2022
1 parent 7d02870 commit 89b4e2b
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/vite/src/modes/global/build.ts
Expand Up @@ -16,17 +16,17 @@ export function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, fi
const vfsLayerMap = new Map<string, string>()
const layerImporterMap = new Map<string, string>()
let tasks: Promise<any>[] = []
let cssPostPlugin: Plugin | undefined
let cssPlugin: Plugin | undefined
let cssPostPlugins: Plugin[] = []
let cssPlugins: Plugin[] = []

async function transformCSS(css: string, id: string) {
const {
postcss = true,
} = await getConfig()
if (!cssPlugin || !postcss)
if (!cssPlugins.length || !postcss)
return css
// @ts-expect-error no this context
const result = await cssPlugin.transform(css, id)
const result = await cssPlugins.shift().transform(css, id)
if (!result)
return css
if (typeof result === 'string')
Expand Down Expand Up @@ -81,14 +81,14 @@ export function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, fi
}
},
async configResolved(config) {
cssPostPlugin = config.plugins.find(i => i.name === 'vite:css-post')
cssPlugin = config.plugins.find(i => i.name === 'vite:css')
cssPostPlugins.push(config.plugins.find(i => i.name === 'vite:css-post'))
cssPlugins.push(config.plugins.find(i => i.name === 'vite:css'))
await ready
},
// we inject a hash to chunk before the dist hash calculation to make sure
// the hash is different when unocss changes
async renderChunk(_, chunk) {
if (!cssPostPlugin)
if (!cssPostPlugins.length)
return null

const chunks = Object.keys(chunk.modules).filter(i => modules.has(i))
Expand All @@ -110,7 +110,7 @@ export function GlobalModeBuildPlugin({ uno, ready, extract, tokens, modules, fi

const hash = getHash(css)
// @ts-expect-error no this context
await cssPostPlugin.transform(getHashPlaceholder(hash), fakeCssId)
await cssPostPlugins.shift().transform(getHashPlaceholder(hash), fakeCssId)
// fool the css plugin to generate the css in corresponding chunk
chunk.modules[fakeCssId] = {
code: null,
Expand Down

0 comments on commit 89b4e2b

Please sign in to comment.