diff --git a/packages/integrations/image/src/index.ts b/packages/integrations/image/src/index.ts index 43c7d8997..d54fd9494 100644 --- a/packages/integrations/image/src/index.ts +++ b/packages/integrations/image/src/index.ts @@ -4,7 +4,7 @@ import type { ImageService, TransformOptions } from './loaders/index.js'; import type { LoggerLevel } from './utils/logger.js'; import { joinPaths, prependForwardSlash, propsToFilename } from './utils/paths.js'; import { createPlugin } from './vite-plugin-astro-image.js'; -import { copyLibFiles } from './vendor/squoosh/main.js'; +import { copyWasmFiles } from './vendor/squoosh/copy-wasm.js'; export { getImage } from './lib/get-image.js'; export { getPicture } from './lib/get-picture.js'; @@ -107,7 +107,7 @@ export default function integration(options: IntegrationOptions = {}): AstroInte }, 'astro:build:done': async ({ dir }) => { if (resolvedOptions.serviceEntryPoint === '@astrojs/image/squoosh') { - await copyLibFiles(_config.output === 'static' ? dir : _buildConfig.server); + await copyWasmFiles(_config.output === 'static' ? dir : _buildConfig.server); } if (_config.output === 'static') { diff --git a/packages/integrations/image/src/vendor/squoosh/copy-wasm.ts b/packages/integrations/image/src/vendor/squoosh/copy-wasm.ts new file mode 100644 index 000000000..1c742f3ea --- /dev/null +++ b/packages/integrations/image/src/vendor/squoosh/copy-wasm.ts @@ -0,0 +1,24 @@ +import fs from 'node:fs/promises'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; + +export async function copyWasmFiles(dir: URL) { + const src = new URL('./', import.meta.url); + await copyDir(fileURLToPath(src), fileURLToPath(dir)); +} + +async function copyDir(src: string, dest: string) { + const itemNames = await fs.readdir(src); + await Promise.all(itemNames.map(async (srcName) => { + const srcPath = path.join(src, srcName); + const destPath = path.join(dest, srcName); + const s = await fs.stat(srcPath); + if (s.isFile() && /.wasm$/.test(srcPath)) { + await fs.mkdir(path.dirname(destPath), { recursive: true }); + await fs.copyFile(srcPath, destPath); + } + else if (s.isDirectory()) { + await copyDir(srcPath, destPath); + } + })); +} diff --git a/packages/integrations/image/src/vendor/squoosh/main.ts b/packages/integrations/image/src/vendor/squoosh/main.ts index a70a042df..cf0fbe097 100644 --- a/packages/integrations/image/src/vendor/squoosh/main.ts +++ b/packages/integrations/image/src/vendor/squoosh/main.ts @@ -1,8 +1,5 @@ import * as worker from './impl.js'; import type { OutputFormat } from '../../loaders/index.js'; -import path from 'node:path'; -import fs from 'node:fs/promises'; -import { fileURLToPath } from 'node:url'; type RotateOperation = { type: 'rotate' @@ -44,24 +41,3 @@ export async function processBuffer( throw Error(`Unsupported encoding format`) } } - -export async function copyLibFiles(dir: URL) { - const src = new URL('./', import.meta.url); - await copyLibDir(fileURLToPath(src), fileURLToPath(dir)); -} - -async function copyLibDir(src: string, dest: string) { - const itemNames = await fs.readdir(src); - await Promise.all(itemNames.map(async (srcName) => { - const srcPath = path.join(src, srcName); - const destPath = path.join(dest, srcName); - const s = await fs.stat(srcPath); - if (s.isFile() && /.wasm$/.test(srcPath)) { - await fs.mkdir(path.dirname(destPath), { recursive: true }); - await fs.copyFile(srcPath, destPath); - } - else if (s.isDirectory()) { - await copyLibDir(srcPath, destPath); - } -})); -}