Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(astro): astro hmr warning and improve performance #2989

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 15 additions & 20 deletions packages/astro/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { resolve } from 'node:path'
import { join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
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 { RESOLVED_ID_RE } from '../../shared-integration/src/layers'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhmm, shouldn't use shared package?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhmm, shouldn't use shared package?

I'm not sure, the same usage is found in the vite plugin

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok to have this, they file will be inline and bundled with this package


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

interface AstroVitePluginOptions {
Expand All @@ -19,18 +19,24 @@ function AstroVitePlugin(options: AstroVitePluginOptions): Plugin {
const { injects, injectReset } = options
const resetInjectPath = injectReset ? injects[0] : undefined
let command: ResolvedConfig['command']
let nodeModulesPath: string
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
nodeModulesPath = `${config.root}/node_modules`
},
async resolveId(id, importer) {
if (RESOLVED_ID_RE.test(id)) {
// https://github.com/withastro/astro/blob/087270c61fd5c91ddd37db5c8fd93a8a0ef41f94/packages/astro/src/core/util.ts#L91-L93
// Align data-astro-dev-id with data-vite-dev-id to fix https://github.com/unocss/unocss/issues/2513
return join(root, id)
}
Comment on lines +34 to +38
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this logic to vite plugin?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have it here, until if there is an actually issue on the Vite side


if (id === UNO_INJECT_ID) {
if (injectReset) {
/**
Expand All @@ -54,7 +60,6 @@ function AstroVitePlugin(options: AstroVitePluginOptions): Plugin {
const resolved = await this.resolve(id, importer, { skipSelf: true })
if (resolved) {
const fsPath = resolved.id
const realId = `${fsPath}${fsPath.includes('?') ? '&' : '?'}${UNO_QUERY_KEY}`

if (injectReset) {
if (resetInjectPath!.includes(id)) {
Expand All @@ -70,30 +75,20 @@ function AstroVitePlugin(options: AstroVitePluginOptions): Plugin {
// css need to be injected after reset style
else if (id.includes('.css') && !resetCSSInjected) {
return new Promise((resolve) => {
resolveCSSQueue.add(() => resolve(realId))
resolveCSSQueue.add(() => {
resolve(fsPath)
})
})
}
}

return realId
return fsPath
}
}
},
load(id, options) {
load(id) {
if (id.endsWith(UNO_INJECT_ID))
return injects.join('\n')

if (
!options?.ssr
&& id.includes(UNO_QUERY_KEY)
&& id.includes('.css')
/**
* If the module in node_modules, astro will reuse same style tag
* @see https://github.com/unocss/unocss/issues/2655
*/
&& !id.startsWith(nodeModulesPath)
)
return ''
},
}
}
Expand Down