mirror of
https://github.com/withastro/astro.git
synced 2024-12-23 21:53:55 -05:00
Fixes ESM imported assets to be root relative (#6465)
* Fixes ESM imported assets to be root relative * Add changeset
This commit is contained in:
parent
9d48328287
commit
65c07ce1b6
5 changed files with 40 additions and 1 deletions
5
.changeset/smart-crews-swim.md
Normal file
5
.changeset/smart-crews-swim.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixes ESM imported assets to be root relative
|
|
@ -9,6 +9,7 @@ import { normalizePath } from 'vite';
|
||||||
import { AstroPluginOptions, ImageTransform } from '../@types/astro';
|
import { AstroPluginOptions, ImageTransform } from '../@types/astro';
|
||||||
import { error } from '../core/logger/core.js';
|
import { error } from '../core/logger/core.js';
|
||||||
import { joinPaths, prependForwardSlash } from '../core/path.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 { VIRTUAL_MODULE_ID, VIRTUAL_SERVICE_ID } from './consts.js';
|
||||||
import { isESMImportedImage } from './internal.js';
|
import { isESMImportedImage } from './internal.js';
|
||||||
import { isLocalService } from './services/service.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('origHeight', meta.height.toString());
|
||||||
url.searchParams.append('origFormat', meta.format);
|
url.searchParams.append('origFormat', meta.format);
|
||||||
|
|
||||||
meta.src = url.toString();
|
meta.src = rootRelativePath(settings.config, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
return `export default ${JSON.stringify(meta)}`;
|
return `export default ${JSON.stringify(meta)}`;
|
||||||
|
|
|
@ -151,6 +151,16 @@ export function relativeToSrcDir(config: AstroConfig, idOrUrl: URL | string) {
|
||||||
return id.slice(slash(fileURLToPath(config.srcDir)).length);
|
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) {
|
export function emoji(char: string, fallback: string) {
|
||||||
return process.platform !== 'win32' ? char : fallback;
|
return process.platform !== 'win32' ? char : fallback;
|
||||||
}
|
}
|
||||||
|
|
|
@ -212,6 +212,24 @@ describe('astro:image', () => {
|
||||||
expect($img.attr('src').startsWith('/_image')).to.equal(true);
|
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', () => {
|
describe('build ssg', () => {
|
||||||
|
|
5
packages/astro/test/fixtures/core-image/src/pages/regular-img.astro
vendored
Normal file
5
packages/astro/test/fixtures/core-image/src/pages/regular-img.astro
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
import image from "../assets/penguin2.jpg";
|
||||||
|
---
|
||||||
|
|
||||||
|
<img src={image.src} />
|
Loading…
Reference in a new issue