@@ -27,6 +27,7 @@ interface AstroPluginOptions {
27
27
export default function astro ( { settings, logger } : AstroPluginOptions ) : vite . Plugin [ ] {
28
28
const { config } = settings ;
29
29
let resolvedConfig : vite . ResolvedConfig ;
30
+ let server : vite . ViteDevServer ;
30
31
31
32
// Variables for determining if an id starts with /src...
32
33
const srcRootWeb = config . srcDir . pathname . slice ( config . root . pathname . length - 1 ) ;
@@ -38,6 +39,9 @@ export default function astro({ settings, logger }: AstroPluginOptions): vite.Pl
38
39
configResolved ( _resolvedConfig ) {
39
40
resolvedConfig = _resolvedConfig ;
40
41
} ,
42
+ configureServer ( _server ) {
43
+ server = _server ;
44
+ } ,
41
45
async load ( id , opts ) {
42
46
const parsedId = parseAstroRequest ( id ) ;
43
47
const query = parsedId . query ;
@@ -46,9 +50,18 @@ export default function astro({ settings, logger }: AstroPluginOptions): vite.Pl
46
50
}
47
51
// For CSS / hoisted scripts, the main Astro module should already be cached
48
52
const filename = normalizePath ( normalizeFilename ( parsedId . filename , config . root ) ) ;
49
- const compileResult = getCachedCompileResult ( config , filename ) ;
53
+ let compileResult = getCachedCompileResult ( config , filename ) ;
50
54
if ( ! compileResult ) {
51
- return null ;
55
+ // In dev, HMR could cause this compile result to be empty, try to load it first
56
+ if ( server ) {
57
+ await server . transformRequest ( '/@fs' + filename ) ;
58
+ compileResult = getCachedCompileResult ( config , filename ) ;
59
+ }
60
+
61
+ // If there's really no compilation result, error
62
+ if ( ! compileResult ) {
63
+ throw new Error ( 'No cached compile result found for ' + id ) ;
64
+ }
52
65
}
53
66
54
67
switch ( query . type ) {
0 commit comments