refactor: move the copy .wasm build step out of the SSR bundle

This commit is contained in:
Tony Sullivan 2022-09-13 11:16:21 -05:00
parent 175b8e51dc
commit 256b0dab04
3 changed files with 26 additions and 26 deletions

View file

@ -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') {

View file

@ -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);
}
}));
}

View file

@ -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);
}
}));
}