Convert usage
This commit is contained in:
parent
63e379c146
commit
4948d73fad
7 changed files with 35 additions and 21 deletions
|
@ -4,7 +4,7 @@ import { pathToFileURL } from 'node:url';
|
|||
import { AstroSettings } from '../@types/astro.js';
|
||||
import { StaticBuildOptions } from '../core/build/types.js';
|
||||
import { AstroError, AstroErrorData } from '../core/errors/index.js';
|
||||
import { rootRelativePath } from '../core/util.js';
|
||||
import File from '../core/file/index.js';
|
||||
import { ImageService, isLocalService, LocalImageService } from './services/service.js';
|
||||
import type { ImageMetadata, ImageTransform } from './types.js';
|
||||
import { imageMetadata } from './utils/metadata.js';
|
||||
|
@ -127,7 +127,8 @@ export async function emitESMImage(
|
|||
fileEmitter: any,
|
||||
settings: AstroSettings
|
||||
) {
|
||||
const url = pathToFileURL(id);
|
||||
const file = new File(id, settings.config);
|
||||
const url = file.toFileURL();
|
||||
const meta = await imageMetadata(url);
|
||||
|
||||
if (!meta) {
|
||||
|
@ -152,7 +153,7 @@ export async function emitESMImage(
|
|||
url.searchParams.append('origHeight', meta.height.toString());
|
||||
url.searchParams.append('origFormat', meta.format);
|
||||
|
||||
meta.src = rootRelativePath(settings.config, url);
|
||||
meta.src = file.toRootRelativePath();
|
||||
}
|
||||
|
||||
return meta;
|
||||
|
|
|
@ -8,6 +8,7 @@ import { normalizePath } from 'vite';
|
|||
import { AstroPluginOptions, ImageTransform } from '../@types/astro';
|
||||
import { error } from '../core/logger/core.js';
|
||||
import { joinPaths, prependForwardSlash } from '../core/path.js';
|
||||
import File from '../core/file/index.js';
|
||||
import { VIRTUAL_MODULE_ID, VIRTUAL_SERVICE_ID } from './consts.js';
|
||||
import { emitESMImage, isESMImportedImage } from './internal.js';
|
||||
import { isLocalService } from './services/service.js';
|
||||
|
@ -76,15 +77,16 @@ export default function assets({
|
|||
return next();
|
||||
}
|
||||
|
||||
const filePathURL = new URL('.' + filePath, settings.config.root);
|
||||
const file = await fs.readFile(filePathURL);
|
||||
const file = new File(filePath, settings.config);
|
||||
const filePathURL = file.toFileURL();
|
||||
const buffer = await fs.readFile(file.toFileURL());
|
||||
|
||||
// Get the file's metadata from the URL
|
||||
let meta = getOrigQueryParams(filePathURL.searchParams);
|
||||
|
||||
// If we don't have them (ex: the image came from Markdown, let's calculate them again)
|
||||
if (!meta) {
|
||||
meta = await imageMetadata(filePathURL, file);
|
||||
meta = await imageMetadata(filePathURL, buffer);
|
||||
|
||||
if (!meta) {
|
||||
return next();
|
||||
|
@ -98,11 +100,11 @@ export default function assets({
|
|||
}
|
||||
|
||||
// if no transforms were added, the original file will be returned as-is
|
||||
let data = file;
|
||||
let data = buffer;
|
||||
let format = meta.format;
|
||||
|
||||
if (transform) {
|
||||
const result = await globalThis.astroAsset.imageService.transform(file, transform);
|
||||
const result = await globalThis.astroAsset.imageService.transform(buffer, transform);
|
||||
data = result.data;
|
||||
format = result.format;
|
||||
}
|
||||
|
@ -201,6 +203,7 @@ export default function assets({
|
|||
async load(id) {
|
||||
if (/\.(jpeg|jpg|png|tiff|webp|gif|svg)$/.test(id)) {
|
||||
const meta = await emitESMImage(id, this.meta.watchMode, this.emitFile, settings);
|
||||
|
||||
return `export default ${JSON.stringify(meta)}`;
|
||||
}
|
||||
},
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import type { Plugin as VitePlugin } from 'vite';
|
||||
import type { AstroSettings } from '../../../@types/astro';
|
||||
import { viteID } from '../../util.js';
|
||||
import type { BuildInternals } from '../internal.js';
|
||||
import { getPageDataByViteID } from '../internal.js';
|
||||
import { AstroBuildPlugin } from '../plugin';
|
||||
import { StaticBuildOptions } from '../types';
|
||||
import File from '../../file/index.js';
|
||||
|
||||
function virtualHoistedEntry(id: string) {
|
||||
return id.startsWith('/astro/hoisted.js?q=');
|
||||
|
@ -66,8 +66,8 @@ export function vitePluginHoistedScripts(
|
|||
const facadeId = output.facadeModuleId!;
|
||||
const pages = internals.hoistedScriptIdToPagesMap.get(facadeId)!;
|
||||
for (const pathname of pages) {
|
||||
const vid = viteID(new URL('.' + pathname, settings.config.root));
|
||||
const pageInfo = getPageDataByViteID(internals, vid);
|
||||
const file = new File(pathname, settings.config);
|
||||
const pageInfo = getPageDataByViteID(internals, file.toViteID());
|
||||
if (pageInfo) {
|
||||
if (canBeInlined) {
|
||||
pageInfo.hoistedScript = {
|
||||
|
|
|
@ -80,6 +80,13 @@ class File {
|
|||
return '/@fs' + abs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts to an id that is used as the key in Vite's module graph
|
||||
*/
|
||||
toViteID() {
|
||||
return viteID(this.toFileURL());
|
||||
}
|
||||
|
||||
static getPathType(raw: string, root: URL): PathType {
|
||||
if(raw.startsWith('/@fs')) {
|
||||
return 'vite-fs-path';
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import type { ModuleLoader } from '../../module-loader/index';
|
||||
import type { AstroConfig, RuntimeMode } from '../../../@types/astro.js';
|
||||
|
||||
import { RuntimeMode } from '../../../@types/astro.js';
|
||||
import { viteID } from '../../util.js';
|
||||
import File from '../../file/index.js';
|
||||
import { isBuildableCSSRequest } from './util.js';
|
||||
import { crawlGraph } from './vite.js';
|
||||
|
||||
|
@ -9,12 +9,14 @@ import { crawlGraph } from './vite.js';
|
|||
export async function getStylesForURL(
|
||||
filePath: URL,
|
||||
loader: ModuleLoader,
|
||||
mode: RuntimeMode
|
||||
mode: RuntimeMode,
|
||||
config: AstroConfig,
|
||||
): Promise<{ urls: Set<string>; stylesMap: Map<string, string> }> {
|
||||
const importedCssUrls = new Set<string>();
|
||||
const importedStylesMap = new Map<string, string>();
|
||||
|
||||
for await (const importedModule of crawlGraph(loader, viteID(filePath), true)) {
|
||||
const file = new File(filePath, config);
|
||||
for await (const importedModule of crawlGraph(loader, file.toViteID(), true)) {
|
||||
if (isBuildableCSSRequest(importedModule.url)) {
|
||||
let ssrModule: Record<string, any>;
|
||||
try {
|
||||
|
|
|
@ -76,7 +76,7 @@ interface GetScriptsAndStylesParams {
|
|||
|
||||
async function getScriptsAndStyles({ env, filePath }: GetScriptsAndStylesParams) {
|
||||
// Add hoisted script tags
|
||||
const scripts = await getScriptsForURL(filePath, env.loader);
|
||||
const scripts = await getScriptsForURL(filePath, env.loader, env.settings.config);
|
||||
|
||||
// Inject HMR scripts
|
||||
if (isPage(filePath, env.settings) && env.mode === 'development') {
|
||||
|
@ -109,7 +109,7 @@ async function getScriptsAndStyles({ env, filePath }: GetScriptsAndStylesParams)
|
|||
}
|
||||
|
||||
// Pass framework CSS in as style tags to be appended to the page.
|
||||
const { urls: styleUrls, stylesMap } = await getStylesForURL(filePath, env.loader, env.mode);
|
||||
const { urls: styleUrls, stylesMap } = await getStylesForURL(filePath, env.loader, env.mode, env.settings.config);
|
||||
let links = new Set<SSRElement>();
|
||||
[...styleUrls].forEach((href) => {
|
||||
links.add({
|
||||
|
|
|
@ -1,17 +1,18 @@
|
|||
import type { SSRElement } from '../../../@types/astro';
|
||||
import type { AstroConfig, SSRElement } from '../../../@types/astro';
|
||||
import type { PluginMetadata as AstroPluginMetadata } from '../../../vite-plugin-astro/types';
|
||||
import type { ModuleInfo, ModuleLoader } from '../../module-loader/index';
|
||||
|
||||
import { viteID } from '../../util.js';
|
||||
import File from '../../file/index.js';
|
||||
import { createModuleScriptElementWithSrc } from '../ssr-element.js';
|
||||
import { crawlGraph } from './vite.js';
|
||||
|
||||
export async function getScriptsForURL(
|
||||
filePath: URL,
|
||||
loader: ModuleLoader
|
||||
loader: ModuleLoader,
|
||||
config: AstroConfig,
|
||||
): Promise<Set<SSRElement>> {
|
||||
const elements = new Set<SSRElement>();
|
||||
const rootID = viteID(filePath);
|
||||
const rootID = new File(filePath, config).toViteID();
|
||||
const modInfo = loader.getModuleInfo(rootID);
|
||||
addHoistedScripts(elements, modInfo);
|
||||
for await (const moduleNode of crawlGraph(loader, rootID, true)) {
|
||||
|
|
Loading…
Reference in a new issue