From 5eba34fcc663def20bdf6e0daad02a6a5472776b Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Wed, 4 Jan 2023 03:06:07 +0800 Subject: [PATCH] Remove deprecated APIs (#5707) * Remove deprecated Astro globals * Remove deprecated hook param * Fix test * Add changeset * Add TODO --- .changeset/selfish-tigers-do.md | 10 ++++ .changeset/tricky-rabbits-count.md | 36 +++++++++++++ .changeset/two-geese-eat.md | 24 +++++++++ examples/integration/index.ts | 2 +- packages/astro/src/@types/astro.ts | 20 +------- packages/astro/src/core/build/index.ts | 2 +- packages/astro/src/core/render/result.ts | 51 +------------------ packages/astro/src/integrations/index.ts | 39 +------------- .../astro/src/runtime/server/astro-global.ts | 31 ++--------- .../test/ssr-adapter-build-config.test.js | 15 +++--- packages/integrations/cloudflare/src/index.ts | 10 ---- packages/integrations/deno/src/index.ts | 8 --- packages/integrations/image/src/index.ts | 9 +--- .../netlify/src/integration-edge-functions.ts | 11 ---- .../netlify/src/integration-functions.ts | 9 ---- packages/integrations/node/src/index.ts | 9 ---- .../integrations/vercel/src/edge/adapter.ts | 9 ---- .../vercel/src/serverless/adapter.ts | 9 ---- 18 files changed, 87 insertions(+), 217 deletions(-) create mode 100644 .changeset/selfish-tigers-do.md create mode 100644 .changeset/tricky-rabbits-count.md create mode 100644 .changeset/two-geese-eat.md diff --git a/.changeset/selfish-tigers-do.md b/.changeset/selfish-tigers-do.md new file mode 100644 index 000000000000..4502080213da --- /dev/null +++ b/.changeset/selfish-tigers-do.md @@ -0,0 +1,10 @@ +--- +'@astrojs/cloudflare': major +'@astrojs/deno': major +'@astrojs/image': minor +'@astrojs/netlify': major +'@astrojs/node': major +'@astrojs/vercel': major +--- + +Remove `astro:build:start` backwards compatibility code diff --git a/.changeset/tricky-rabbits-count.md b/.changeset/tricky-rabbits-count.md new file mode 100644 index 000000000000..a9d015b08ea6 --- /dev/null +++ b/.changeset/tricky-rabbits-count.md @@ -0,0 +1,36 @@ +--- +'astro': major +--- + +Remove deprecated `Astro` global APIs, including `Astro.resolve`, `Astro.fetchContent`, and `Astro.canonicalURL`. + +#### `Astro.resolve` + +You can resolve asset paths using `import` instead. For example: + +```astro +--- +import 'style.css' +import imageUrl from './image.png' +--- + + +``` + +See the [v0.25 migration guide](https://docs.astro.build/en/migrate/#deprecated-astroresolve) for more information. + +#### `Astro.fetchContent` + +Use `Astro.glob` instead to fetch markdown files, or migrate to the [Content Collections](https://docs.astro.build/en/guides/content-collections/) feature. + +```js +let allPosts = await Astro.glob('./posts/*.md'); +``` + +#### `Astro.canonicalURL` + +Use `Astro.url` instead to construct the canonical URL. + +```js +const canonicalURL = new URL(Astro.url.pathname, Astro.site); +``` diff --git a/.changeset/two-geese-eat.md b/.changeset/two-geese-eat.md new file mode 100644 index 000000000000..d626d331f3d6 --- /dev/null +++ b/.changeset/two-geese-eat.md @@ -0,0 +1,24 @@ +--- +'astro': major +--- + +Remove `buildConfig` option parameter from integration `astro:build:start` hook in favour of the `build.config` option in the `astro:config:setup` hook. + +```js +export default function myIntegration() { + return { + name: 'my-integration', + hooks: { + 'astro:config:setup': ({ updateConfig }) => { + updateConfig({ + build: { + client: '...', + server: '...', + serverEntry: '...', + }, + }); + }, + }, + }; +} +``` diff --git a/examples/integration/index.ts b/examples/integration/index.ts index 2c7249b837e5..8c6cb35bf12f 100644 --- a/examples/integration/index.ts +++ b/examples/integration/index.ts @@ -10,7 +10,7 @@ export default function createIntegration(): AstroIntegration { // See the @astrojs/react integration for an example // https://github.com/withastro/astro/blob/main/packages/integrations/react/src/index.ts }, - 'astro:build:start': ({ buildConfig }) => { + 'astro:build:setup': ({ config, updateConfig }) => { // See the @astrojs/netlify integration for an example // https://github.com/withastro/astro/blob/main/packages/integrations/netlify/src/integration-functions.ts }, diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index e20d0eeed677..1206c3431bfc 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -109,18 +109,6 @@ export interface BuildConfig { export interface AstroGlobal = Record> extends AstroGlobalPartial, AstroSharedContext { - /** - * Canonical URL of the current page. - * @deprecated Use `Astro.url` instead. - * - * Example: - * ```astro - * --- - * const canonicalURL = new URL(Astro.url.pathname, Astro.site); - * --- - * ``` - */ - canonicalURL: URL; /** * A full URL object of the request URL. * Equivalent to: `new URL(Astro.request.url)` @@ -254,12 +242,6 @@ export interface AstroGlobal = Record; /** * Fetch local files into your static site setup * @@ -1390,7 +1372,7 @@ export interface AstroIntegration { 'astro:server:start'?: (options: { address: AddressInfo }) => void | Promise; 'astro:server:done'?: () => void | Promise; 'astro:build:ssr'?: (options: { manifest: SerializedSSRManifest }) => void | Promise; - 'astro:build:start'?: (options: { buildConfig: BuildConfig }) => void | Promise; + 'astro:build:start'?: () => void | Promise; 'astro:build:setup'?: (options: { vite: vite.InlineConfig; pages: Map; diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index 34f84b40f65c..967723cc5587 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -90,7 +90,7 @@ class AstroBuilder { server: this.settings.config.build.server, serverEntry: this.settings.config.build.serverEntry, }; - await runHookBuildStart({ config: this.settings.config, buildConfig, logging: this.logging }); + await runHookBuildStart({ config: this.settings.config, logging: this.logging }); this.validateConfig(); info(this.logging, 'build', `output target: ${colors.green(this.settings.config.output)}`); diff --git a/packages/astro/src/core/render/result.ts b/packages/astro/src/core/render/result.ts index 5b1c870972b2..0bc054cd7ca8 100644 --- a/packages/astro/src/core/render/result.ts +++ b/packages/astro/src/core/render/result.ts @@ -183,7 +183,7 @@ export function createResult(args: CreateResultArgs): SSRResult { } } - return Reflect.get(request, clientAddressSymbol); + return Reflect.get(request, clientAddressSymbol) as string; }, get cookies() { if (cookies) { @@ -207,59 +207,10 @@ export function createResult(args: CreateResultArgs): SSRResult { }); } : onlyAvailableInSSR('Astro.redirect'), - resolve(path: string) { - 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: ---- -import "${path}"; ---- -`; - } else if (isScriptRequest(path)) { - extra = `It looks like you are resolving scripts. If you are adding a script tag, replace with this: - - - -or consider make it a module like so: - - -`; - } - - 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 ''; - }, response: response as AstroGlobal['response'], slots: astroSlots as unknown as AstroGlobal['slots'], }; - Object.defineProperty(Astro, 'canonicalURL', { - get: function () { - warn( - args.logging, - 'deprecation', - `${bold('Astro.canonicalURL')} is deprecated! Use \`Astro.url\` instead. -Example: - ---- -const canonicalURL = new URL(Astro.url.pathname, Astro.site); ---- -` - ); - return new URL(this.request.url.pathname, this.site); - }, - }); - Object.defineProperty(Astro, '__renderMarkdown', { // Ensure this API is not exposed to users enumerable: false, diff --git a/packages/astro/src/integrations/index.ts b/packages/astro/src/integrations/index.ts index e7ce7e51d629..3733c6b2d0c0 100644 --- a/packages/astro/src/integrations/index.ts +++ b/packages/astro/src/integrations/index.ts @@ -206,55 +206,18 @@ export async function runHookServerDone({ export async function runHookBuildStart({ config, - buildConfig, logging, }: { config: AstroConfig; - buildConfig: BuildConfig; logging: LogOptions; }) { - function warnDeprecated( - integration: AstroIntegration, - prop: 'server' | 'client' | 'serverEntry' - ) { - let value: any = Reflect.get(buildConfig, prop); - Object.defineProperty(buildConfig, prop, { - enumerable: true, - get() { - return value; - }, - set(newValue) { - value = newValue; - warn( - logging, - 'astro:build:start', - `Your adapter ${bold(integration.name)} is using a deprecated API, buildConfig. ${bold( - prop - )} config should be set via config.build.${prop} instead.` - ); - }, - }); - return () => { - Object.defineProperty(buildConfig, prop, { - enumerable: true, - value, - }); - }; - } - for (const integration of config.integrations) { if (integration?.hooks?.['astro:build:start']) { - const undoClientWarning = warnDeprecated(integration, 'client'); - const undoServerWarning = warnDeprecated(integration, 'server'); - const undoServerEntryWarning = warnDeprecated(integration, 'serverEntry'); await withTakingALongTimeMsg({ name: integration.name, - hookResult: integration.hooks['astro:build:start']({ buildConfig }), + hookResult: integration.hooks['astro:build:start'](), logging, }); - undoClientWarning(); - undoServerEntryWarning(); - undoServerWarning(); } } } diff --git a/packages/astro/src/runtime/server/astro-global.ts b/packages/astro/src/runtime/server/astro-global.ts index 5dd17afcef8e..3182026bf35b 100644 --- a/packages/astro/src/runtime/server/astro-global.ts +++ b/packages/astro/src/runtime/server/astro-global.ts @@ -1,13 +1,6 @@ import type { AstroGlobalPartial } from '../../@types/astro'; import { ASTRO_VERSION } from '../../core/constants.js'; -/** Create the Astro.fetchContent() runtime function. */ -function createDeprecatedFetchContentFn() { - return () => { - throw new Error('Deprecated: Astro.fetchContent() has been replaced with Astro.glob().'); - }; -} - /** Create the Astro.glob() runtime function. */ function createAstroGlobFn() { const globHandler = (importMetaGlobResult: Record, globValue: () => any) => { @@ -25,29 +18,15 @@ function createAstroGlobFn() { // This is used to create the top-level Astro global; the one that you can use // Inside of getStaticPaths. +// TODO: remove `_filePathname` and `_projectRootStr` from the compiler export function createAstro( - filePathname: string, - _site: string | undefined, - projectRootStr: string + _filePathname: string, + site: string | undefined, + _projectRootStr: string ): AstroGlobalPartial { - const site = _site ? new URL(_site) : undefined; - const referenceURL = new URL(filePathname, `http://localhost`); - const projectRoot = new URL(projectRootStr); return { - site, + site: site ? new URL(site) : undefined, generator: `Astro v${ASTRO_VERSION}`, - fetchContent: createDeprecatedFetchContentFn(), glob: createAstroGlobFn(), - // INVESTIGATE is there a use-case for multi args? - // TODO remove in 2.0 - resolve(...segments: string[]) { - let resolved = segments.reduce((u, segment) => new URL(segment, u), referenceURL).pathname; - // When inside of project root, remove the leading path so you are - // left with only `/src/images/tower.png` - if (resolved.startsWith(projectRoot.pathname)) { - resolved = '/' + resolved.slice(projectRoot.pathname.length); - } - return resolved; - }, }; } diff --git a/packages/astro/test/ssr-adapter-build-config.test.js b/packages/astro/test/ssr-adapter-build-config.test.js index ab0ccf3af42e..d739cee3cd09 100644 --- a/packages/astro/test/ssr-adapter-build-config.test.js +++ b/packages/astro/test/ssr-adapter-build-config.test.js @@ -1,5 +1,4 @@ import { expect } from 'chai'; -import { load as cheerioLoad } from 'cheerio'; import { loadFixture } from './test-utils.js'; import { viteID } from '../dist/core/util.js'; @@ -8,15 +7,18 @@ describe('Integration buildConfig hook', () => { let fixture; before(async () => { - let _config; fixture = await loadFixture({ root: './fixtures/ssr-request/', output: 'server', adapter: { name: 'my-ssr-adapter', hooks: { - 'astro:config:setup': ({ updateConfig }) => { + 'astro:config:setup': ({ config, updateConfig }) => { updateConfig({ + build: { + server: new URL('./dist/.root/server/', config.root), + client: new URL('./dist/.root/client/', config.root), + }, vite: { plugins: [ { @@ -40,12 +42,7 @@ describe('Integration buildConfig hook', () => { }, }); }, - 'astro:build:start': ({ buildConfig }) => { - buildConfig.server = new URL('./dist/.root/server/', _config.root); - buildConfig.client = new URL('./dist/.root/client/', _config.root); - }, - 'astro:config:done': ({ config, setAdapter }) => { - _config = config; + 'astro:config:done': ({ setAdapter }) => { setAdapter({ name: 'my-ssr-adapter', serverEntrypoint: '@my-ssr', diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index d39f0f8759f8..1df284fbbb19 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -39,14 +39,12 @@ const SERVER_BUILD_FOLDER = '/$server_build/'; export default function createIntegration(args?: Options): AstroIntegration { let _config: AstroConfig; let _buildConfig: BuildConfig; - let needsBuildConfig = false; const isModeDirectory = args?.mode === 'directory'; return { name: '@astrojs/cloudflare', hooks: { 'astro:config:setup': ({ config, updateConfig }) => { - needsBuildConfig = !config.build.client; updateConfig({ build: { client: new URL(`.${config.base}`, config.outDir), @@ -90,14 +88,6 @@ export default function createIntegration(args?: Options): AstroIntegration { vite.ssr.target = vite.ssr.target || 'webworker'; } }, - 'astro:build:start': ({ buildConfig }) => { - // Backwards compat - if (needsBuildConfig) { - buildConfig.client = new URL(`.${_config.base}`, _config.outDir); - buildConfig.server = new URL(`.${SERVER_BUILD_FOLDER}`, _config.outDir); - buildConfig.serverEntry = '_worker.js'; - } - }, 'astro:build:done': async () => { const entryPath = fileURLToPath(new URL(_buildConfig.serverEntry, _buildConfig.server)), entryUrl = new URL(_buildConfig.serverEntry, _config.outDir), diff --git a/packages/integrations/deno/src/index.ts b/packages/integrations/deno/src/index.ts index 968bec24af92..8907c489da01 100644 --- a/packages/integrations/deno/src/index.ts +++ b/packages/integrations/deno/src/index.ts @@ -31,12 +31,10 @@ export function getAdapter(args?: Options): AstroAdapter { export default function createIntegration(args?: Options): AstroIntegration { let _buildConfig: BuildConfig; let _vite: any; - let needsBuildConfig = false; return { name: '@astrojs/deno', hooks: { 'astro:config:done': ({ setAdapter, config }) => { - needsBuildConfig = !config.build.client; setAdapter(getAdapter(args)); _buildConfig = config.build; @@ -47,12 +45,6 @@ export default function createIntegration(args?: Options): AstroIntegration { ); } }, - 'astro:build:start': ({ buildConfig }) => { - // Backwards compat - if (needsBuildConfig) { - _buildConfig = buildConfig; - } - }, 'astro:build:setup': ({ vite, target }) => { if (target === 'server') { _vite = vite; diff --git a/packages/integrations/image/src/index.ts b/packages/integrations/image/src/index.ts index 43f64935be9c..c30ee31ede4b 100644 --- a/packages/integrations/image/src/index.ts +++ b/packages/integrations/image/src/index.ts @@ -53,7 +53,6 @@ export default function integration(options: IntegrationOptions = {}): AstroInte let _config: AstroConfig; let _buildConfig: BuildConfig; - let needsBuildConfig = false; // During SSG builds, this is used to track all transformed images required. const staticImages = new Map>(); @@ -80,7 +79,6 @@ export default function integration(options: IntegrationOptions = {}): AstroInte name: PKG_NAME, hooks: { 'astro:config:setup': async ({ command, config, updateConfig, injectRoute }) => { - needsBuildConfig = !config.build?.server; _config = config; updateConfig({ vite: getViteConfiguration(command === 'dev'), @@ -107,18 +105,13 @@ export default function integration(options: IntegrationOptions = {}): AstroInte _config = config; _buildConfig = config.build; }, - 'astro:build:start': ({ buildConfig }) => { + 'astro:build:start': () => { const adapterName = _config.adapter?.name; if (adapterName && UNSUPPORTED_ADAPTERS.has(adapterName)) { throw new Error( `@astrojs/image is not supported with the ${adapterName} adapter. Please choose a Node.js compatible adapter.` ); } - - // Backwards compat - if (needsBuildConfig) { - _buildConfig = buildConfig; - } }, 'astro:build:setup': async () => { // Used to cache all images rendered to HTML diff --git a/packages/integrations/netlify/src/integration-edge-functions.ts b/packages/integrations/netlify/src/integration-edge-functions.ts index 8e16e059570f..35b660e2cdd2 100644 --- a/packages/integrations/netlify/src/integration-edge-functions.ts +++ b/packages/integrations/netlify/src/integration-edge-functions.ts @@ -111,13 +111,11 @@ export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {}) let _config: AstroConfig; let entryFile: string; let _buildConfig: BuildConfig; - let needsBuildConfig = false; let _vite: any; return { name: '@astrojs/netlify/edge-functions', hooks: { 'astro:config:setup': ({ config, updateConfig }) => { - needsBuildConfig = !config.build.client; // Add a plugin that shims the global environment. const injectPlugin: VitePlugin = { name: '@astrojs/netlify/plugin-inject', @@ -156,15 +154,6 @@ export function netlifyEdgeFunctions({ dist }: NetlifyEdgeFunctionsOptions = {}) ); } }, - 'astro:build:start': ({ buildConfig }) => { - if (needsBuildConfig) { - buildConfig.client = _config.outDir; - buildConfig.server = new URL('./.netlify/edge-functions/', _config.root); - buildConfig.serverEntry = 'entry.js'; - _buildConfig = buildConfig; - entryFile = buildConfig.serverEntry.replace(/\.m?js/, ''); - } - }, 'astro:build:setup': ({ vite, target }) => { if (target === 'server') { _vite = vite; diff --git a/packages/integrations/netlify/src/integration-functions.ts b/packages/integrations/netlify/src/integration-functions.ts index e1d6de42046f..e8ff4bd1f899 100644 --- a/packages/integrations/netlify/src/integration-functions.ts +++ b/packages/integrations/netlify/src/integration-functions.ts @@ -22,12 +22,10 @@ function netlifyFunctions({ }: NetlifyFunctionsOptions = {}): AstroIntegration { let _config: AstroConfig; let entryFile: string; - let needsBuildConfig = false; return { name: '@astrojs/netlify', hooks: { 'astro:config:setup': ({ config, updateConfig }) => { - needsBuildConfig = !config.build.client; const outDir = dist ?? new URL('./dist/', config.root); updateConfig({ outDir, @@ -49,13 +47,6 @@ function netlifyFunctions({ ); } }, - 'astro:build:start': ({ buildConfig }) => { - if (needsBuildConfig) { - buildConfig.client = _config.outDir; - buildConfig.server = new URL('./.netlify/functions-internal/', _config.root); - entryFile = buildConfig.serverEntry.replace(/\.m?js/, ''); - } - }, 'astro:build:done': async ({ routes, dir }) => { await createRedirects(routes, dir, entryFile, false); }, diff --git a/packages/integrations/node/src/index.ts b/packages/integrations/node/src/index.ts index 0453ca9d9359..bbe5e44b4cb6 100644 --- a/packages/integrations/node/src/index.ts +++ b/packages/integrations/node/src/index.ts @@ -16,7 +16,6 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr throw new Error(`[@astrojs/node] Setting the 'mode' option is required.`); } - let needsBuildConfig = false; let _options: Options; return { name: '@astrojs/node', @@ -31,7 +30,6 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr }); }, 'astro:config:done': ({ setAdapter, config }) => { - needsBuildConfig = !config.build?.server; _options = { ...userOptions, client: config.build.client?.toString(), @@ -45,13 +43,6 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr console.warn(`[@astrojs/node] \`output: "server"\` is required to use this adapter.`); } }, - 'astro:build:start': ({ buildConfig }) => { - // Backwards compat - if (needsBuildConfig) { - _options.client = buildConfig.client.toString(); - _options.server = buildConfig.server.toString(); - } - }, }, }; } diff --git a/packages/integrations/vercel/src/edge/adapter.ts b/packages/integrations/vercel/src/edge/adapter.ts index 28a10cecdc0f..c4457ffdd1c2 100644 --- a/packages/integrations/vercel/src/edge/adapter.ts +++ b/packages/integrations/vercel/src/edge/adapter.ts @@ -30,13 +30,11 @@ export default function vercelEdge({ includeFiles = [] }: VercelEdgeConfig = {}) let buildTempFolder: URL; let functionFolder: URL; let serverEntry: string; - let needsBuildConfig = false; return { name: PACKAGE_NAME, hooks: { 'astro:config:setup': ({ config, updateConfig }) => { - needsBuildConfig = !config.build.client; const outDir = getVercelOutput(config.root); updateConfig({ outDir, @@ -61,13 +59,6 @@ export default function vercelEdge({ includeFiles = [] }: VercelEdgeConfig = {}) `); } }, - 'astro:build:start': ({ buildConfig }) => { - if (needsBuildConfig) { - buildConfig.client = new URL('./static/', _config.outDir); - buildTempFolder = buildConfig.server = new URL('./dist/', _config.root); - serverEntry = buildConfig.serverEntry = 'entry.mjs'; - } - }, 'astro:build:setup': ({ vite, target }) => { if (target === 'server') { vite.resolve ||= {}; diff --git a/packages/integrations/vercel/src/serverless/adapter.ts b/packages/integrations/vercel/src/serverless/adapter.ts index 61528679c105..3ff5eb3e57a9 100644 --- a/packages/integrations/vercel/src/serverless/adapter.ts +++ b/packages/integrations/vercel/src/serverless/adapter.ts @@ -29,13 +29,11 @@ export default function vercelServerless({ let buildTempFolder: URL; let functionFolder: URL; let serverEntry: string; - let needsBuildConfig = false; return { name: PACKAGE_NAME, hooks: { 'astro:config:setup': ({ config, updateConfig }) => { - needsBuildConfig = !config.build.client; const outDir = getVercelOutput(config.root); updateConfig({ outDir, @@ -60,13 +58,6 @@ export default function vercelServerless({ `); } }, - 'astro:build:start': ({ buildConfig }) => { - if (needsBuildConfig) { - buildConfig.client = new URL('./static/', _config.outDir); - buildTempFolder = buildConfig.server = new URL('./dist/', _config.root); - serverEntry = buildConfig.serverEntry = 'entry.js'; - } - }, 'astro:build:done': async ({ routes }) => { // Merge any includes from `vite.assetsInclude const inc = includeFiles?.map((file) => new URL(file, _config.root)) || [];