Skip to content

Commit

Permalink
fix(astro): incorrect path and build failure (#2648)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed May 22, 2023
1 parent 2916a95 commit 1c61fac
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions packages/astro/src/index.ts
Expand Up @@ -4,7 +4,7 @@ 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 } from 'vite'
import type { Plugin, ResolvedConfig } from 'vite'

const UNO_INJECT_ID = 'uno-astro'
const UNO_QUERY_KEY = 'uno-with-astro-key'
Expand All @@ -15,15 +15,25 @@ interface AstroVitePluginOptions {

function AstroVitePlugin(options: AstroVitePluginOptions): Plugin {
const { injects } = options

let config: ResolvedConfig

return {
name: 'unocss:astro',
apply: 'serve',
enforce: 'pre',
resolveId(id, importer) {
configResolved(_config) {
config = _config
},
async resolveId(id, importer) {
if (id === UNO_INJECT_ID)
return id
if (importer?.endsWith(UNO_INJECT_ID))
return `${id}${id.includes('?') ? '&' : '?'}${UNO_QUERY_KEY}`
if (importer?.endsWith(UNO_INJECT_ID) && config && config.command === 'serve') {
const resolved = await this.resolve(id, importer, { skipSelf: true })
if (resolved) {
const fsPath = resolved.id
return `${fsPath}${fsPath.includes('?') ? '&' : '?'}${UNO_QUERY_KEY}`
}
}
},
load(id, options) {
if (id.endsWith(UNO_INJECT_ID))
Expand Down

0 comments on commit 1c61fac

Please sign in to comment.