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:
parent
d38b2a4fe8
commit
1bf0ddd277
2 changed files with 20 additions and 2 deletions
5
.changeset/friendly-needles-invite.md
Normal file
5
.changeset/friendly-needles-invite.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
"astro": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Improves HMR for Astro style and script modules
|
|
@ -27,6 +27,7 @@ interface AstroPluginOptions {
|
||||||
export default function astro({ settings, logger }: AstroPluginOptions): vite.Plugin[] {
|
export default function astro({ settings, logger }: AstroPluginOptions): vite.Plugin[] {
|
||||||
const { config } = settings;
|
const { config } = settings;
|
||||||
let resolvedConfig: vite.ResolvedConfig;
|
let resolvedConfig: vite.ResolvedConfig;
|
||||||
|
let server: vite.ViteDevServer;
|
||||||
|
|
||||||
// Variables for determining if an id starts with /src...
|
// Variables for determining if an id starts with /src...
|
||||||
const srcRootWeb = config.srcDir.pathname.slice(config.root.pathname.length - 1);
|
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) {
|
configResolved(_resolvedConfig) {
|
||||||
resolvedConfig = _resolvedConfig;
|
resolvedConfig = _resolvedConfig;
|
||||||
},
|
},
|
||||||
|
configureServer(_server) {
|
||||||
|
server = _server;
|
||||||
|
},
|
||||||
async load(id, opts) {
|
async load(id, opts) {
|
||||||
const parsedId = parseAstroRequest(id);
|
const parsedId = parseAstroRequest(id);
|
||||||
const query = parsedId.query;
|
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
|
// For CSS / hoisted scripts, the main Astro module should already be cached
|
||||||
const filename = normalizePath(normalizeFilename(parsedId.filename, config.root));
|
const filename = normalizePath(normalizeFilename(parsedId.filename, config.root));
|
||||||
const compileResult = getCachedCompileResult(config, filename);
|
let compileResult = getCachedCompileResult(config, filename);
|
||||||
if (!compileResult) {
|
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) {
|
switch (query.type) {
|
||||||
|
|
Loading…
Reference in a new issue