This repository has been archived on 2025-12-24. You can view files and clone it, but cannot push or open issues or pull requests.
site-template/packages/imagetools/plugin/index.js
2025-03-07 14:59:06 +01:00

89 lines
2.1 KiB
JavaScript

// @ts-check
import fs from "node:fs";
import stream from "node:stream";
import { fileURLToPath } from "node:url";
import { posix as path, resolve } from "node:path";
import load from "./hooks/load.js";
import config from "./hooks/config.js";
import transform from "./hooks/transform.js";
import { middleware } from "../ssr/index.js";
import { GlobalConfigOptions } from "../utils/runtimeChecks.js";
if (!globalThis.astroImageToolsStore)
globalThis.astroImageToolsStore = new Map();
export const store = globalThis.astroImageToolsStore;
const filename = fileURLToPath(import.meta.url);
const astroViteConfigsPath = resolve(filename, "../../astroViteConfigs.js");
const vitePluginAstroImageTools = {
name: "vite-plugin-astro-imagetools",
enforce: "pre",
config,
async configResolved(config) {
const { mode } = config;
const { outDir, sourcemap } = config.build;
let inheritedPattern =
config.build.rollupOptions.output?.assetFileNames?.replace(
"[name]",
"[name]@[width]"
);
let assetFileNames = path.normalize(
GlobalConfigOptions.assetFileNames ||
inheritedPattern ||
`/_astro/[name]@[width].[hash][extname]`
);
const { dir: assetsDir } = path.posix.parse(
assetFileNames.replaceAll(path.sep, path.posix.sep)
);
if (!assetFileNames.startsWith("/"))
assetFileNames = path.join("/", assetFileNames);
const astroViteConfigs = JSON.parse(
(await fs.promises.readFile(astroViteConfigsPath, "utf8")).slice(15)
);
const newAstroViteConfigs = {
...astroViteConfigs,
mode,
outDir,
assetsDir,
sourcemap,
assetFileNames,
};
await fs.promises.writeFile(
astroViteConfigsPath,
`export default ${JSON.stringify(newAstroViteConfigs, null, 2)}`
);
},
load,
transform,
configureServer(server) {
server.middlewares.use(async (request, response, next) => {
const buffer = await middleware(request, response);
if (buffer) {
return stream.Readable.from(buffer).pipe(response);
}
next();
});
},
};
export default vitePluginAstroImageTools;