Skip to content

Commit

Permalink
Merge branch 'feat/next-config-client-reference' of https://github.co…
Browse files Browse the repository at this point in the history
…m/withastro/astro into feat/next-config-client-reference
  • Loading branch information
florian-lefebvre committed Sep 9, 2024
2 parents 62dd4f0 + 927f9b3 commit 16fd30a
Show file tree
Hide file tree
Showing 76 changed files with 444 additions and 552 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-worms-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Updates error messages that assume content collections are located in `src/content/` with more generic language
6 changes: 3 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"editor.defaultFormatter": "biomejs.biome"
},
"editor.codeActionsOnSave": {
"quickFix.biome": "explicit",
"source.fixAll.biome": "explicit"
}
"quickFix.biome": "explicit",
"source.fixAll.biome": "explicit"
}
}
8 changes: 7 additions & 1 deletion packages/astro/e2e/custom-client-directives.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ test.describe('Custom Client Directives - build server', () => {

test.beforeAll(async ({ astro }) => {
await astro.build({
adapter: testAdapter(),
adapter: testAdapter({
extendAdapter: {
adapterFeatures: {
forceServerOutput: false,
},
},
}),
});
previewServer = await astro.preview();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/e2e/fixtures/actions-blog/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import node from '@astrojs/node';
export default defineConfig({
site: 'https://example.com',
integrations: [db(), react()],
output: 'hybrid',
output: 'static',
adapter: node({
mode: 'standalone',
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import node from '@astrojs/node';
export default defineConfig({
site: 'https://example.com',
integrations: [db(), react()],
output: 'hybrid',
output: 'static',
adapter: node({
mode: 'standalone',
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import nodejs from '@astrojs/node';
// https://astro.build/config
export default defineConfig({
base: '/base',
output: 'hybrid',
output: 'static',
adapter: nodejs({ mode: 'standalone' }),
integrations: [react(), mdx()],
trailingSlash: process.env.TRAILING_SLASH ?? 'always',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import HTMLError from '../components/HTMLError.astro';
import { generateLongText } from '../lorem';
const content = generateLongText(5);
export const prerender = false;
---

<html>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { defineConfig } from 'astro/config';

// https://astro.build/config
export default defineConfig({
output: 'hybrid',
output: 'static',
adapter: nodejs({ mode: 'standalone' }),
integrations: [react(),vue(),svelte()],
redirects: {
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@
"test:e2e:chrome": "playwright test",
"test:e2e:firefox": "playwright test --config playwright.firefox.config.js",
"test:types": "tsc --project tsconfig.tests.json",
"test:node": "astro-scripts test \"test/**/*.test.js\""
"test:node": "astro-scripts test \"test/**/*.test.js\"",
"test:units": "astro-scripts test \"test/**/units/**/*.test.js\""
},
"dependencies": {
"@astrojs/compiler": "^2.10.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/actions/integration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ActionsWithoutServerOutputError } from '../core/errors/errors-data.js';
import { AstroError } from '../core/errors/errors.js';
import { isServerLikeOutput, viteID } from '../core/util.js';
import { viteID } from '../core/util.js';
import type { AstroSettings } from '../types/astro.js';
import type { AstroIntegration } from '../types/public/integrations.js';
import { ACTIONS_TYPES_FILE, VIRTUAL_MODULE_ID } from './consts.js';
Expand Down Expand Up @@ -30,7 +30,7 @@ export default function astroIntegrationActionsRouteHandler({
});
},
'astro:config:done': async (params) => {
if (!isServerLikeOutput(params.config)) {
if (params.buildOutput === 'static') {
const error = new AstroError(ActionsWithoutServerOutputError);
error.stack = undefined;
throw error;
Expand Down
8 changes: 4 additions & 4 deletions packages/astro/src/assets/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { AstroError } from '../../core/errors/errors.js';
import { AstroErrorData } from '../../core/errors/index.js';
import type { Logger } from '../../core/logger/core.js';
import { isRemotePath, removeLeadingForwardSlash } from '../../core/path.js';
import { isServerLikeOutput } from '../../core/util.js';
import type { MapValue } from '../../type-utils.js';
import type { AstroConfig } from '../../types/public/config.js';
import { getConfiguredImageService } from '../internal.js';
Expand Down Expand Up @@ -50,7 +49,7 @@ export async function prepareAssetsGenerationEnv(
pipeline: BuildPipeline,
totalCount: number,
): Promise<AssetEnv> {
const { config, logger } = pipeline;
const { config, logger, settings } = pipeline;
let useCache = true;
const assetsCacheDir = new URL('assets/', config.cacheDir);
const count = { total: totalCount, current: 1 };
Expand All @@ -66,8 +65,9 @@ export async function prepareAssetsGenerationEnv(
useCache = false;
}

const isServerOutput = settings.buildOutput === 'server';
let serverRoot: URL, clientRoot: URL;
if (isServerLikeOutput(config)) {
if (isServerOutput) {
serverRoot = config.build.server;
clientRoot = config.build.client;
} else {
Expand All @@ -77,7 +77,7 @@ export async function prepareAssetsGenerationEnv(

return {
logger,
isSSR: isServerLikeOutput(config),
isSSR: isServerOutput,
count,
useCache,
assetsCacheDir,
Expand Down
48 changes: 40 additions & 8 deletions packages/astro/src/assets/endpoint/config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
import type { AstroSettings } from '../../types/astro.js';
import { resolveInjectedRoute } from '../../core/routing/manifest/create.js';
import type { AstroSettings, ManifestData } from '../../types/astro.js';
import type { RouteData } from '../../types/public/internal.js';

export function injectImageEndpoint(settings: AstroSettings, mode: 'dev' | 'build') {
export function injectImageEndpoint(
settings: AstroSettings,
manifest: ManifestData,
mode: 'dev' | 'build',
cwd?: string,
) {
manifest.routes.push(getImageEndpointData(settings, mode, cwd));
}

export function ensureImageEndpointRoute(
settings: AstroSettings,
manifest: ManifestData,
mode: 'dev' | 'build',
cwd?: string,
) {
if (!manifest.routes.some((route) => route.route === '/_image')) {
manifest.routes.push(getImageEndpointData(settings, mode, cwd));
}
}

function getImageEndpointData(
settings: AstroSettings,
mode: 'dev' | 'build',
cwd?: string,
): RouteData {
const endpointEntrypoint =
settings.config.image.endpoint ??
(mode === 'dev' ? 'astro/assets/endpoint/node' : 'astro/assets/endpoint/generic');

settings.injectedRoutes.push({
pattern: '/_image',
entrypoint: endpointEntrypoint,
return {
type: 'endpoint',
isIndex: false,
route: '/_image',
pattern: /^\/_image$/,
segments: [[{ content: '_image', dynamic: false, spread: false }]],
params: [],
component: resolveInjectedRoute(endpointEntrypoint, settings.config.root, cwd).component,
generate: () => '',
pathname: '/_image',
prerender: false,
});

return settings;
fallbackRoutes: [],
};
}
5 changes: 2 additions & 3 deletions packages/astro/src/assets/vite-plugin-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
removeBase,
removeQueryString,
} from '../core/path.js';
import { isServerLikeOutput } from '../core/util.js';
import type { AstroPluginOptions, AstroSettings } from '../types/astro.js';
import { VALID_INPUT_FORMATS, VIRTUAL_MODULE_ID, VIRTUAL_SERVICE_ID } from './consts.js';
import type { ImageTransform } from './types.js';
Expand Down Expand Up @@ -131,7 +130,7 @@ export default function assets({
// so that it's tree-shaken away for all platforms that don't need it.
export const outDir = /* #__PURE__ */ new URL(${JSON.stringify(
new URL(
isServerLikeOutput(settings.config)
settings.buildOutput === 'server'
? settings.config.build.client
: settings.config.outDir,
),
Expand Down Expand Up @@ -222,7 +221,7 @@ export default function assets({
if (options?.ssr) {
return `export default ${getProxyCode(
imageMetadata,
isServerLikeOutput(settings.config),
settings.buildOutput === 'server',
)}`;
} else {
globalThis.astroAsset.referencedImages.add(imageMetadata.fsPath);
Expand Down
4 changes: 3 additions & 1 deletion packages/astro/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { UserConfig as ViteUserConfig } from 'vite';
import { Logger } from '../core/logger/core.js';
import { createRouteManifest } from '../core/routing/index.js';
import type { AstroInlineConfig, AstroUserConfig } from '../types/public/config.js';

export function defineConfig(config: AstroUserConfig) {
Expand Down Expand Up @@ -40,6 +41,7 @@ export function getViteConfig(
const { astroConfig: config } = await resolveConfig(inlineAstroConfig, cmd);
let settings = await createSettings(config, userViteConfig.root);
settings = await runHookConfigSetup({ settings, command: cmd, logger });
const manifest = await createRouteManifest({ settings }, logger);
const viteConfig = await createVite(
{
mode,
Expand All @@ -48,7 +50,7 @@ export function getViteConfig(
astroContentListenPlugin({ settings, logger, fs }),
],
},
{ settings, logger, mode, sync: false },
{ settings, logger, mode, sync: false, manifest },
);
await runHookConfigDone({ settings, logger });
return mergeConfig(viteConfig, userViteConfig);
Expand Down
5 changes: 2 additions & 3 deletions packages/astro/src/content/vite-plugin-content-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { getProxyCode } from '../assets/utils/proxy.js';
import { AstroError } from '../core/errors/errors.js';
import { AstroErrorData } from '../core/errors/index.js';
import type { Logger } from '../core/logger/core.js';
import { isServerLikeOutput } from '../core/util.js';
import type { AstroSettings } from '../types/astro.js';
import type { AstroConfig } from '../types/public/config.js';
import type {
Expand Down Expand Up @@ -115,7 +114,7 @@ export function astroContentImportPlugin({
const code = `
export const id = ${JSON.stringify(id)};
export const collection = ${JSON.stringify(collection)};
export const data = ${stringifyEntryData(data, isServerLikeOutput(settings.config))};
export const data = ${stringifyEntryData(data, settings.buildOutput === 'server')};
export const _internal = {
type: 'data',
filePath: ${JSON.stringify(_internal.filePath)},
Expand All @@ -140,7 +139,7 @@ export const _internal = {
export const collection = ${JSON.stringify(collection)};
export const slug = ${JSON.stringify(slug)};
export const body = ${JSON.stringify(body)};
export const data = ${stringifyEntryData(data, isServerLikeOutput(settings.config))};
export const data = ${stringifyEntryData(data, settings.buildOutput === 'server')};
export const _internal = {
type: 'content',
filePath: ${JSON.stringify(_internal.filePath)},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type { Plugin } from 'vite';
import { encodeName } from '../core/build/util.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { appendForwardSlash, removeFileExtension } from '../core/path.js';
import { isServerLikeOutput } from '../core/util.js';
import { rootRelativePath } from '../core/viteUtils.js';
import type { AstroSettings } from '../types/astro.js';
import type { AstroPluginMetadata } from '../vite-plugin-astro/index.js';
Expand Down Expand Up @@ -53,7 +52,7 @@ export function astroContentVirtualModPlugin({
fs,
}: AstroContentVirtualModPluginParams): Plugin {
let IS_DEV = false;
const IS_SERVER = isServerLikeOutput(settings.config);
const IS_SERVER = settings.buildOutput === 'server';
let dataStoreFile: URL;
return {
name: 'astro-content-virtual-mod-plugin',
Expand Down
15 changes: 8 additions & 7 deletions packages/astro/src/core/build/common.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import npath from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { appendForwardSlash } from '../../core/path.js';
import type { AstroSettings } from '../../types/astro.js';
import type { AstroConfig } from '../../types/public/config.js';
import type { RouteData } from '../../types/public/internal.js';

const STATUS_CODE_PAGES = new Set(['/404', '/500']);
const FALLBACK_OUT_DIR_NAME = './.astro/';

function getOutRoot(astroConfig: AstroConfig): URL {
if (astroConfig.output === 'static') {
return new URL('./', astroConfig.outDir);
function getOutRoot(astroSettings: AstroSettings): URL {
if (astroSettings.buildOutput === 'static') {
return new URL('./', astroSettings.config.outDir);
} else {
return new URL('./', astroConfig.build.client);
return new URL('./', astroSettings.config.build.client);
}
}

export function getOutFolder(
astroConfig: AstroConfig,
astroSettings: AstroSettings,
pathname: string,
routeData: RouteData,
): URL {
const outRoot = getOutRoot(astroConfig);
const outRoot = getOutRoot(astroSettings);
const routeType = routeData.type;

// This is the root folder to write to.
Expand All @@ -30,7 +31,7 @@ export function getOutFolder(
case 'fallback':
case 'page':
case 'redirect':
switch (astroConfig.build.format) {
switch (astroSettings.config.build.format) {
case 'directory': {
if (STATUS_CODE_PAGES.has(pathname)) {
return new URL('.' + appendForwardSlash(npath.dirname(pathname)), outRoot);
Expand Down
10 changes: 5 additions & 5 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { callGetStaticPaths } from '../render/route-cache.js';
import { createRequest } from '../request.js';
import { matchRoute } from '../routing/match.js';
import { stringifyParams } from '../routing/params.js';
import { getOutputFilename, isServerLikeOutput } from '../util.js';
import { getOutputFilename } from '../util.js';
import { getOutFile, getOutFolder } from './common.js';
import { cssOrder, mergeInlineCss } from './internal.js';
import { BuildPipeline } from './pipeline.js';
Expand All @@ -49,12 +49,12 @@ import { getTimeStat, shouldAppendForwardSlash } from './util.js';

export async function generatePages(options: StaticBuildOptions, internals: BuildInternals) {
const generatePagesTimer = performance.now();
const ssr = isServerLikeOutput(options.settings.config);
const ssr = options.settings.buildOutput === 'server';
let manifest: SSRManifest;
if (ssr) {
manifest = await BuildPipeline.retrieveManifest(options, internals);
} else {
const baseDirectory = getOutputDirectory(options.settings.config);
const baseDirectory = getOutputDirectory(options.settings);
const renderersEntryUrl = new URL('renderers.mjs', baseDirectory);
const renderers = await import(renderersEntryUrl.toString());
let middleware: MiddlewareHandler = (_, next) => next();
Expand Down Expand Up @@ -138,7 +138,7 @@ export async function generatePages(options: StaticBuildOptions, internals: Buil
delete globalThis?.astroAsset?.addStaticImage;
}

await runHookBuildGenerated({ config, logger });
await runHookBuildGenerated({ settings: options.settings, logger });
}

const THRESHOLD_SLOW_RENDER_TIME_MS = 500;
Expand Down Expand Up @@ -466,7 +466,7 @@ async function generatePath(
body = Buffer.from(await response.arrayBuffer());
}

const outFolder = getOutFolder(config, pathname, route);
const outFolder = getOutFolder(pipeline.settings, pathname, route);
const outFile = getOutFile(config, outFolder, pathname, route);
route.distURL = outFile;

Expand Down
Loading

0 comments on commit 16fd30a

Please sign in to comment.