@@ -9,6 +9,8 @@ import { STYLE_EXTENSIONS } from '../util.js';
9
9
*/
10
10
const fileExtensionsToSSR = new Set ( [ '.astro' , '.md' ] ) ;
11
11
12
+ const STRIP_QUERY_PARAMS_REGEX = / \? .* $ / ;
13
+
12
14
/** recursively crawl the module graph to get all style files imported by parent id */
13
15
export async function * crawlGraph (
14
16
viteServer : vite . ViteDevServer ,
@@ -43,16 +45,18 @@ export async function* crawlGraph(
43
45
// to only SSR modules that we can safely transform, we check against
44
46
// a list of file extensions based on our built-in vite plugins
45
47
if ( importedModule . id ) {
46
- // use URL to strip special query params like "?content"
47
- const { pathname } = new URL ( `file://${ importedModule . id } ` ) ;
48
+ // Strip special query params like "?content".
49
+ // NOTE: Cannot use `new URL()` here because not all IDs will be valid paths.
50
+ // For example, `virtual:image-loader` if you don't have the plugin installed.
51
+ const importedModulePathname = importedModule . id . replace ( STRIP_QUERY_PARAMS_REGEX , '' ) ;
48
52
// If the entry is a style, skip any modules that are not also styles.
49
53
// Tools like Tailwind might add HMR dependencies as `importedModules`
50
54
// but we should skip them--they aren't really imported. Without this,
51
55
// every hoisted script in the project is added to every page!
52
- if ( entryIsStyle && ! STYLE_EXTENSIONS . has ( npath . extname ( pathname ) ) ) {
56
+ if ( entryIsStyle && ! STYLE_EXTENSIONS . has ( npath . extname ( importedModulePathname ) ) ) {
53
57
continue ;
54
58
}
55
- if ( fileExtensionsToSSR . has ( npath . extname ( pathname ) ) ) {
59
+ if ( fileExtensionsToSSR . has ( npath . extname ( importedModulePathname ) ) ) {
56
60
const mod = viteServer . moduleGraph . getModuleById ( importedModule . id ) ;
57
61
if ( ! mod ?. ssrModule ) {
58
62
await viteServer . ssrLoadModule ( importedModule . id ) ;
0 commit comments