0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

Add fallback compile for astro script and style load (#9664)

This commit is contained in:
Bjorn Lu 2024-01-15 23:26:42 +08:00 committed by GitHub
parent d38b2a4fe8
commit 1bf0ddd277
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Improves HMR for Astro style and script modules

View file

@ -27,6 +27,7 @@ interface AstroPluginOptions {
export default function astro({ settings, logger }: AstroPluginOptions): vite.Plugin[] {
const { config } = settings;
let resolvedConfig: vite.ResolvedConfig;
let server: vite.ViteDevServer;
// Variables for determining if an id starts with /src...
const srcRootWeb = config.srcDir.pathname.slice(config.root.pathname.length - 1);
@ -38,6 +39,9 @@ export default function astro({ settings, logger }: AstroPluginOptions): vite.Pl
configResolved(_resolvedConfig) {
resolvedConfig = _resolvedConfig;
},
configureServer(_server) {
server = _server;
},
async load(id, opts) {
const parsedId = parseAstroRequest(id);
const query = parsedId.query;
@ -46,9 +50,18 @@ export default function astro({ settings, logger }: AstroPluginOptions): vite.Pl
}
// For CSS / hoisted scripts, the main Astro module should already be cached
const filename = normalizePath(normalizeFilename(parsedId.filename, config.root));
const compileResult = getCachedCompileResult(config, filename);
let compileResult = getCachedCompileResult(config, filename);
if (!compileResult) {
return null;
// In dev, HMR could cause this compile result to be empty, try to load it first
if (server) {
await server.transformRequest('/@fs' + filename);
compileResult = getCachedCompileResult(config, filename);
}
// If there's really no compilation result, error
if (!compileResult) {
throw new Error('No cached compile result found for ' + id);
}
}
switch (query.type) {