Skip to content

Commit

Permalink
Add fallback compile for astro script and style load (#9664)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Jan 15, 2024
1 parent d38b2a4 commit 1bf0ddd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-needles-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Improves HMR for Astro style and script modules
17 changes: 15 additions & 2 deletions packages/astro/src/vite-plugin-astro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit 1bf0ddd

Please sign in to comment.