-
-
Notifications
You must be signed in to change notification settings - Fork 779
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
||
const UNO_INJECT_ID = 'uno-astro' | ||
const UNO_QUERY_KEY = 'uno-with-astro-key' | ||
const astroCSSKeyRE = /(\?|\&)lang\.css/ | ||
|
||
interface AstroVitePluginOptions { | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we move this logic to vite plugin? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
/** | ||
|
@@ -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)) { | ||
|
@@ -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 '' | ||
}, | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure, the same usage is found in the vite plugin
There was a problem hiding this comment.
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