diff --git a/src/core/build/static-build.ts b/src/core/build/static-build.ts index 8e72bc4b2650..1ccc0050650d 100644 --- a/src/core/build/static-build.ts +++ b/src/core/build/static-build.ts @@ -316,7 +316,7 @@ async function generatePath(pathname: string, opts: StaticBuildOptions, gopts: G debug(logging, 'generate', `Generating: ${pathname}`); const rootpath = new URL(astroConfig.buildOptions.site || 'http://localhost/').pathname; - const result = createResult({ astroConfig, origin, params, pathname, renderers }); + const result = createResult({ astroConfig, logging, origin, params, pathname, renderers }); result.links = new Set( linkIds.map((href) => ({ props: { diff --git a/src/core/ssr/css.ts b/src/core/ssr/css.ts index 6c731c1ad606..4ee0e80d8635 100644 --- a/src/core/ssr/css.ts +++ b/src/core/ssr/css.ts @@ -6,6 +6,13 @@ import { viteID } from '../util.js'; // https://vitejs.dev/guide/features.html#css-pre-processors export const STYLE_EXTENSIONS = new Set(['.css', '.pcss', '.postcss', '.scss', '.sass', '.styl', '.stylus', '.less']); +const cssRe = new RegExp( + `\\.(${Array.from(STYLE_EXTENSIONS) + .map((s) => s.slice(1)) + .join('|')})($|\\?)` +); +export const isCSSRequest = (request: string): boolean => cssRe.test(request); + /** * getStylesForURL * Given a filePath URL, crawl Vite’s module graph to find style files diff --git a/src/core/ssr/index.ts b/src/core/ssr/index.ts index d52a27eeaca5..daa627220373 100644 --- a/src/core/ssr/index.ts +++ b/src/core/ssr/index.ts @@ -219,7 +219,7 @@ export async function render(renderers: Renderer[], mod: ComponentInstance, ssrO if (!Component) throw new Error(`Expected an exported Astro component but received typeof ${typeof Component}`); if (!Component.isAstroComponentFactory) throw new Error(`Unable to SSR non-Astro component (${route?.component})`); - const result = createResult({ astroConfig, origin, params, pathname, renderers }); + const result = createResult({ astroConfig, logging, origin, params, pathname, renderers }); // Resolves specifiers in the inline hydrated scripts, such as "@astrojs/renderer-preact/client.js" result.resolve = async (s: string) => { // The legacy build needs these to remain unresolved so that vite HTML diff --git a/src/core/ssr/result.ts b/src/core/ssr/result.ts index 32694dd05330..95ff36239937 100644 --- a/src/core/ssr/result.ts +++ b/src/core/ssr/result.ts @@ -1,10 +1,14 @@ import type { AstroConfig, AstroGlobal, AstroGlobalPartial, Params, Renderer, SSRElement, SSRResult } from '../../@types/astro'; +import { bold } from 'kleur/colors'; import { canonicalURL as getCanonicalURL } from '../util.js'; +import { isCSSRequest } from './css.js'; import { renderSlot } from '../../runtime/server/index.js'; +import { warn, LogOptions } from '../logger.js'; export interface CreateResultArgs { astroConfig: AstroConfig; + logging: LogOptions; origin: string; params: Params; pathname: string; @@ -34,6 +38,26 @@ export function createResult(args: CreateResultArgs): SSRResult { params, url, }, + resolve(path: string) { + if(astroConfig.buildOptions.experimentalStaticBuild) { + let extra = `This can be replaced with a dynamic import like so: await import("${path}")`; + if(isCSSRequest(path)) { + extra = `It looks like you are resolving styles. If you are adding a link tag, replace with this: + + +` + } + + warn(args.logging, `deprecation`, `${bold('Astro.resolve()')} is deprecated. We see that you are trying to resolve ${path}. +${extra}`); + // Intentionally return an empty string so that it is not relied upon. + return ''; + } + + return astroGlobal.resolve(path); + }, slots: Object.fromEntries(Object.entries(slots || {}).map(([slotName]) => [slotName, true])), // This is used for but shouldn't be used publicly privateRenderSlotDoNotUse(slotName: string) { diff --git a/src/vite-plugin-build-css/index.ts b/src/vite-plugin-build-css/index.ts index 0a175784229a..155fdc8ede06 100644 --- a/src/vite-plugin-build-css/index.ts +++ b/src/vite-plugin-build-css/index.ts @@ -4,7 +4,7 @@ import type { BuildInternals } from '../core/build/internal'; import * as path from 'path'; import esbuild from 'esbuild'; import { Plugin as VitePlugin } from '../core/vite'; -import { STYLE_EXTENSIONS } from '../core/ssr/css.js'; +import { isCSSRequest } from '../core/ssr/css.js'; const PLUGIN_NAME = '@astrojs/rollup-plugin-build-css'; @@ -13,13 +13,6 @@ const ASTRO_STYLE_PREFIX = '@astro-inline-style'; const ASTRO_PAGE_STYLE_PREFIX = '@astro-page-all-styles'; -const cssRe = new RegExp( - `\\.(${Array.from(STYLE_EXTENSIONS) - .map((s) => s.slice(1)) - .join('|')})($|\\?)` -); -const isCSSRequest = (request: string): boolean => cssRe.test(request); - export function getAstroPageStyleId(pathname: string) { let styleId = ASTRO_PAGE_STYLE_PREFIX + pathname; if (styleId.endsWith('/')) {