Skip to content

Commit

Permalink
Fix build.client and build.server resolve behaviour (#11916)
Browse files Browse the repository at this point in the history
Co-authored-by: Florian Lefebvre <[email protected]>
Co-authored-by: Sarah Rainsberger <[email protected]>
  • Loading branch information
3 people authored Sep 9, 2024
1 parent fa4671c commit 46ea29f
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 128 deletions.
15 changes: 15 additions & 0 deletions .changeset/chilly-terms-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'astro': major
---

Updates how the `build.client` and `build.server` option values get resolved to match existing documentation. With this fix, the option values will now correctly resolve relative to the `outDir` option. So if `outDir` is set to `./dist/nested/`, then by default:

- `build.client` will resolve to `<root>/dist/nested/client/`
- `build.server` will resolve to `<root>/dist/nested/server/`

Previously the values were incorrectly resolved:

- `build.client` was resolved to `<root>/dist/nested/dist/client/`
- `build.server` was resolved to `<root>/dist/nested/dist/server/`

If you were relying on the previous build paths, make sure that your project code is updated to the new build paths.
45 changes: 29 additions & 16 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { type BuiltinTheme, bundledThemes } from 'shiki';

import type { OutgoingHttpHeaders } from 'node:http';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { z } from 'zod';
import { EnvSchema } from '../../env/schema.js';
import type { AstroUserConfig, ViteUserConfig } from '../../types/public/config.js';
Expand Down Expand Up @@ -57,8 +57,8 @@ export const ASTRO_CONFIG_DEFAULTS = {
trailingSlash: 'ignore',
build: {
format: 'directory',
client: './dist/client/',
server: './dist/server/',
client: './client/',
server: './server/',
assets: '_astro',
serverEntry: 'entry.mjs',
redirects: true,
Expand Down Expand Up @@ -540,6 +540,9 @@ export const AstroConfigSchema = z.object({
export type AstroConfigType = z.infer<typeof AstroConfigSchema>;

export function createRelativeSchema(cmd: string, fileProtocolRoot: string) {
let originalBuildClient: string;
let originalBuildServer: string;

// We need to extend the global schema to add transforms that are relative to root.
// This is type checked against the global schema to make sure we still match.
const AstroConfigRelativeSchema = AstroConfigSchema.extend({
Expand Down Expand Up @@ -570,16 +573,30 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: string) {
.union([z.literal('file'), z.literal('directory'), z.literal('preserve')])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.format),
// NOTE: `client` and `server` are transformed relative to the default outDir first,
// later we'll fix this to be relative to the actual `outDir`
client: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.client)
.transform((val) => resolveDirAsUrl(val, fileProtocolRoot)),
.transform((val) => {
originalBuildClient = val;
return resolveDirAsUrl(
val,
path.resolve(fileProtocolRoot, ASTRO_CONFIG_DEFAULTS.outDir),
);
}),
server: z
.string()
.optional()
.default(ASTRO_CONFIG_DEFAULTS.build.server)
.transform((val) => resolveDirAsUrl(val, fileProtocolRoot)),
.transform((val) => {
originalBuildServer = val;
return resolveDirAsUrl(
val,
path.resolve(fileProtocolRoot, ASTRO_CONFIG_DEFAULTS.outDir),
);
}),
assets: z.string().optional().default(ASTRO_CONFIG_DEFAULTS.build.assets),
assetsPrefix: z
.string()
Expand Down Expand Up @@ -636,19 +653,15 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: string) {
),
})
.transform((config) => {
// If the user changed outDir but not build.server, build.config, adjust so those
// are relative to the outDir, as is the expected default.
if (
!config.build.server.toString().startsWith(config.outDir.toString()) &&
config.build.server.toString().endsWith('dist/server/')
) {
config.build.server = new URL('./dist/server/', config.outDir);
}
// If the user changed `outDir`, we need to also update `build.client` and `build.server`
// the be based on the correct `outDir`
if (
!config.build.client.toString().startsWith(config.outDir.toString()) &&
config.build.client.toString().endsWith('dist/client/')
config.outDir.toString() !==
resolveDirAsUrl(ASTRO_CONFIG_DEFAULTS.outDir, fileProtocolRoot).toString()
) {
config.build.client = new URL('./dist/client/', config.outDir);
const outDirPath = fileURLToPath(config.outDir);
config.build.client = resolveDirAsUrl(originalBuildClient, outDirPath);
config.build.server = resolveDirAsUrl(originalBuildServer, outDirPath);
}

// Handle `base` trailing slash based on `trailingSlash` config
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/types/public/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ export interface AstroUserConfig {
* @docs
* @name build.client
* @type {string}
* @default `'./dist/client'`
* @default `'./client'`
* @description
* Controls the output directory of your client-side CSS and JavaScript when building a website with server-rendered pages.
* `outDir` controls where the code is built to.
Expand All @@ -581,7 +581,7 @@ export interface AstroUserConfig {
* @docs
* @name build.server
* @type {string}
* @default `'./dist/server'`
* @default `'./server'`
* @description
* Controls the output directory of server JavaScript when building to SSR.
*
Expand Down
12 changes: 0 additions & 12 deletions packages/astro/test/astro-assets-prefix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ describe('Assets Prefix - Static', () => {
fixture = await loadFixture({
root: './fixtures/astro-assets-prefix/',
outDir: './dist/static',
build: {
client: './dist/static/client',
server: './dist/static/server',
},
});
await fixture.build();
});
Expand Down Expand Up @@ -79,8 +75,6 @@ describe('Assets Prefix - with path prefix', () => {
root: './fixtures/astro-assets-prefix/',
outDir: './dist/server',
build: {
client: './dist/server/client',
server: './dist/server/server',
assetsPrefix: '/starting-slash',
},
});
Expand All @@ -106,10 +100,6 @@ describe('Assets Prefix, server', () => {
output: 'server',
adapter: testAdapter(),
outDir: './dist/server',
build: {
client: './dist/server/client',
server: './dist/server/server',
},
});
await fixture.build();
app = await fixture.loadTestAdapterApp();
Expand Down Expand Up @@ -169,8 +159,6 @@ describe('Assets Prefix, with path prefix', () => {
adapter: testAdapter(),
outDir: './dist/server-path-prefix',
build: {
client: './dist/server-path-prefix/client',
server: './dist/server-path-prefix/server',
assetsPrefix: '/starting-slash',
},
});
Expand Down
16 changes: 0 additions & 16 deletions packages/astro/test/before-hydration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ describe('Astro Scripts before-hydration', () => {
fixture = await loadFixture({
root: './fixtures/before-hydration/',
outDir: './dist/static-integration',
build: {
client: './dist/static-integration/client',
server: './dist/static-integration/server',
},
integrations: [
preact(),
{
Expand Down Expand Up @@ -74,10 +70,6 @@ describe('Astro Scripts before-hydration', () => {
fixture = await loadFixture({
root: './fixtures/before-hydration/',
outDir: './dist/static-no-integration',
build: {
client: './dist/static-no-integration/client',
server: './dist/static-no-integration/server',
},
});
});

Expand Down Expand Up @@ -126,10 +118,6 @@ describe('Astro Scripts before-hydration', () => {
output: 'server',
adapter: testAdapter(),
outDir: './dist/server-integration',
build: {
client: './dist/server-integration/client',
server: './dist/server-integration/server',
},
integrations: [
preact(),
{
Expand Down Expand Up @@ -169,10 +157,6 @@ describe('Astro Scripts before-hydration', () => {
root: './fixtures/before-hydration/',
output: 'server',
outDir: './dist/static-no-integration',
build: {
client: './dist/static-no-integration/client',
server: './dist/static-no-integration/server',
},
adapter: testAdapter(),
});
});
Expand Down
12 changes: 0 additions & 12 deletions packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,10 +814,6 @@ describe('astro:image', () => {
root: './fixtures/core-image-ssr/',
output: 'server',
outDir: './dist/server-base-path',
build: {
client: './dist/server-base-path/client',
server: './dist/server-base-path/server',
},
adapter: testAdapter(),
image: {
service: testImageService(),
Expand Down Expand Up @@ -1100,10 +1096,6 @@ describe('astro:image', () => {
root: './fixtures/core-image-ssr/',
output: 'server',
outDir: './dist/server-dev',
build: {
client: './dist/server-dev/client',
server: './dist/server-dev/server',
},
adapter: testAdapter(),
base: 'some-base',
image: {
Expand Down Expand Up @@ -1139,10 +1131,6 @@ describe('astro:image', () => {
root: './fixtures/core-image-ssr/',
output: 'server',
outDir: './dist/server-prod',
build: {
client: './dist/server-prod/client',
server: './dist/server-prod/server',
},
adapter: testAdapter(),
image: {
endpoint: 'astro/assets/endpoint/node',
Expand Down
12 changes: 0 additions & 12 deletions packages/astro/test/css-inline-stylesheets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ describe('Setting inlineStylesheets to never in static output', () => {
output: 'static',
outDir: './dist/static-inline-stylesheets-never',
build: {
client: './dist/static-inline-stylesheets-never/client',
server: './dist/static-inline-stylesheets-never/server',
inlineStylesheets: 'never',
},
});
Expand Down Expand Up @@ -58,8 +56,6 @@ describe('Setting inlineStylesheets to never in server output', () => {
adapter: testAdapter(),
outDir: './dist/server-inline-stylesheets-never',
build: {
client: './dist/server-inline-stylesheets-never/client',
server: './dist/server-inline-stylesheets-never/server',
inlineStylesheets: 'never',
},
});
Expand Down Expand Up @@ -100,8 +96,6 @@ describe('Setting inlineStylesheets to auto in static output', () => {
output: 'static',
outDir: './dist/static-inline-stylesheets-auto',
build: {
client: './dist/static-inline-stylesheets-auto/client',
server: './dist/static-inline-stylesheets-auto/server',
inlineStylesheets: 'auto',
},
vite: {
Expand Down Expand Up @@ -148,8 +142,6 @@ describe('Setting inlineStylesheets to auto in server output', () => {
adapter: testAdapter(),
outDir: './dist/server-inline-stylesheets-auto',
build: {
client: './dist/server-inline-stylesheets-auto/client',
server: './dist/server-inline-stylesheets-auto/server',
inlineStylesheets: 'auto',
},
vite: {
Expand Down Expand Up @@ -198,8 +190,6 @@ describe('Setting inlineStylesheets to always in static output', () => {
output: 'static',
outDir: './dist/static-inline-stylesheets-always',
build: {
client: './dist/static-inline-stylesheets-always/client',
server: './dist/static-inline-stylesheets-always/server',
inlineStylesheets: 'always',
},
});
Expand Down Expand Up @@ -238,8 +228,6 @@ describe('Setting inlineStylesheets to always in server output', () => {
adapter: testAdapter(),
outDir: './dist/server-inline-stylesheets-always',
build: {
client: './dist/server-inline-stylesheets-always/client',
server: './dist/server-inline-stylesheets-always/server',
inlineStylesheets: 'always',
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ describe('Experimental Content Collections cache - inlineStylesheets to never in
adapter: testAdapter(),
outDir: './dist/inline-stylesheets-never',
build: {
client: './dist/inline-stylesheets-never/client',
server: './dist/inline-stylesheets-never/server',
inlineStylesheets: 'never',
},
experimental: {
Expand Down Expand Up @@ -108,8 +106,6 @@ describe('Experimental Content Collections cache - inlineStylesheets to auto in
output: 'static',
outDir: './dist/inline-stylesheets-auto',
build: {
client: './dist/inline-stylesheets-auto/client',
server: './dist/inline-stylesheets-auto/server',
inlineStylesheets: 'auto',
},
vite: {
Expand Down Expand Up @@ -210,8 +206,6 @@ describe('Setting inlineStylesheets to always in server output', () => {
adapter: testAdapter(),
outDir: './dist/inline-stylesheets-always',
build: {
client: './dist/inline-stylesheets-always/client',
server: './dist/inline-stylesheets-always/server',
inlineStylesheets: 'always',
},
experimental: {
Expand Down
16 changes: 0 additions & 16 deletions packages/astro/test/i18n-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1337,10 +1337,6 @@ describe('[SSR] i18n routing', () => {
root: './fixtures/i18n-routing-prefix-always/',
output: 'server',
outDir: './dist/pathname-prefix-always-no-redirect',
build: {
client: './dist/pathname-prefix-always-no-redirect/client',
server: './dist/pathname-prefix-always-no-redirect/server',
},
adapter: testAdapter(),
i18n: {
routing: {
Expand Down Expand Up @@ -1628,10 +1624,6 @@ describe('[SSR] i18n routing', () => {
root: './fixtures/i18n-routing/',
output: 'server',
outDir: './dist/locales-underscore',
build: {
client: './dist/locales-underscore/client',
server: './dist/locales-underscore/server',
},
adapter: testAdapter(),
i18n: {
defaultLocale: 'en',
Expand Down Expand Up @@ -1902,10 +1894,6 @@ describe('SSR fallback from missing locale index to default locale index', () =>
root: './fixtures/i18n-routing-prefix-other-locales/',
output: 'server',
outDir: './dist/missing-locale-to-default',
build: {
client: './dist/missing-locale-to-default/client',
server: './dist/missing-locale-to-default/server',
},
adapter: testAdapter(),
i18n: {
defaultLocale: 'en',
Expand Down Expand Up @@ -2003,10 +1991,6 @@ describe('Fallback rewrite SSR', () => {
root: './fixtures/i18n-routing-fallback/',
output: 'server',
outDir: './dist/i18n-routing-fallback',
build: {
client: './dist/i18n-routing-fallback/client',
server: './dist/i18n-routing-fallback/server',
},
adapter: testAdapter(),
i18n: {
defaultLocale: 'en',
Expand Down
8 changes: 0 additions & 8 deletions packages/astro/test/ssr-prerender.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ describe('SSR: prerender', () => {
root: './fixtures/ssr-prerender/',
output: 'server',
outDir: './dist/normal',
build: {
client: './dist/normal/client',
server: './dist/normal/server',
},
adapter: testAdapter(),
});
await fixture.build();
Expand Down Expand Up @@ -93,10 +89,6 @@ describe.skip('Integrations can hook into the prerendering decision', () => {
root: './fixtures/ssr-prerender/',
output: 'server',
outDir: './dist/integration-prerender',
build: {
client: './dist/integration-prerender/client',
server: './dist/integration-prerender/server',
},
integrations: [testIntegration],
adapter: testAdapter(),
});
Expand Down
Loading

0 comments on commit 46ea29f

Please sign in to comment.