Skip to content

Commit

Permalink
fix(vite): compactiablity with astro
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Nov 22, 2021
1 parent 7c98f56 commit 5c06d83
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 6 additions & 0 deletions packages/plugins-common/layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export function resolveId(id: string) {
}
}

export function getLayer(id: string) {
if (id === '/__uno.css')
return LAYER_MARK_ALL
return id.match(/^\/__uno_(\w+)\.css$/)?.[1]
}

export const LAYER_PLACEHOLDER_RE = /(\\?")?#--unocss--\s*{\s*layer\s*:\s*(.+?);?\s*}/g
export function getLayerPlaceholder(layer: string) {
return `#--unocss--{layer:${layer}}`
Expand Down
13 changes: 10 additions & 3 deletions packages/vite/src/modes/global/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@ import { getHash, getPath } from '../../../../plugins-common/utils'
import { UnocssPluginContext } from '../../../../plugins-common/context'
import {
getHashPlaceholder,
getLayer,
getLayerPlaceholder,
HASH_PLACEHOLDER_RE,
LAYER_MARK_ALL,
LAYER_PLACEHOLDER_RE,
resolveId,
} from '../../../../plugins-common'

const cssPluginIds = [
'vite:css-post',
// astro compact
'@astrojs/rollup-plugin-build-css',
]

export function GlobalModeBuildPlugin({ uno, extract, tokens, modules, filter }: UnocssPluginContext): Plugin[] {
const vfsLayerMap = new Map<string, string>()
let tasks: Promise<any>[] = []
let cssPlugin: Plugin | undefined

return [
{
name: 'unocss:global:build:scan',
name: 'unocss:global:build:extra',
apply: 'build',
enforce: 'pre',
buildStart() {
Expand All @@ -43,12 +50,12 @@ export function GlobalModeBuildPlugin({ uno, extract, tokens, modules, filter }:
}
},
load(id) {
const layer = vfsLayerMap.get(getPath(id))
const layer = vfsLayerMap.get(getPath(id)) || getLayer(id)
if (layer)
return getLayerPlaceholder(layer)
},
configResolved(config) {
cssPlugin = config.plugins.find(i => i.name === 'vite:css-post')
cssPlugin = config.plugins.find(i => cssPluginIds.includes(i.name))
},
// we inject a hash to chunk before the dist hash calculation to make sure
// the hash is different when unocss changes
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"preview": "astro preview"
},
"dependencies": {
"astro": "^0.21.0-next.7"
"astro": "^0.21.0"
}
}

0 comments on commit 5c06d83

Please sign in to comment.