full site update
This commit is contained in:
124
node_modules/astro/dist/assets/build/generate.js
generated
vendored
124
node_modules/astro/dist/assets/build/generate.js
generated
vendored
@@ -6,12 +6,11 @@ import { getTimeStat } from "../../core/build/util.js";
|
||||
import { AstroError } from "../../core/errors/errors.js";
|
||||
import { AstroErrorData } from "../../core/errors/index.js";
|
||||
import { isRemotePath, removeLeadingForwardSlash } from "../../core/path.js";
|
||||
import { isServerLikeOutput } from "../../core/util.js";
|
||||
import { getConfiguredImageService } from "../internal.js";
|
||||
import { isESMImportedImage } from "../utils/imageKind.js";
|
||||
import { loadRemoteImage } from "./remote.js";
|
||||
import { loadRemoteImage, revalidateRemoteImage } from "./remote.js";
|
||||
async function prepareAssetsGenerationEnv(pipeline, totalCount) {
|
||||
const { config, logger } = pipeline;
|
||||
const { config, logger, settings } = pipeline;
|
||||
let useCache = true;
|
||||
const assetsCacheDir = new URL("assets/", config.cacheDir);
|
||||
const count = { total: totalCount, current: 1 };
|
||||
@@ -24,8 +23,9 @@ async function prepareAssetsGenerationEnv(pipeline, totalCount) {
|
||||
);
|
||||
useCache = false;
|
||||
}
|
||||
const isServerOutput = settings.buildOutput === "server";
|
||||
let serverRoot, clientRoot;
|
||||
if (isServerLikeOutput(config)) {
|
||||
if (isServerOutput) {
|
||||
serverRoot = config.build.server;
|
||||
clientRoot = config.build.client;
|
||||
} else {
|
||||
@@ -34,7 +34,7 @@ async function prepareAssetsGenerationEnv(pipeline, totalCount) {
|
||||
}
|
||||
return {
|
||||
logger,
|
||||
isSSR: isServerLikeOutput(config),
|
||||
isSSR: isServerOutput,
|
||||
count,
|
||||
useCache,
|
||||
assetsCacheDir,
|
||||
@@ -47,12 +47,10 @@ async function prepareAssetsGenerationEnv(pipeline, totalCount) {
|
||||
function getFullImagePath(originalFilePath, env) {
|
||||
return new URL(removeLeadingForwardSlash(originalFilePath), env.serverRoot);
|
||||
}
|
||||
async function generateImagesForPath(originalFilePath, transformsAndPath, env, queue) {
|
||||
async function generateImagesForPath(originalFilePath, transformsAndPath, env) {
|
||||
let originalImage;
|
||||
for (const [_, transform] of transformsAndPath.transforms) {
|
||||
await queue.add(async () => generateImage(transform.finalPath, transform.transform)).catch((e) => {
|
||||
throw e;
|
||||
});
|
||||
await generateImage(transform.finalPath, transform.transform);
|
||||
}
|
||||
if (!env.isSSR && transformsAndPath.originalSrcPath && !globalThis.astroAsset.referencedImages?.has(transformsAndPath.originalSrcPath)) {
|
||||
try {
|
||||
@@ -72,7 +70,7 @@ async function generateImagesForPath(originalFilePath, transformsAndPath, env, q
|
||||
const timeEnd = performance.now();
|
||||
const timeChange = getTimeStat(timeStart, timeEnd);
|
||||
const timeIncrease = `(+${timeChange})`;
|
||||
const statsText = generationData.cached ? `(reused cache entry)` : `(before: ${generationData.weight.before}kB, after: ${generationData.weight.after}kB)`;
|
||||
const statsText = generationData.cached !== "miss" ? generationData.cached === "hit" ? `(reused cache entry)` : `(revalidated cache entry)` : `(before: ${generationData.weight.before}kB, after: ${generationData.weight.after}kB)`;
|
||||
const count = `(${env.count.current}/${env.count.total})`;
|
||||
env.logger.info(
|
||||
null,
|
||||
@@ -85,30 +83,69 @@ async function generateImagesForPath(originalFilePath, transformsAndPath, env, q
|
||||
const finalFileURL = new URL("." + filepath, env.clientRoot);
|
||||
const finalFolderURL = new URL("./", finalFileURL);
|
||||
await fs.promises.mkdir(finalFolderURL, { recursive: true });
|
||||
const cacheFile = basename(filepath) + (isLocalImage ? "" : ".json");
|
||||
const cacheFile = basename(filepath);
|
||||
const cachedFileURL = new URL(cacheFile, env.assetsCacheDir);
|
||||
const cacheMetaFile = cacheFile + ".json";
|
||||
const cachedMetaFileURL = new URL(cacheMetaFile, env.assetsCacheDir);
|
||||
try {
|
||||
if (isLocalImage) {
|
||||
await fs.promises.copyFile(cachedFileURL, finalFileURL, fs.constants.COPYFILE_FICLONE);
|
||||
return {
|
||||
cached: true
|
||||
cached: "hit"
|
||||
};
|
||||
} else {
|
||||
const JSONData = JSON.parse(readFileSync(cachedFileURL, "utf-8"));
|
||||
if (!JSONData.data || !JSONData.expires) {
|
||||
await fs.promises.unlink(cachedFileURL);
|
||||
const JSONData = JSON.parse(readFileSync(cachedMetaFileURL, "utf-8"));
|
||||
if (!JSONData.expires) {
|
||||
try {
|
||||
await fs.promises.unlink(cachedFileURL);
|
||||
} catch {
|
||||
}
|
||||
await fs.promises.unlink(cachedMetaFileURL);
|
||||
throw new Error(
|
||||
`Malformed cache entry for ${filepath}, cache will be regenerated for this file.`
|
||||
);
|
||||
}
|
||||
if (JSONData.expires > Date.now()) {
|
||||
await fs.promises.writeFile(finalFileURL, Buffer.from(JSONData.data, "base64"));
|
||||
return {
|
||||
cached: true
|
||||
};
|
||||
} else {
|
||||
await fs.promises.unlink(cachedFileURL);
|
||||
if (JSONData.data) {
|
||||
const { data, ...meta } = JSONData;
|
||||
await Promise.all([
|
||||
fs.promises.writeFile(cachedFileURL, Buffer.from(data, "base64")),
|
||||
writeCacheMetaFile(cachedMetaFileURL, meta, env)
|
||||
]);
|
||||
}
|
||||
if (JSONData.expires > Date.now()) {
|
||||
await fs.promises.copyFile(cachedFileURL, finalFileURL, fs.constants.COPYFILE_FICLONE);
|
||||
return {
|
||||
cached: "hit"
|
||||
};
|
||||
}
|
||||
if (JSONData.etag || JSONData.lastModified) {
|
||||
try {
|
||||
const revalidatedData = await revalidateRemoteImage(options.src, {
|
||||
etag: JSONData.etag,
|
||||
lastModified: JSONData.lastModified
|
||||
});
|
||||
if (revalidatedData.data.length) {
|
||||
originalImage = revalidatedData;
|
||||
} else {
|
||||
await writeCacheMetaFile(cachedMetaFileURL, revalidatedData, env);
|
||||
await fs.promises.copyFile(
|
||||
cachedFileURL,
|
||||
finalFileURL,
|
||||
fs.constants.COPYFILE_FICLONE
|
||||
);
|
||||
return { cached: "revalidated" };
|
||||
}
|
||||
} catch (e) {
|
||||
env.logger.warn(
|
||||
null,
|
||||
`An error was encountered while revalidating a cached remote asset. Proceeding with stale cache. ${e}`
|
||||
);
|
||||
await fs.promises.copyFile(cachedFileURL, finalFileURL, fs.constants.COPYFILE_FICLONE);
|
||||
return { cached: "hit" };
|
||||
}
|
||||
}
|
||||
await fs.promises.unlink(cachedFileURL);
|
||||
await fs.promises.unlink(cachedMetaFileURL);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e.code !== "ENOENT") {
|
||||
@@ -121,7 +158,9 @@ async function generateImagesForPath(originalFilePath, transformsAndPath, env, q
|
||||
}
|
||||
let resultData = {
|
||||
data: void 0,
|
||||
expires: originalImage.expires
|
||||
expires: originalImage.expires,
|
||||
etag: originalImage.etag,
|
||||
lastModified: originalImage.lastModified
|
||||
};
|
||||
const imageService = await getConfiguredImageService();
|
||||
try {
|
||||
@@ -131,6 +170,9 @@ async function generateImagesForPath(originalFilePath, transformsAndPath, env, q
|
||||
env.imageConfig
|
||||
)).data;
|
||||
} catch (e) {
|
||||
if (AstroError.is(e)) {
|
||||
throw e;
|
||||
}
|
||||
const error = new AstroError(
|
||||
{
|
||||
...AstroErrorData.CouldNotTransformImage,
|
||||
@@ -145,13 +187,10 @@ async function generateImagesForPath(originalFilePath, transformsAndPath, env, q
|
||||
if (isLocalImage) {
|
||||
await fs.promises.writeFile(cachedFileURL, resultData.data);
|
||||
} else {
|
||||
await fs.promises.writeFile(
|
||||
cachedFileURL,
|
||||
JSON.stringify({
|
||||
data: Buffer.from(resultData.data).toString("base64"),
|
||||
expires: resultData.expires
|
||||
})
|
||||
);
|
||||
await Promise.all([
|
||||
fs.promises.writeFile(cachedFileURL, resultData.data),
|
||||
writeCacheMetaFile(cachedMetaFileURL, resultData, env)
|
||||
]);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -163,7 +202,7 @@ async function generateImagesForPath(originalFilePath, transformsAndPath, env, q
|
||||
await fs.promises.writeFile(finalFileURL, resultData.data);
|
||||
}
|
||||
return {
|
||||
cached: false,
|
||||
cached: "miss",
|
||||
weight: {
|
||||
// Divide by 1024 to get size in kilobytes
|
||||
before: Math.trunc(originalImage.data.byteLength / 1024),
|
||||
@@ -172,6 +211,23 @@ async function generateImagesForPath(originalFilePath, transformsAndPath, env, q
|
||||
};
|
||||
}
|
||||
}
|
||||
async function writeCacheMetaFile(cachedMetaFileURL, resultData, env) {
|
||||
try {
|
||||
return await fs.promises.writeFile(
|
||||
cachedMetaFileURL,
|
||||
JSON.stringify({
|
||||
expires: resultData.expires,
|
||||
etag: resultData.etag,
|
||||
lastModified: resultData.lastModified
|
||||
})
|
||||
);
|
||||
} catch (e) {
|
||||
env.logger.warn(
|
||||
null,
|
||||
`An error was encountered while writing the cache file for a remote asset. Proceeding without caching this asset. Error: ${e}`
|
||||
);
|
||||
}
|
||||
}
|
||||
function getStaticImageList() {
|
||||
if (!globalThis?.astroAsset?.staticImages) {
|
||||
return /* @__PURE__ */ new Map();
|
||||
@@ -180,11 +236,7 @@ function getStaticImageList() {
|
||||
}
|
||||
async function loadImage(path, env) {
|
||||
if (isRemotePath(path)) {
|
||||
const remoteImage = await loadRemoteImage(path);
|
||||
return {
|
||||
data: remoteImage.data,
|
||||
expires: remoteImage.expires
|
||||
};
|
||||
return await loadRemoteImage(path);
|
||||
}
|
||||
return {
|
||||
data: await fs.promises.readFile(getFullImagePath(path, env)),
|
||||
|
Reference in New Issue
Block a user