From f55b4829bff86baec1b467be86f1be7561d09c81 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Mon, 13 Mar 2023 12:09:56 +0100 Subject: [PATCH] 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 --- packages/astro/src/assets/internal.ts | 42 ------------------ packages/astro/src/assets/utils/emitAsset.ts | 43 +++++++++++++++++++ .../astro/src/assets/vite-plugin-assets.ts | 3 +- packages/astro/src/content/utils.ts | 2 +- 4 files changed, 46 insertions(+), 44 deletions(-) create mode 100644 packages/astro/src/assets/utils/emitAsset.ts diff --git a/packages/astro/src/assets/internal.ts b/packages/astro/src/assets/internal.ts index 3bad39ae4..343c9e8b5 100644 --- a/packages/astro/src/assets/internal.ts +++ b/packages/astro/src/assets/internal.ts @@ -1,13 +1,8 @@ import fs from 'node:fs'; -import path from 'node:path'; -import { pathToFileURL } from 'node:url'; -import type { AstroSettings } from '../@types/astro.js'; import type { StaticBuildOptions } from '../core/build/types.js'; import { AstroError, AstroErrorData } from '../core/errors/index.js'; -import { rootRelativePath } from '../core/util.js'; import { ImageService, isLocalService, LocalImageService } from './services/service.js'; import type { ImageMetadata, ImageTransform } from './types.js'; -import { imageMetadata } from './utils/metadata.js'; export function isESMImportedImage(src: ImageMetadata | string): src is ImageMetadata { return typeof src === 'object'; @@ -120,40 +115,3 @@ export async function generateImage( }, }; } - -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; -} diff --git a/packages/astro/src/assets/utils/emitAsset.ts b/packages/astro/src/assets/utils/emitAsset.ts new file mode 100644 index 000000000..74b851eed --- /dev/null +++ b/packages/astro/src/assets/utils/emitAsset.ts @@ -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; +} diff --git a/packages/astro/src/assets/vite-plugin-assets.ts b/packages/astro/src/assets/vite-plugin-assets.ts index f3c022e61..33b081548 100644 --- a/packages/astro/src/assets/vite-plugin-assets.ts +++ b/packages/astro/src/assets/vite-plugin-assets.ts @@ -9,9 +9,10 @@ import type { AstroPluginOptions, ImageTransform } from '../@types/astro'; import { error } from '../core/logger/core.js'; import { joinPaths, prependForwardSlash } from '../core/path.js'; import { VIRTUAL_MODULE_ID, VIRTUAL_SERVICE_ID } from './consts.js'; -import { emitESMImage, isESMImportedImage } from './internal.js'; +import { isESMImportedImage } from './internal.js'; import { isLocalService } from './services/service.js'; import { copyWasmFiles } from './services/vendor/squoosh/copy-wasm.js'; +import { emitESMImage } from './utils/emitAsset.js'; import { imageMetadata } from './utils/metadata.js'; import { getOrigQueryParams } from './utils/queryParams.js'; import { propsToFilename } from './utils/transformToPath.js'; diff --git a/packages/astro/src/content/utils.ts b/packages/astro/src/content/utils.ts index 1fd600748..00d0f61e4 100644 --- a/packages/astro/src/content/utils.ts +++ b/packages/astro/src/content/utils.ts @@ -7,7 +7,7 @@ import type { EmitFile } from 'rollup'; import { ErrorPayload as ViteErrorPayload, normalizePath, ViteDevServer } from 'vite'; import { z } from 'zod'; import type { AstroConfig, AstroSettings } from '../@types/astro.js'; -import { emitESMImage } from '../assets/internal.js'; +import { emitESMImage } from '../assets/utils/emitAsset.js'; import { AstroError, AstroErrorData } from '../core/errors/index.js'; import { CONTENT_TYPES_FILE } from './consts.js';