Skip to content

Commit

Permalink
Add Astro.resolve deprecation warning for the static build (withastro…
Browse files Browse the repository at this point in the history
…#2416)

* Add Astro.resolve deprecation warning for the static build

* Adds a changeset
  • Loading branch information
matthewp committed Jan 19, 2022
1 parent 9920c8d commit a70461d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:https://localhost/').pathname;
const result = createResult({ astroConfig, origin, params, pathname, renderers });
const result = createResult({ astroConfig, logging, origin, params, pathname, renderers });
result.links = new Set<SSRElement>(
linkIds.map((href) => ({
props: {
Expand Down
7 changes: 7 additions & 0 deletions src/core/ssr/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/core/ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/core/ssr/result.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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:
<style global>
@import "${path}";
</style>
`
}

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 <Markdown> but shouldn't be used publicly
privateRenderSlotDoNotUse(slotName: string) {
Expand Down
9 changes: 1 addition & 8 deletions src/vite-plugin-build-css/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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('/')) {
Expand Down

0 comments on commit a70461d

Please sign in to comment.