full site update
This commit is contained in:
119
node_modules/astro/dist/assets/vite-plugin-assets.js
generated
vendored
119
node_modules/astro/dist/assets/vite-plugin-assets.js
generated
vendored
@@ -1,7 +1,5 @@
|
||||
import { extname } from "node:path";
|
||||
import MagicString from "magic-string";
|
||||
import { normalizePath } from "vite";
|
||||
import { extendManualChunks } from "../core/build/plugins/util.js";
|
||||
import { AstroError, AstroErrorData } from "../core/errors/index.js";
|
||||
import {
|
||||
appendForwardSlash,
|
||||
@@ -10,12 +8,14 @@ import {
|
||||
removeBase,
|
||||
removeQueryString
|
||||
} from "../core/path.js";
|
||||
import { isServerLikeOutput } from "../core/util.js";
|
||||
import { normalizePath } from "../core/viteUtils.js";
|
||||
import { VALID_INPUT_FORMATS, VIRTUAL_MODULE_ID, VIRTUAL_SERVICE_ID } from "./consts.js";
|
||||
import { fontsPlugin } from "./fonts/vite-plugin-fonts.js";
|
||||
import { getAssetsPrefix } from "./utils/getAssetsPrefix.js";
|
||||
import { isESMImportedImage } from "./utils/imageKind.js";
|
||||
import { emitESMImage } from "./utils/node/emitAsset.js";
|
||||
import { getProxyCode } from "./utils/proxy.js";
|
||||
import { makeSvgComponent } from "./utils/svg.js";
|
||||
import { hashTransform, propsToFilename } from "./utils/transformToPath.js";
|
||||
const resolvedVirtualModuleId = "\0" + VIRTUAL_MODULE_ID;
|
||||
const assetRegex = new RegExp(`\\.(${VALID_INPUT_FORMATS.join("|")})`, "i");
|
||||
@@ -35,7 +35,7 @@ const addStaticImageFactory = (settings) => {
|
||||
const hash = hashTransform(options, settings.config.image.service.entrypoint, hashProperties);
|
||||
let finalFilePath;
|
||||
let transformsForPath = globalThis.astroAsset.staticImages.get(finalOriginalPath);
|
||||
let transformForHash = transformsForPath?.transforms.get(hash);
|
||||
const transformForHash = transformsForPath?.transforms.get(hash);
|
||||
if (transformsForPath && transformForHash) {
|
||||
finalFilePath = transformForHash.finalPath;
|
||||
} else {
|
||||
@@ -64,31 +64,27 @@ const addStaticImageFactory = (settings) => {
|
||||
}
|
||||
};
|
||||
};
|
||||
function assets({
|
||||
settings,
|
||||
mode
|
||||
}) {
|
||||
function assets({ fs, settings, sync, logger }) {
|
||||
let resolvedConfig;
|
||||
let shouldEmitFile = false;
|
||||
let isBuild = false;
|
||||
globalThis.astroAsset = {
|
||||
referencedImages: /* @__PURE__ */ new Set()
|
||||
};
|
||||
const imageComponentPrefix = settings.config.image.responsiveStyles ? "Responsive" : "";
|
||||
return [
|
||||
// Expose the components and different utilities from `astro:assets` and handle serving images from `/_image` in dev
|
||||
// Expose the components and different utilities from `astro:assets`
|
||||
{
|
||||
name: "astro:assets",
|
||||
outputOptions(outputOptions) {
|
||||
extendManualChunks(outputOptions, {
|
||||
after(id) {
|
||||
if (id.includes("astro/dist/assets/services/")) {
|
||||
return `astro/assets-service`;
|
||||
}
|
||||
}
|
||||
});
|
||||
config(_, env) {
|
||||
isBuild = env.command === "build";
|
||||
},
|
||||
async resolveId(id) {
|
||||
async resolveId(id, _importer, options) {
|
||||
if (id === VIRTUAL_SERVICE_ID) {
|
||||
return await this.resolve(settings.config.image.service.entrypoint);
|
||||
if (options?.ssr) {
|
||||
return await this.resolve(settings.config.image.service.entrypoint);
|
||||
}
|
||||
return await this.resolve("astro/assets/services/noop");
|
||||
}
|
||||
if (id === VIRTUAL_MODULE_ID) {
|
||||
return resolvedVirtualModuleId;
|
||||
@@ -96,41 +92,42 @@ function assets({
|
||||
},
|
||||
load(id) {
|
||||
if (id === resolvedVirtualModuleId) {
|
||||
return `
|
||||
export { getConfiguredImageService, isLocalService } from "astro/assets";
|
||||
import { getImage as getImageInternal } from "astro/assets";
|
||||
export { default as Image } from "astro/components/Image.astro";
|
||||
export { default as Picture } from "astro/components/Picture.astro";
|
||||
export { inferRemoteSize } from "astro/assets/utils/inferRemoteSize.js";
|
||||
return {
|
||||
code: `
|
||||
export { getConfiguredImageService, isLocalService } from "astro/assets";
|
||||
import { getImage as getImageInternal } from "astro/assets";
|
||||
export { default as Image } from "astro/components/${imageComponentPrefix}Image.astro";
|
||||
export { default as Picture } from "astro/components/${imageComponentPrefix}Picture.astro";
|
||||
export { default as Font } from "astro/components/Font.astro";
|
||||
export { inferRemoteSize } from "astro/assets/utils/inferRemoteSize.js";
|
||||
|
||||
export const imageConfig = ${JSON.stringify(settings.config.image)};
|
||||
// This is used by the @astrojs/node integration to locate images.
|
||||
// It's unused on other platforms, but on some platforms like Netlify (and presumably also Vercel)
|
||||
// new URL("dist/...") is interpreted by the bundler as a signal to include that directory
|
||||
// in the Lambda bundle, which would bloat the bundle with images.
|
||||
// To prevent this, we mark the URL construction as pure,
|
||||
// so that it's tree-shaken away for all platforms that don't need it.
|
||||
export const outDir = /* #__PURE__ */ new URL(${JSON.stringify(
|
||||
new URL(
|
||||
isServerLikeOutput(settings.config) ? settings.config.build.client : settings.config.outDir
|
||||
)
|
||||
)});
|
||||
export const assetsDir = /* #__PURE__ */ new URL(${JSON.stringify(
|
||||
settings.config.build.assets
|
||||
)}, outDir);
|
||||
export const getImage = async (options) => await getImageInternal(options, imageConfig);
|
||||
`;
|
||||
export const imageConfig = ${JSON.stringify(settings.config.image)};
|
||||
// This is used by the @astrojs/node integration to locate images.
|
||||
// It's unused on other platforms, but on some platforms like Netlify (and presumably also Vercel)
|
||||
// new URL("dist/...") is interpreted by the bundler as a signal to include that directory
|
||||
// in the Lambda bundle, which would bloat the bundle with images.
|
||||
// To prevent this, we mark the URL construction as pure,
|
||||
// so that it's tree-shaken away for all platforms that don't need it.
|
||||
export const outDir = /* #__PURE__ */ new URL(${JSON.stringify(
|
||||
new URL(
|
||||
settings.buildOutput === "server" ? settings.config.build.client : settings.config.outDir
|
||||
)
|
||||
)});
|
||||
export const assetsDir = /* #__PURE__ */ new URL(${JSON.stringify(
|
||||
settings.config.build.assets
|
||||
)}, outDir);
|
||||
export const getImage = async (options) => await getImageInternal(options, imageConfig);
|
||||
`
|
||||
};
|
||||
}
|
||||
},
|
||||
buildStart() {
|
||||
if (mode != "build") {
|
||||
return;
|
||||
}
|
||||
if (!isBuild) return;
|
||||
globalThis.astroAsset.addStaticImage = addStaticImageFactory(settings);
|
||||
},
|
||||
// In build, rewrite paths to ESM imported images in code to their final location
|
||||
async renderChunk(code) {
|
||||
const assetUrlRE = /__ASTRO_ASSET_IMAGE__([\w$]{8})__(?:_(.*?)__)?/g;
|
||||
const assetUrlRE = /__ASTRO_ASSET_IMAGE__([\w$]+)__(?:_(.*?)__)?/g;
|
||||
let match;
|
||||
let s;
|
||||
while (match = assetUrlRE.exec(code)) {
|
||||
@@ -174,26 +171,40 @@ function assets({
|
||||
if (!assetRegexEnds.test(id)) {
|
||||
return;
|
||||
}
|
||||
const emitFile = shouldEmitFile ? this.emitFile : void 0;
|
||||
const imageMetadata = await emitESMImage(id, this.meta.watchMode, emitFile);
|
||||
const emitFile = shouldEmitFile ? this.emitFile.bind(this) : void 0;
|
||||
const imageMetadata = await emitESMImage(
|
||||
id,
|
||||
this.meta.watchMode,
|
||||
id.endsWith(".svg"),
|
||||
emitFile
|
||||
);
|
||||
if (!imageMetadata) {
|
||||
throw new AstroError({
|
||||
...AstroErrorData.ImageNotFound,
|
||||
message: AstroErrorData.ImageNotFound.message(id)
|
||||
});
|
||||
}
|
||||
if (id.endsWith(".svg")) {
|
||||
const contents = await fs.promises.readFile(imageMetadata.fsPath, { encoding: "utf8" });
|
||||
return { code: makeSvgComponent(imageMetadata, contents) };
|
||||
}
|
||||
if (options?.ssr) {
|
||||
return `export default ${getProxyCode(
|
||||
imageMetadata,
|
||||
isServerLikeOutput(settings.config)
|
||||
)}`;
|
||||
return {
|
||||
code: `export default ${getProxyCode(
|
||||
imageMetadata,
|
||||
settings.buildOutput === "server"
|
||||
)}`
|
||||
};
|
||||
} else {
|
||||
globalThis.astroAsset.referencedImages.add(imageMetadata.fsPath);
|
||||
return `export default ${JSON.stringify(imageMetadata)}`;
|
||||
return {
|
||||
code: `export default ${JSON.stringify(imageMetadata)}`
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
fontsPlugin({ settings, sync, logger })
|
||||
];
|
||||
}
|
||||
export {
|
||||
|
Reference in New Issue
Block a user