summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@skypack.dev> 2023-03-08 15:16:13 -0500
committerGravatar GitHub <noreply@github.com> 2023-03-08 15:16:13 -0500
commit65c07ce1b6ab8db50d3866bc36c2e387a9281c6c (patch)
tree11cc25dd866ffa8e3c70adb003077db971904c4b
parent9d48328287348a2d00103807352d66c405c7bc7e (diff)
downloadastro-65c07ce1b6ab8db50d3866bc36c2e387a9281c6c.tar.gz
astro-65c07ce1b6ab8db50d3866bc36c2e387a9281c6c.tar.zst
astro-65c07ce1b6ab8db50d3866bc36c2e387a9281c6c.zip
Fixes ESM imported assets to be root relative (#6465)
* Fixes ESM imported assets to be root relative * Add changeset
-rw-r--r--.changeset/smart-crews-swim.md5
-rw-r--r--packages/astro/src/assets/vite-plugin-assets.ts3
-rw-r--r--packages/astro/src/core/util.ts10
-rw-r--r--packages/astro/test/core-image.test.js18
-rw-r--r--packages/astro/test/fixtures/core-image/src/pages/regular-img.astro5
5 files changed, 40 insertions, 1 deletions
diff --git a/.changeset/smart-crews-swim.md b/.changeset/smart-crews-swim.md
new file mode 100644
index 000000000..7f7234b62
--- /dev/null
+++ b/.changeset/smart-crews-swim.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fixes ESM imported assets to be root relative
diff --git a/packages/astro/src/assets/vite-plugin-assets.ts b/packages/astro/src/assets/vite-plugin-assets.ts
index 770e43400..020ac6758 100644
--- a/packages/astro/src/assets/vite-plugin-assets.ts
+++ b/packages/astro/src/assets/vite-plugin-assets.ts
@@ -9,6 +9,7 @@ import { normalizePath } from 'vite';
import { AstroPluginOptions, ImageTransform } from '../@types/astro';
import { error } from '../core/logger/core.js';
import { joinPaths, prependForwardSlash } from '../core/path.js';
+import { rootRelativePath } from '../core/util.js';
import { VIRTUAL_MODULE_ID, VIRTUAL_SERVICE_ID } from './consts.js';
import { isESMImportedImage } from './internal.js';
import { isLocalService } from './services/service.js';
@@ -223,7 +224,7 @@ export default function assets({
url.searchParams.append('origHeight', meta.height.toString());
url.searchParams.append('origFormat', meta.format);
- meta.src = url.toString();
+ meta.src = rootRelativePath(settings.config, url);
}
return `export default ${JSON.stringify(meta)}`;
diff --git a/packages/astro/src/core/util.ts b/packages/astro/src/core/util.ts
index a7152e41a..51abe62c1 100644
--- a/packages/astro/src/core/util.ts
+++ b/packages/astro/src/core/util.ts
@@ -151,6 +151,16 @@ export function relativeToSrcDir(config: AstroConfig, idOrUrl: URL | string) {
return id.slice(slash(fileURLToPath(config.srcDir)).length);
}
+export function rootRelativePath(config: AstroConfig, idOrUrl: URL | string) {
+ let id: string;
+ if (typeof idOrUrl !== 'string') {
+ id = unwrapId(viteID(idOrUrl));
+ } else {
+ id = idOrUrl;
+ }
+ return prependForwardSlash(id.slice(normalizePath(fileURLToPath(config.root)).length));
+}
+
export function emoji(char: string, fallback: string) {
return process.platform !== 'win32' ? char : fallback;
}
diff --git a/packages/astro/test/core-image.test.js b/packages/astro/test/core-image.test.js
index cbdfae92f..d6b6642c3 100644
--- a/packages/astro/test/core-image.test.js
+++ b/packages/astro/test/core-image.test.js
@@ -212,6 +212,24 @@ describe('astro:image', () => {
expect($img.attr('src').startsWith('/_image')).to.equal(true);
});
});
+
+ describe('regular img tag', () => {
+ /** @type {ReturnType<import('cheerio')['load']>} */
+ let $;
+ before(async () => {
+ let res = await fixture.fetch('/regular-img');
+ let html = await res.text();
+ $ = cheerio.load(html);
+ });
+
+ it('does not have a file url', async () => {
+ expect($('img').attr('src').startsWith('file://')).to.equal(false);
+ });
+
+ it('includes /src in the path', async () => {
+ expect($('img').attr('src').startsWith('/src')).to.equal(true);
+ });
+ });
});
describe('build ssg', () => {
diff --git a/packages/astro/test/fixtures/core-image/src/pages/regular-img.astro b/packages/astro/test/fixtures/core-image/src/pages/regular-img.astro
new file mode 100644
index 000000000..eacf14e60
--- /dev/null
+++ b/packages/astro/test/fixtures/core-image/src/pages/regular-img.astro
@@ -0,0 +1,5 @@
+---
+import image from "../assets/penguin2.jpg";
+---
+
+<img src={image.src} />