Skip to content

Commit

Permalink
fix(astro): remove the code that fixes the css ordering (#3172)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Oct 7, 2023
1 parent f941611 commit f0bbe3d
Showing 1 changed file with 3 additions and 57 deletions.
60 changes: 3 additions & 57 deletions packages/astro/src/index.ts
Expand Up @@ -4,32 +4,25 @@ import type { AstroIntegration } from 'astro'
import type { VitePluginConfig } from '@unocss/vite'
import VitePlugin from '@unocss/vite'
import type { UserConfigDefaults } from '@unocss/core'
import type { Plugin, ResolvedConfig } from 'vite'
import type { Plugin } from 'vite'
import { normalizePath } from 'vite'
import { RESOLVED_ID_RE } from '../../shared-integration/src/layers'

const UNO_INJECT_ID = 'uno-astro'
const astroCSSKeyRE = /(\?|\&)lang\.css/

interface AstroVitePluginOptions {
injects: string[]
injectReset: boolean
}

function AstroVitePlugin(options: AstroVitePluginOptions): Plugin {
const { injects, injectReset } = options
const resetInjectPath = injectReset ? injects[0] : undefined
let command: ResolvedConfig['command']
const { injects } = options
let root: string
let resetCSSInjected = false
const resolveCSSQueue = new Set<() => void>()

return {
name: 'unocss:astro',
enforce: 'pre',
configResolved(config) {
root = config.root
command = config.command
},
async resolveId(id, importer) {
if (RESOLVED_ID_RE.test(id)) {
Expand All @@ -38,54 +31,8 @@ function AstroVitePlugin(options: AstroVitePluginOptions): Plugin {
return this.resolve(normalizePath(join(root, id)), importer, { skipSelf: true })
}

if (id === UNO_INJECT_ID) {
if (injectReset) {
/**
* When running here, means that this is a new file.
* We need to make sure that the reset css for each file
* needs to be loaded first.
*/
resetCSSInjected = false
}
if (id === UNO_INJECT_ID)
return id
}

if (
injectReset && command === 'serve'
// css need to be injected after reset style
&& astroCSSKeyRE.test(id) && !resetCSSInjected
)
return new Promise(resolve => resolveCSSQueue.add(() => resolve()))

if (importer?.endsWith(UNO_INJECT_ID) && command === 'serve') {
const resolved = await this.resolve(id, importer, { skipSelf: true })
if (resolved) {
const fsPath = resolved.id

if (injectReset) {
if (resetInjectPath!.includes(id)) {
// Make sure the reset style is injected first
setTimeout(() => {
resolveCSSQueue.forEach((res) => {
res()
resolveCSSQueue.delete(res)
})
})
resetCSSInjected = true
}
// css need to be injected after reset style
else if (id.includes('.css') && !resetCSSInjected) {
return new Promise((resolve) => {
resolveCSSQueue.add(() => {
resolve(fsPath)
})
})
}
}

return fsPath
}
}
},
load(id) {
if (id.endsWith(UNO_INJECT_ID))
Expand Down Expand Up @@ -153,7 +100,6 @@ export default function UnoCSSAstroIntegration<Theme extends object>(
updateConfig({
vite: {
plugins: [AstroVitePlugin({
injectReset: !!injectReset,
injects,
}), ...VitePlugin(options, defaults)],
},
Expand Down

0 comments on commit f0bbe3d

Please sign in to comment.