diff --git a/packages/astro/src/index.ts b/packages/astro/src/index.ts index bee67c5e1f..18c4eaf458 100644 --- a/packages/astro/src/index.ts +++ b/packages/astro/src/index.ts @@ -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)) { @@ -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)) @@ -153,7 +100,6 @@ export default function UnoCSSAstroIntegration( updateConfig({ vite: { plugins: [AstroVitePlugin({ - injectReset: !!injectReset, injects, }), ...VitePlugin(options, defaults)], },