From 60140374418ff0ee80899615be8e718ae57f791a Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 27 Jun 2023 11:58:23 +0100 Subject: [PATCH] fix: correct URL for entry points (#7490) Co-authored-by: Bjorn Lu --- .changeset/brave-waves-battle.md | 5 +++++ packages/astro/CHANGELOG.md | 17 ++++++++++++++ packages/astro/src/@types/astro.ts | 15 ------------- packages/astro/src/core/build/generate.ts | 6 ++--- packages/astro/src/core/build/index.ts | 15 +------------ .../src/core/build/plugins/plugin-ssr.ts | 2 +- packages/astro/src/core/build/static-build.ts | 22 +++++++++++-------- packages/astro/src/core/build/types.ts | 2 -- packages/astro/src/integrations/index.ts | 9 ++------ .../astro/test/ssr-split-manifest.test.js | 2 +- 10 files changed, 43 insertions(+), 52 deletions(-) create mode 100644 .changeset/brave-waves-battle.md diff --git a/.changeset/brave-waves-battle.md b/.changeset/brave-waves-battle.md new file mode 100644 index 000000000000..ebfde5863d8c --- /dev/null +++ b/.changeset/brave-waves-battle.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix the URL that belongs to `entryPoints` in the hook `astro:build:ssr`. The paths were created with the wrong output directory. diff --git a/packages/astro/CHANGELOG.md b/packages/astro/CHANGELOG.md index 917271ba12c4..26b000ee06e5 100644 --- a/packages/astro/CHANGELOG.md +++ b/packages/astro/CHANGELOG.md @@ -36,6 +36,23 @@ }); ``` +- [#7220](https://github.com/withastro/astro/pull/7220) [`459b5bd05`](https://github.com/withastro/astro/commit/459b5bd05f562238f7250520efe3cf0fa156bb45) Thanks [@ematipico](https://github.com/ematipico)! - The Astro hook `astro:build:ssr` now receives a new option in their payload, called `entryPoints`. + + `entryPoints` is defined as a `Map`, where `RouteData` represents the information of a Astro route and `URL` is the path to the physical file emitted at the end of the build. + + ```ts + export function integration(): AstroIntegration { + return { + name: "my-integration", + hooks: { + "astro:build:ssr": ({ entryPoints }) => { + // do something with `entryPoints` + } + } + } + } + ``` + ### Patch Changes - [#7438](https://github.com/withastro/astro/pull/7438) [`30bb36371`](https://github.com/withastro/astro/commit/30bb363713e3d2c50d0d4816d970aa93b836a3b0) Thanks [@bluwy](https://github.com/bluwy)! - Fix `astro:build:setup` hook `updateConfig` utility, where the configuration wasn't correctly updated when the hook was fired. diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 43069aa1795d..cca5753fef8b 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -111,21 +111,6 @@ export interface CLIFlags { experimentalRedirects?: boolean; } -export interface BuildConfig { - /** - * @deprecated Use config.build.client instead. - */ - client: URL; - /** - * @deprecated Use config.build.server instead. - */ - server: URL; - /** - * @deprecated Use config.build.serverEntry instead. - */ - serverEntry: string; -} - /** * Astro global available in all contexts in .astro files * diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 551f7afa8395..b53360d125a5 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -133,8 +133,9 @@ export function chunkIsPage( export async function generatePages(opts: StaticBuildOptions, internals: BuildInternals) { const timer = performance.now(); const ssr = isServerLikeOutput(opts.settings.config); - const serverEntry = opts.buildConfig.serverEntry; - const outFolder = ssr ? opts.buildConfig.server : getOutDirWithinCwd(opts.settings.config.outDir); + const outFolder = ssr + ? opts.settings.config.build.server + : getOutDirWithinCwd(opts.settings.config.outDir); if (ssr && !hasPrerenderedPages(internals)) return; @@ -180,7 +181,6 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn await runHookBuildGenerated({ config: opts.settings.config, - buildConfig: opts.buildConfig, logging: opts.logging, }); diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index 9e72e6e36fdb..ca1c98aff29a 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -1,11 +1,5 @@ import type { AstroTelemetry } from '@astrojs/telemetry'; -import type { - AstroConfig, - AstroSettings, - BuildConfig, - ManifestData, - RuntimeMode, -} from '../../@types/astro'; +import type { AstroConfig, AstroSettings, ManifestData, RuntimeMode } from '../../@types/astro'; import fs from 'fs'; import * as colors from 'kleur/colors'; @@ -123,11 +117,6 @@ class AstroBuilder { /** Run the build logic. build() is marked private because usage should go through ".run()" */ private async build({ viteConfig }: { viteConfig: vite.InlineConfig }) { - const buildConfig: BuildConfig = { - client: this.settings.config.build.client, - server: this.settings.config.build.server, - serverEntry: this.settings.config.build.serverEntry, - }; await runHookBuildStart({ config: this.settings.config, logging: this.logging }); this.validateConfig(); @@ -168,7 +157,6 @@ class AstroBuilder { routeCache: this.routeCache, teardownCompiler: this.teardownCompiler, viteConfig, - buildConfig, }; const { internals } = await viteBuild(opts); @@ -188,7 +176,6 @@ class AstroBuilder { // You're done! Time to clean up. await runHookBuildDone({ config: this.settings.config, - buildConfig, pages: pageNames, routes: Object.values(allPages).map((pd) => pd.route), logging: this.logging, diff --git a/packages/astro/src/core/build/plugins/plugin-ssr.ts b/packages/astro/src/core/build/plugins/plugin-ssr.ts index 486c314340b7..ebda7fb3e650 100644 --- a/packages/astro/src/core/build/plugins/plugin-ssr.ts +++ b/packages/astro/src/core/build/plugins/plugin-ssr.ts @@ -382,7 +382,7 @@ function storeEntryPoint( const componentPath = getPathFromVirtualModulePageName(RESOLVED_SPLIT_MODULE_ID, moduleKey); for (const [page, pageData] of Object.entries(options.allPages)) { if (componentPath == page) { - const publicPath = fileURLToPath(options.settings.config.outDir); + const publicPath = fileURLToPath(options.settings.config.build.server); internals.entryPoints.set(pageData.route, pathToFileURL(join(publicPath, fileName))); } } diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 7d6004c0dd89..0be7f8739502 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -3,7 +3,7 @@ import * as eslexer from 'es-module-lexer'; import glob from 'fast-glob'; import fs from 'fs'; import { bgGreen, bgMagenta, black, dim } from 'kleur/colors'; -import { extname } from 'node:path'; +import { extname, join } from 'node:path'; import path from 'path'; import { fileURLToPath } from 'url'; import * as vite from 'vite'; @@ -146,7 +146,7 @@ async function ssrBuild( ) { const { allPages, settings, viteConfig } = opts; const ssr = isServerLikeOutput(settings.config); - const out = ssr ? opts.buildConfig.server : getOutDirWithinCwd(settings.config.outDir); + const out = ssr ? settings.config.build.server : getOutDirWithinCwd(settings.config.outDir); const routes = Object.values(allPages).map((pd) => pd.route); const { lastVitePlugins, vitePlugins } = container.runBeforeHook('ssr', input); @@ -227,7 +227,7 @@ async function clientBuild( const { settings, viteConfig } = opts; const timer = performance.now(); const ssr = isServerLikeOutput(settings.config); - const out = ssr ? opts.buildConfig.client : getOutDirWithinCwd(settings.config.outDir); + const out = ssr ? settings.config.build.client : getOutDirWithinCwd(settings.config.outDir); // Nothing to do if there is no client-side JS. if (!input.size) { @@ -289,12 +289,12 @@ async function runPostBuildHooks( ) { const mutations = await container.runPostHook(ssrReturn, clientReturn); const config = container.options.settings.config; - const buildConfig = container.options.settings.config.build; + const build = container.options.settings.config.build; for (const [fileName, mutation] of mutations) { const root = isServerLikeOutput(config) ? mutation.build === 'server' - ? buildConfig.server - : buildConfig.client + ? build.server + : build.client : config.outDir; const fileURL = new URL(fileName, root); await fs.promises.mkdir(new URL('./', fileURL), { recursive: true }); @@ -313,7 +313,9 @@ async function cleanStaticOutput(opts: StaticBuildOptions, internals: BuildInter allStaticFiles.add(internals.pageToBundleMap.get(pageData.moduleSpecifier)); } const ssr = isServerLikeOutput(opts.settings.config); - const out = ssr ? opts.buildConfig.server : getOutDirWithinCwd(opts.settings.config.outDir); + const out = ssr + ? opts.settings.config.build.server + : getOutDirWithinCwd(opts.settings.config.outDir); // The SSR output is all .mjs files, the client output is not. const files = await glob('**/*.mjs', { cwd: fileURLToPath(out), @@ -394,8 +396,10 @@ async function copyFiles(fromFolder: URL, toFolder: URL, includeDotfiles = false async function ssrMoveAssets(opts: StaticBuildOptions) { info(opts.logging, 'build', 'Rearranging server assets...'); const serverRoot = - opts.settings.config.output === 'static' ? opts.buildConfig.client : opts.buildConfig.server; - const clientRoot = opts.buildConfig.client; + opts.settings.config.output === 'static' + ? opts.settings.config.build.client + : opts.settings.config.build.server; + const clientRoot = opts.settings.config.build.client; const assets = opts.settings.config.build.assets; const serverAssets = new URL(`./${assets}/`, appendForwardSlash(serverRoot.toString())); const clientAssets = new URL(`./${assets}/`, appendForwardSlash(clientRoot.toString())); diff --git a/packages/astro/src/core/build/types.ts b/packages/astro/src/core/build/types.ts index 5b6db5d49d9a..5089475a9960 100644 --- a/packages/astro/src/core/build/types.ts +++ b/packages/astro/src/core/build/types.ts @@ -2,7 +2,6 @@ import type { default as vite, InlineConfig } from 'vite'; import type { AstroConfig, AstroSettings, - BuildConfig, ComponentInstance, ManifestData, MiddlewareHandler, @@ -36,7 +35,6 @@ export type AllPagesData = Record; export interface StaticBuildOptions { allPages: AllPagesData; settings: AstroSettings; - buildConfig: BuildConfig; logging: LogOptions; manifest: ManifestData; mode: RuntimeMode; diff --git a/packages/astro/src/integrations/index.ts b/packages/astro/src/integrations/index.ts index f0abd1576263..eaf4b21d1fb4 100644 --- a/packages/astro/src/integrations/index.ts +++ b/packages/astro/src/integrations/index.ts @@ -7,7 +7,6 @@ import type { AstroConfig, AstroRenderer, AstroSettings, - BuildConfig, ContentEntryType, DataEntryType, HookParameters, @@ -323,14 +322,12 @@ export async function runHookBuildSsr({ export async function runHookBuildGenerated({ config, - buildConfig, logging, }: { config: AstroConfig; - buildConfig: BuildConfig; logging: LogOptions; }) { - const dir = isServerLikeOutput(config) ? buildConfig.client : config.outDir; + const dir = isServerLikeOutput(config) ? config.build.client : config.outDir; for (const integration of config.integrations) { if (integration?.hooks?.['astro:build:generated']) { @@ -345,18 +342,16 @@ export async function runHookBuildGenerated({ export async function runHookBuildDone({ config, - buildConfig, pages, routes, logging, }: { config: AstroConfig; - buildConfig: BuildConfig; pages: string[]; routes: RouteData[]; logging: LogOptions; }) { - const dir = isServerLikeOutput(config) ? buildConfig.client : config.outDir; + const dir = isServerLikeOutput(config) ? config.build.client : config.outDir; await fs.promises.mkdir(dir, { recursive: true }); for (const integration of config.integrations) { diff --git a/packages/astro/test/ssr-split-manifest.test.js b/packages/astro/test/ssr-split-manifest.test.js index 5005f6279953..6d3167bec120 100644 --- a/packages/astro/test/ssr-split-manifest.test.js +++ b/packages/astro/test/ssr-split-manifest.test.js @@ -41,7 +41,7 @@ describe('astro:ssr-manifest, split', () => { it('should give access to entry points that exists on file system', async () => { // number of the pages inside src/ expect(entryPoints.size).to.equal(4); - for (const fileUrl in entryPoints.values()) { + for (const fileUrl of entryPoints.values()) { let filePath = fileURLToPath(fileUrl); expect(existsSync(filePath)).to.be.true; }