Skip to content

Commit 42fd693

Browse files
authored
fix astro image bad imports (#4279)
1 parent e65c772 commit 42fd693

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

.changeset/real-feet-rule.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Handle "not found" imports without throwing an "Invalid URL" error

.changeset/strange-meals-march.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/image': patch
3+
---
4+
5+
Add better warnings if the integration was not properly configured.

packages/astro/src/core/render/dev/vite.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { STYLE_EXTENSIONS } from '../util.js';
99
*/
1010
const fileExtensionsToSSR = new Set(['.astro', '.md']);
1111

12+
const STRIP_QUERY_PARAMS_REGEX = /\?.*$/;
13+
1214
/** recursively crawl the module graph to get all style files imported by parent id */
1315
export async function* crawlGraph(
1416
viteServer: vite.ViteDevServer,
@@ -43,16 +45,18 @@ export async function* crawlGraph(
4345
// to only SSR modules that we can safely transform, we check against
4446
// a list of file extensions based on our built-in vite plugins
4547
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, '');
4852
// If the entry is a style, skip any modules that are not also styles.
4953
// Tools like Tailwind might add HMR dependencies as `importedModules`
5054
// but we should skip them--they aren't really imported. Without this,
5155
// 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))) {
5357
continue;
5458
}
55-
if (fileExtensionsToSSR.has(npath.extname(pathname))) {
59+
if (fileExtensionsToSSR.has(npath.extname(importedModulePathname))) {
5660
const mod = viteServer.moduleGraph.getModuleById(importedModule.id);
5761
if (!mod?.ssrModule) {
5862
await viteServer.ssrLoadModule(importedModule.id);

packages/integrations/image/src/lib/get-image.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ export async function getImage(
107107

108108
if (!loader) {
109109
// @ts-ignore
110-
const { default: mod } = await import('virtual:image-loader');
110+
const { default: mod } = await import('virtual:image-loader').catch(() => {
111+
throw new Error('[@astrojs/image] Builtin image loader not found. (Did you remember to add the integration to your Astro config?)');
112+
});
111113
loader = mod as ImageService;
112114
globalThis.astroImage = globalThis.astroImage || {};
113115
globalThis.astroImage.loader = loader;

0 commit comments

Comments
 (0)