From 5208c88aeb512250f2a443edede574710dbccffa Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Wed, 19 Jan 2022 08:25:42 -0500 Subject: [PATCH] Add Astro.resolve deprecation warning for the static build (#2416) * Add Astro.resolve deprecation warning for the static build * Adds a changeset --- .changeset/proud-buses-destroy.md | 5 ++++ packages/astro/src/core/build/static-build.ts | 2 +- packages/astro/src/core/ssr/css.ts | 7 ++++++ packages/astro/src/core/ssr/index.ts | 2 +- packages/astro/src/core/ssr/result.ts | 24 +++++++++++++++++++ .../astro/src/vite-plugin-build-css/index.ts | 9 +------ 6 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 .changeset/proud-buses-destroy.md diff --git a/.changeset/proud-buses-destroy.md b/.changeset/proud-buses-destroy.md new file mode 100644 index 000000000..2d354a498 --- /dev/null +++ b/.changeset/proud-buses-destroy.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Adds Astro.resolve deprecation for the static build diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 8e72bc4b2..1ccc00506 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/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/packages/astro/src/core/ssr/css.ts b/packages/astro/src/core/ssr/css.ts index 6c731c1ad..4ee0e80d8 100644 --- a/packages/astro/src/core/ssr/css.ts +++ b/packages/astro/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/packages/astro/src/core/ssr/index.ts b/packages/astro/src/core/ssr/index.ts index d52a27eea..daa627220 100644 --- a/packages/astro/src/core/ssr/index.ts +++ b/packages/astro/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/packages/astro/src/core/ssr/result.ts b/packages/astro/src/core/ssr/result.ts index 32694dd05..95ff36239 100644 --- a/packages/astro/src/core/ssr/result.ts +++ b/packages/astro/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/packages/astro/src/vite-plugin-build-css/index.ts b/packages/astro/src/vite-plugin-build-css/index.ts index 0a1757842..155fdc8ed 100644 --- a/packages/astro/src/vite-plugin-build-css/index.ts +++ b/packages/astro/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('/')) {