-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(images): Move emitESMImage util to another file (#6505)
* fix(images): Move emitESMImage util to another file, so we don't import it by accident in contexts we shouldn.t * fix(images): Fix import paths
- Loading branch information
1 parent
f6eddff
commit f55b482
Showing
4 changed files
with
46 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import { pathToFileURL } from 'node:url'; | ||
import type { AstroSettings } from '../../@types/astro'; | ||
import { rootRelativePath } from '../../core/util.js'; | ||
import { imageMetadata } from './metadata.js'; | ||
|
||
export async function emitESMImage( | ||
id: string, | ||
watchMode: boolean, | ||
fileEmitter: any, | ||
settings: AstroSettings | ||
) { | ||
const url = pathToFileURL(id); | ||
const meta = await imageMetadata(url); | ||
|
||
if (!meta) { | ||
return; | ||
} | ||
|
||
// Build | ||
if (!watchMode) { | ||
const pathname = decodeURI(url.pathname); | ||
const filename = path.basename(pathname, path.extname(pathname) + `.${meta.format}`); | ||
|
||
const handle = fileEmitter({ | ||
name: filename, | ||
source: await fs.promises.readFile(url), | ||
type: 'asset', | ||
}); | ||
|
||
meta.src = `__ASTRO_ASSET_IMAGE__${handle}__`; | ||
} else { | ||
// Pass the original file information through query params so we don't have to load the file twice | ||
url.searchParams.append('origWidth', meta.width.toString()); | ||
url.searchParams.append('origHeight', meta.height.toString()); | ||
url.searchParams.append('origFormat', meta.format); | ||
|
||
meta.src = rootRelativePath(settings.config, url); | ||
} | ||
|
||
return meta; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters