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

Add fallback compile for astro script and style load #9664

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

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

this is inside load, this doesn't cause a loop?

Copy link
Member Author

Choose a reason for hiding this comment

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

The id of this load is something like /Users/bjorn/project/src/pages/index.astro/?astro&lang.css, but the filename here would be /Users/bjorn/project/src/pages/index.astro. So it won't cause a loop and should only transform the main Astro file so we have the cache ready.

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