Merge branch 'main' into feat/vercel-adapter

This commit is contained in:
JuanM04 2022-03-30 21:50:41 -03:00
commit 20c3969a43
No known key found for this signature in database
GPG key ID: 0171B712E406271A
93 changed files with 1043 additions and 449 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Allow components to return a Response

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes non-GET API routes in dev with Node 14

View file

@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/deno': patch
---
Add a Deno adapter for SSR

View file

@ -134,6 +134,11 @@ jobs:
node-version: ${{ matrix.node_version }}
cache: 'pnpm'
- name: Use Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.19.3
- name: Download Build Artifacts
uses: actions/download-artifact@v3

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -1,9 +1,9 @@
import { defineConfig } from 'astro/config';
import svelte from '@astrojs/svelte';
import nodejs from '@astrojs/node';
import deno from '@astrojs/deno';
// https://astro.build/config
export default defineConfig({
adapter: nodejs(),
adapter: deno(),
integrations: [svelte()],
});

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -9,6 +9,8 @@ node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env

View file

@ -12,7 +12,7 @@
"build:ci": "turbo run build:ci --no-deps --scope=astro --scope=create-astro --scope=\"@astrojs/*\"",
"build:examples": "turbo run build --scope=\"@example/*\"",
"dev": "turbo run dev --no-deps --no-cache --parallel --scope=astro --scope=create-astro --scope=\"@astrojs/*\"",
"test": "pnpm run test --filter astro --filter @astrojs/webapi",
"test": "pnpm run test --filter astro --filter @astrojs/webapi --filter @astrojs/deno",
"test:match": "cd packages/astro && pnpm run test:match",
"test:templates": "pnpm run test --filter create-astro",
"test:smoke": "node scripts/smoke/index.js",

View file

@ -107,6 +107,7 @@
"mime": "^3.0.0",
"ora": "^6.1.0",
"parse5": "^6.0.1",
"path-browserify": "^1.0.1",
"path-to-regexp": "^6.2.0",
"postcss": "^8.4.12",
"postcss-load-config": "^3.1.4",
@ -148,6 +149,7 @@
"@types/mime": "^2.0.3",
"@types/mocha": "^9.1.0",
"@types/parse5": "^6.0.3",
"@types/path-browserify": "^1.0.0",
"@types/prettier": "^2.4.4",
"@types/resolve": "^1.20.1",
"@types/rimraf": "^3.0.2",

View file

@ -4,6 +4,7 @@ import type * as vite from 'vite';
import { z } from 'zod';
import type { AstroConfigSchema } from '../core/config';
import type { AstroComponentFactory, Metadata } from '../runtime/server';
import type { ViteConfigWithSSR } from '../core/create-vite';
export type { SSRManifest } from '../core/app/types';
export interface AstroBuiltinProps {
@ -171,7 +172,6 @@ export interface AstroUserConfig {
integrations?: Array<AstroIntegration | AstroIntegration[]>;
/**
* @docs
* @name adapter
* @type {AstroIntegration}
* @default `undefined`
@ -679,8 +679,9 @@ export interface AstroIntegration {
'astro:config:done'?: (options: { config: AstroConfig; setAdapter: (adapter: AstroAdapter) => void }) => void | Promise<void>;
'astro:server:setup'?: (options: { config: Readonly<AstroConfig>; server: vite.ViteDevServer }) => void | Promise<void>;
'astro:server:start'?: (options: { config: Readonly<AstroConfig>; address: AddressInfo }) => void | Promise<void>;
'astro:server:done'?: (options: { config: Readonly<AstroConfig> }) => void | Promise<void>;
'astro:server:done'?: (options: {config: Readonly<AstroConfig>;}) => void | Promise<void>;
'astro:build:start'?: (options: { config: Readonly<AstroConfig>; buildConfig: BuildConfig }) => void | Promise<void>;
'astro:build:setup'?: (options: { config: Readonly<AstroConfig>; vite: ViteConfigWithSSR; target: 'client' | 'server' }) => void;
'astro:build:done'?: (options: { config: Readonly<AstroConfig>; pages: { pathname: string }[]; dir: URL; routes: RouteData[] }) => void | Promise<void>;
};
}

View file

@ -1,12 +1,12 @@
/* eslint-disable no-console */
import type { AstroConfig } from '../@types/astro';
import { enableVerboseLogging, LogOptions } from '../core/logger.js';
import { LogOptions } from '../core/logger/core.js';
import * as colors from 'kleur/colors';
import yargs from 'yargs-parser';
import { z } from 'zod';
import { defaultLogDestination } from '../core/logger.js';
import { nodeLogDestination, enableVerboseLogging } from '../core/logger/node.js';
import build from '../core/build/index.js';
import add from '../core/add/index.js';
import devServer from '../core/dev/index.js';
@ -87,7 +87,7 @@ export async function cli(args: string[]) {
// logLevel
let logging: LogOptions = {
dest: defaultLogDestination,
dest: nodeLogDestination,
level: 'info',
};
if (flags.verbose) {

View file

@ -10,7 +10,7 @@ import preferredPM from 'preferred-pm';
import ora from 'ora';
import { resolveConfigURL } from '../config.js';
import { apply as applyPolyfill } from '../polyfill.js';
import { error, info, debug, LogOptions } from '../logger.js';
import { error, info, debug, LogOptions } from '../logger/core.js';
import { printHelp } from '../messages.js';
import * as msg from '../messages.js';
import * as CONSTS from './consts.js';

View file

@ -1,8 +1,9 @@
import type { ComponentInstance, EndpointHandler, ManifestData, RouteData } from '../../@types/astro';
import type { SSRManifest as Manifest, RouteInfo } from './types';
import type { LogOptions } from '../logger/core.js';
import mime from 'mime';
import { defaultLogOptions } from '../logger.js';
import { consoleLogDestination } from '../logger/console.js';
export { deserializeManifest } from './common.js';
import { matchRoute } from '../routing/match.js';
import { render } from '../render/core.js';
@ -18,7 +19,10 @@ export class App {
#routeDataToRouteInfo: Map<RouteData, RouteInfo>;
#routeCache: RouteCache;
#encoder = new TextEncoder();
#logging = defaultLogOptions;
#logging: LogOptions = {
dest: consoleLogDestination,
level: 'info',
};
constructor(manifest: Manifest) {
this.#manifest = manifest;
@ -26,7 +30,7 @@ export class App {
routes: manifest.routes.map((route) => route.routeData),
};
this.#routeDataToRouteInfo = new Map(manifest.routes.map((route) => [route.routeData, route]));
this.#routeCache = new RouteCache(defaultLogOptions);
this.#routeCache = new RouteCache(this.#logging);
}
match(request: Request): RouteData | undefined {
const url = new URL(request.url);
@ -105,7 +109,7 @@ export class App {
const url = new URL(request.url);
const handler = mod as unknown as EndpointHandler;
const result = await callEndpoint(handler, {
logging: defaultLogOptions,
logging: this.#logging,
origin: url.origin,
pathname: url.pathname,
request,

View file

@ -5,7 +5,7 @@ import type { OutputAsset, OutputChunk, RollupOutput } from 'rollup';
import { fileURLToPath } from 'url';
import type { AstroConfig, ComponentInstance, EndpointHandler, SSRLoadedRenderer } from '../../@types/astro';
import type { BuildInternals } from '../../core/build/internal.js';
import { debug, info } from '../../core/logger.js';
import { debug, info } from '../logger/core.js';
import { appendForwardSlash, prependForwardSlash } from '../../core/path.js';
import type { RenderOptions } from '../../core/render/core';
import { BEFORE_HYDRATION_SCRIPT_ID } from '../../vite-plugin-scripts/index.js';

View file

@ -1,5 +1,5 @@
import type { AstroConfig, BuildConfig, ManifestData } from '../../@types/astro';
import type { LogOptions } from '../logger';
import type { LogOptions } from '../logger/core';
import fs from 'fs';
import * as colors from 'kleur/colors';
@ -7,7 +7,8 @@ import { apply as applyPolyfill } from '../polyfill.js';
import { performance } from 'perf_hooks';
import * as vite from 'vite';
import { createVite, ViteConfigWithSSR } from '../create-vite.js';
import { debug, defaultLogOptions, info, levels, timerMessage, warn, warnIfUsingExperimentalSSR } from '../logger.js';
import { debug, info, levels, timerMessage, warn, warnIfUsingExperimentalSSR } from '../logger/core.js';
import { nodeLogOptions } from '../logger/node.js';
import { createRouteManifest } from '../routing/index.js';
import { generateSitemap } from '../render/sitemap.js';
import { collectPagesData } from './page-data.js';
@ -25,7 +26,7 @@ export interface BuildOptions {
}
/** `astro build` */
export default async function build(config: AstroConfig, options: BuildOptions = { logging: defaultLogOptions }): Promise<void> {
export default async function build(config: AstroConfig, options: BuildOptions = { logging: nodeLogOptions }): Promise<void> {
applyPolyfill();
const builder = new AstroBuilder(config, options);
await builder.run();

View file

@ -1,12 +1,12 @@
import type { AstroConfig, ComponentInstance, ManifestData, RouteData } from '../../@types/astro';
import type { AllPagesData } from './types';
import type { LogOptions } from '../logger';
import { info } from '../logger.js';
import type { LogOptions } from '../logger/core';
import { info } from '../logger/core.js';
import type { ViteDevServer } from 'vite';
import { fileURLToPath } from 'url';
import * as colors from 'kleur/colors';
import { debug } from '../logger.js';
import { debug } from '../logger/core.js';
import { preload as ssrPreload } from '../render/dev/index.js';
import { generateRssFunction } from '../render/rss.js';
import { callGetStaticPaths, RouteCache, RouteCacheEntry } from '../render/route-cache.js';

View file

@ -2,7 +2,7 @@ import type { ViteDevServer } from 'vite';
import type { RollupOutput, RollupWatcher } from 'rollup';
import type { AstroConfig, RouteType } from '../../@types/astro';
import type { AllPagesData, PageBuildData } from './types';
import type { LogOptions } from '../logger';
import type { LogOptions } from '../logger/core';
import type { ViteConfigWithSSR } from '../create-vite.js';
import { fileURLToPath } from 'url';

View file

@ -9,7 +9,7 @@ import npath from 'path';
import { fileURLToPath } from 'url';
import * as vite from 'vite';
import { createBuildInternals } from '../../core/build/internal.js';
import { info } from '../../core/logger.js';
import { info } from '../logger/core.js';
import { appendForwardSlash, prependForwardSlash } from '../../core/path.js';
import { emptyDir, removeDir } from '../../core/util.js';
import { rollupPluginAstroBuildCSS } from '../../vite-plugin-build-css/index.js';
@ -20,6 +20,7 @@ import { vitePluginPages } from './vite-plugin-pages.js';
import { generatePages } from './generate.js';
import { trackPageData } from './internal.js';
import { isBuildingToSSR } from '../util.js';
import { runHookBuildSetup } from '../../integrations/index.js';
import { getTimeStat } from './util.js';
export async function staticBuild(opts: StaticBuildOptions) {
@ -112,8 +113,8 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
const { astroConfig, viteConfig } = opts;
const ssr = astroConfig.buildOptions.experimentalSsr;
const out = ssr ? opts.buildConfig.server : astroConfig.dist;
// TODO: use vite.mergeConfig() here?
return await vite.build({
const viteBuildConfig = {
logLevel: 'error',
mode: 'production',
css: viteConfig.css,
@ -122,7 +123,6 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
emptyOutDir: false,
manifest: false,
outDir: fileURLToPath(out),
ssr: true,
rollupOptions: {
input: [],
output: {
@ -132,6 +132,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
assetFileNames: 'assets/asset.[hash][extname]',
},
},
ssr: true,
// must match an esbuild target
target: 'esnext',
// improve build performance
@ -156,7 +157,13 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
server: viteConfig.server,
base: astroConfig.buildOptions.site ? new URL(astroConfig.buildOptions.site).pathname : '/',
ssr: viteConfig.ssr,
} as ViteConfigWithSSR);
resolve: viteConfig.resolve,
} as ViteConfigWithSSR;
await runHookBuildSetup({ config: astroConfig, vite: viteBuildConfig, target: 'server' });
// TODO: use vite.mergeConfig() here?
return await vite.build(viteBuildConfig);
}
async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals, input: Set<string>) {
@ -173,7 +180,7 @@ async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals,
const out = isBuildingToSSR(astroConfig) ? opts.buildConfig.client : astroConfig.dist;
const buildResult = await vite.build({
const viteBuildConfig = {
logLevel: 'info',
mode: 'production',
css: viteConfig.css,
@ -207,7 +214,11 @@ async function clientBuild(opts: StaticBuildOptions, internals: BuildInternals,
envPrefix: 'PUBLIC_',
server: viteConfig.server,
base: appendForwardSlash(astroConfig.buildOptions.site ? new URL(astroConfig.buildOptions.site).pathname : '/'),
});
} as ViteConfigWithSSR;
await runHookBuildSetup({ config: astroConfig, vite: viteBuildConfig, target: 'client' });
const buildResult = await vite.build(viteBuildConfig);
info(opts.logging, null, dim(`Completed in ${getTimeStat(timer, performance.now())}.\n`));
return buildResult;
}

View file

@ -1,5 +1,5 @@
import type { AstroConfig } from '../@types/astro';
import type { LogOptions } from './logger';
import type { LogOptions } from './logger/core';
import { builtinModules } from 'module';
import { fileURLToPath } from 'url';
@ -81,6 +81,13 @@ export async function createVite(commandConfig: ViteConfigWithSSR, { astroConfig
css: {
postcss: astroConfig.styleOptions.postcss || {},
},
resolve: {
alias: {
// This is needed for Deno compatibility, as the non-browser version
// of this module depends on Node `crypto`
randombytes: 'randombytes/browser',
},
},
// Note: SSR API is in beta (https://vitejs.dev/guide/ssr.html)
ssr: {
external: [...ALWAYS_EXTERNAL],

View file

@ -4,7 +4,8 @@ import * as vite from 'vite';
import type { AstroConfig } from '../../@types/astro';
import { runHookConfigDone, runHookConfigSetup, runHookServerDone, runHookServerSetup, runHookServerStart } from '../../integrations/index.js';
import { createVite } from '../create-vite.js';
import { defaultLogOptions, info, LogOptions, warn, warnIfUsingExperimentalSSR } from '../logger.js';
import { info, LogOptions, warn, warnIfUsingExperimentalSSR } from '../logger/core.js';
import { nodeLogOptions } from '../logger/node.js';
import * as msg from '../messages.js';
import { apply as applyPolyfill } from '../polyfill.js';
import { getResolvedHostForVite } from '../util.js';
@ -19,7 +20,7 @@ export interface DevServer {
}
/** `astro dev` */
export default async function dev(config: AstroConfig, options: DevOptions = { logging: defaultLogOptions }): Promise<DevServer> {
export default async function dev(config: AstroConfig, options: DevOptions = { logging: nodeLogOptions }): Promise<DevServer> {
const devStart = performance.now();
applyPolyfill();
config = await runHookConfigSetup({ config, command: 'dev' });

View file

@ -1,229 +0,0 @@
import type { AstroConfig } from '../@types/astro';
import { bold, cyan, dim, red, yellow, reset } from 'kleur/colors';
import { performance } from 'perf_hooks';
import { Writable } from 'stream';
import stringWidth from 'string-width';
import * as readline from 'readline';
import debugPackage from 'debug';
import { format as utilFormat } from 'util';
import { isBuildingToSSR } from './util.js';
type ConsoleStream = Writable & {
fd: 1 | 2;
};
function getLoggerLocale(): string {
const defaultLocale = 'en-US';
if (process.env.LANG) {
const extractedLocale = process.env.LANG.split('.')[0].replace(/_/g, '-');
// Check if language code is atleast two characters long (ie. en, es).
// NOTE: if "c" locale is encountered, the default locale will be returned.
if (extractedLocale.length < 2) return defaultLocale;
else return extractedLocale.substring(0, 5);
} else return defaultLocale;
}
const dt = new Intl.DateTimeFormat(getLoggerLocale(), {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
});
let lastMessage: string;
let lastMessageCount = 1;
export const defaultLogDestination = new Writable({
objectMode: true,
write(event: LogMessage, _, callback) {
let dest: ConsoleStream = process.stderr;
if (levels[event.level] < levels['error']) {
dest = process.stdout;
}
function getPrefix() {
let prefix = '';
let type = event.type;
if (type) {
// hide timestamp when type is undefined
prefix += dim(dt.format(new Date()) + ' ');
if (event.level === 'info') {
type = bold(cyan(`[${type}]`));
} else if (event.level === 'warn') {
type = bold(yellow(`[${type}]`));
} else if (event.level === 'error') {
type = bold(red(`[${type}]`));
}
prefix += `${type} `;
}
return reset(prefix);
}
let message = utilFormat(...event.args);
// For repeat messages, only update the message counter
if (message === lastMessage) {
lastMessageCount++;
if (levels[event.level] < levels['error']) {
let lines = 1;
let len = stringWidth(`${getPrefix()}${message}`);
let cols = (dest as typeof process.stdout).columns;
if (len > cols) {
lines = Math.ceil(len / cols);
}
for (let i = 0; i < lines; i++) {
readline.clearLine(dest, 0);
readline.cursorTo(dest, 0);
readline.moveCursor(dest, 0, -1);
}
}
message = `${message} ${yellow(`(x${lastMessageCount})`)}`;
} else {
lastMessage = message;
lastMessageCount = 1;
}
dest.write(getPrefix());
dest.write(message);
dest.write('\n');
callback();
},
});
interface LogWritable<T> extends Writable {
write: (chunk: T) => boolean;
}
export type LoggerLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'; // same as Pino
export type LoggerEvent = 'info' | 'warn' | 'error';
export interface LogOptions {
dest?: LogWritable<LogMessage>;
level?: LoggerLevel;
}
export const defaultLogOptions: Required<LogOptions> = {
dest: defaultLogDestination,
level: 'info',
};
export interface LogMessage {
type: string | null;
level: LoggerLevel;
message: string;
args: Array<any>;
}
export const levels: Record<LoggerLevel, number> = {
debug: 20,
info: 30,
warn: 40,
error: 50,
silent: 90,
};
export function enableVerboseLogging() {
debugPackage.enable('*,-babel');
debug('cli', '--verbose flag enabled! Enabling: DEBUG="*,-babel"');
debug('cli', 'Tip: Set the DEBUG env variable directly for more control. Example: "DEBUG=astro:*,vite:* astro build".');
}
/** Full logging API */
export function log(opts: LogOptions = {}, level: LoggerLevel, type: string | null, ...args: Array<any>) {
const logLevel = opts.level ?? defaultLogOptions.level;
const dest = opts.dest ?? defaultLogOptions.dest;
const event: LogMessage = {
type,
level,
args,
message: '',
};
// test if this level is enabled or not
if (levels[logLevel] > levels[level]) {
return; // do nothing
}
dest.write(event);
}
const debuggers: Record<string, debugPackage.Debugger['log']> = {};
/**
* Emit a message only shown in debug mode.
* Astro (along with many of its dependencies) uses the `debug` package for debug logging.
* You can enable these logs with the `DEBUG=astro:*` environment variable.
* More info https://github.com/debug-js/debug#environment-variables
*/
export function debug(type: string, ...messages: Array<any>) {
const namespace = `astro:${type}`;
debuggers[namespace] = debuggers[namespace] || debugPackage(namespace);
return debuggers[namespace](...messages);
}
/** Emit a user-facing message. Useful for UI and other console messages. */
export function info(opts: LogOptions, type: string | null, ...messages: Array<any>) {
return log(opts, 'info', type, ...messages);
}
/** Emit a warning message. Useful for high-priority messages that aren't necessarily errors. */
export function warn(opts: LogOptions, type: string | null, ...messages: Array<any>) {
return log(opts, 'warn', type, ...messages);
}
/** Emit a error message, Useful when Astro can't recover from some error. */
export function error(opts: LogOptions, type: string | null, ...messages: Array<any>) {
return log(opts, 'error', type, ...messages);
}
type LogFn = typeof info | typeof warn | typeof error;
export function table(opts: LogOptions, columns: number[]) {
return function logTable(logFn: LogFn, ...input: Array<any>) {
const messages = columns.map((len, i) => padStr(input[i].toString(), len));
logFn(opts, null, ...messages);
};
}
// A default logger for when too lazy to pass LogOptions around.
export const logger = {
info: info.bind(null, defaultLogOptions),
warn: warn.bind(null, defaultLogOptions),
error: error.bind(null, defaultLogOptions),
};
function padStr(str: string, len: number) {
const strLen = stringWidth(str);
if (strLen > len) {
return str.substring(0, len - 3) + '...';
}
const spaces = Array.from({ length: len - strLen }, () => ' ').join('');
return str + spaces;
}
export let defaultLogLevel: LoggerLevel;
if (process.argv.includes('--verbose')) {
defaultLogLevel = 'debug';
} else if (process.argv.includes('--silent')) {
defaultLogLevel = 'silent';
} else {
defaultLogLevel = 'info';
}
/** Print out a timer message for debug() */
export function timerMessage(message: string, startTime: number = performance.now()) {
let timeDiff = performance.now() - startTime;
let timeDisplay = timeDiff < 750 ? `${Math.round(timeDiff)}ms` : `${(timeDiff / 1000).toFixed(1)}s`;
return `${message} ${dim(timeDisplay)}`;
}
/**
* A warning that SSR is experimental. Remove when we can.
*/
export function warnIfUsingExperimentalSSR(opts: LogOptions, config: AstroConfig) {
if (isBuildingToSSR(config)) {
warn(
opts,
'warning',
bold(`Warning:`),
` SSR support is still experimental and subject to API changes. If using in production pin your dependencies to prevent accidental breakage.`
);
}
}

View file

@ -0,0 +1,51 @@
import type { AstroConfig } from '../../@types/astro';
import type { LogMessage } from './core.js';
import { bold, cyan, dim, red, yellow, reset } from 'kleur/colors';
import stringWidth from 'string-width';
import { format as utilFormat } from 'util';
import { levels, dateTimeFormat } from './core.js';
let lastMessage: string;
let lastMessageCount = 1;
export const consoleLogDestination = {
write(event: LogMessage) {
// eslint-disable-next-line no-console
let dest = console.error;
if (levels[event.level] < levels['error']) {
// eslint-disable-next-line no-console
dest = console.log;
}
function getPrefix() {
let prefix = '';
let type = event.type;
if (type) {
// hide timestamp when type is undefined
prefix += dim(dateTimeFormat.format(new Date()) + ' ');
if (event.level === 'info') {
type = bold(cyan(`[${type}]`));
} else if (event.level === 'warn') {
type = bold(yellow(`[${type}]`));
} else if (event.level === 'error') {
type = bold(red(`[${type}]`));
}
prefix += `${type} `;
}
return reset(prefix);
}
let message = utilFormat(...event.args);
// For repeat messages, only update the message counter
if (message === lastMessage) {
lastMessageCount++;
message = `${message} ${yellow(`(x${lastMessageCount})`)}`;
} else {
lastMessage = message;
lastMessageCount = 1;
}
const outMessage = getPrefix() + message;
dest(outMessage);
return true;
},
};

View file

@ -0,0 +1,139 @@
import type { AstroConfig } from '../../@types/astro';
import { bold, dim } from 'kleur/colors';
import stringWidth from 'string-width';
interface LogWritable<T> {
write: (chunk: T) => boolean;
}
export type LoggerLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'; // same as Pino
export type LoggerEvent = 'info' | 'warn' | 'error';
export interface LogOptions {
dest: LogWritable<LogMessage>;
level: LoggerLevel;
}
function getLoggerLocale(): string {
const defaultLocale = 'en-US';
if (process.env.LANG) {
const extractedLocale = process.env.LANG.split('.')[0].replace(/_/g, '-');
// Check if language code is atleast two characters long (ie. en, es).
// NOTE: if "c" locale is encountered, the default locale will be returned.
if (extractedLocale.length < 2) return defaultLocale;
else return extractedLocale.substring(0, 5);
} else return defaultLocale;
}
export const dateTimeFormat = new Intl.DateTimeFormat(getLoggerLocale(), {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
});
export interface LogMessage {
type: string | null;
level: LoggerLevel;
message: string;
args: Array<any>;
}
export const levels: Record<LoggerLevel, number> = {
debug: 20,
info: 30,
warn: 40,
error: 50,
silent: 90,
};
/** Full logging API */
export function log(opts: LogOptions, level: LoggerLevel, type: string | null, ...args: Array<any>) {
const logLevel = opts.level;
const dest = opts.dest;
const event: LogMessage = {
type,
level,
args,
message: '',
};
// test if this level is enabled or not
if (levels[logLevel] > levels[level]) {
return; // do nothing
}
dest.write(event);
}
/** Emit a user-facing message. Useful for UI and other console messages. */
export function info(opts: LogOptions, type: string | null, ...messages: Array<any>) {
return log(opts, 'info', type, ...messages);
}
/** Emit a warning message. Useful for high-priority messages that aren't necessarily errors. */
export function warn(opts: LogOptions, type: string | null, ...messages: Array<any>) {
return log(opts, 'warn', type, ...messages);
}
/** Emit a error message, Useful when Astro can't recover from some error. */
export function error(opts: LogOptions, type: string | null, ...messages: Array<any>) {
return log(opts, 'error', type, ...messages);
}
type LogFn = typeof info | typeof warn | typeof error;
export function table(opts: LogOptions, columns: number[]) {
return function logTable(logFn: LogFn, ...input: Array<any>) {
const messages = columns.map((len, i) => padStr(input[i].toString(), len));
logFn(opts, null, ...messages);
};
}
export function debug(...args: any[]) {
if ('_astroGlobalDebug' in globalThis) {
(globalThis as any)._astroGlobalDebug(...args);
}
}
function padStr(str: string, len: number) {
const strLen = stringWidth(str);
if (strLen > len) {
return str.substring(0, len - 3) + '...';
}
const spaces = Array.from({ length: len - strLen }, () => ' ').join('');
return str + spaces;
}
export let defaultLogLevel: LoggerLevel;
if (typeof process !== 'undefined') {
if (process.argv.includes('--verbose')) {
defaultLogLevel = 'debug';
} else if (process.argv.includes('--silent')) {
defaultLogLevel = 'silent';
} else {
defaultLogLevel = 'info';
}
} else {
defaultLogLevel = 'info';
}
/** Print out a timer message for debug() */
export function timerMessage(message: string, startTime: number = Date.now()) {
let timeDiff = Date.now() - startTime;
let timeDisplay = timeDiff < 750 ? `${Math.round(timeDiff)}ms` : `${(timeDiff / 1000).toFixed(1)}s`;
return `${message} ${dim(timeDisplay)}`;
}
/**
* A warning that SSR is experimental. Remove when we can.
*/
export function warnIfUsingExperimentalSSR(opts: LogOptions, config: AstroConfig) {
if (config._ctx.adapter?.serverEntrypoint) {
warn(
opts,
'warning',
bold(`Warning:`),
` SSR support is still experimental and subject to API changes. If using in production pin your dependencies to prevent accidental breakage.`
);
}
}

View file

@ -0,0 +1,131 @@
import { bold, cyan, dim, red, yellow, reset } from 'kleur/colors';
import stringWidth from 'string-width';
import debugPackage from 'debug';
import { format as utilFormat } from 'util';
import * as readline from 'readline';
import { Writable } from 'stream';
import { info, warn, error, dateTimeFormat } from './core.js';
type ConsoleStream = Writable & {
fd: 1 | 2;
};
let lastMessage: string;
let lastMessageCount = 1;
export const nodeLogDestination = new Writable({
objectMode: true,
write(event: LogMessage, _, callback) {
let dest: ConsoleStream = process.stderr;
if (levels[event.level] < levels['error']) {
dest = process.stdout;
}
function getPrefix() {
let prefix = '';
let type = event.type;
if (type) {
// hide timestamp when type is undefined
prefix += dim(dateTimeFormat.format(new Date()) + ' ');
if (event.level === 'info') {
type = bold(cyan(`[${type}]`));
} else if (event.level === 'warn') {
type = bold(yellow(`[${type}]`));
} else if (event.level === 'error') {
type = bold(red(`[${type}]`));
}
prefix += `${type} `;
}
return reset(prefix);
}
let message = utilFormat(...event.args);
// For repeat messages, only update the message counter
if (message === lastMessage) {
lastMessageCount++;
if (levels[event.level] < levels['error']) {
let lines = 1;
let len = stringWidth(`${getPrefix()}${message}`);
let cols = (dest as unknown as typeof process.stdout).columns;
if (len > cols) {
lines = Math.ceil(len / cols);
}
for (let i = 0; i < lines; i++) {
readline.clearLine(dest, 0);
readline.cursorTo(dest, 0);
readline.moveCursor(dest, 0, -1);
}
}
message = `${message} ${yellow(`(x${lastMessageCount})`)}`;
} else {
lastMessage = message;
lastMessageCount = 1;
}
dest.write(getPrefix());
dest.write(message);
dest.write('\n');
callback();
},
});
interface LogWritable<T> {
write: (chunk: T) => boolean;
}
export type LoggerLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'; // same as Pino
export type LoggerEvent = 'info' | 'warn' | 'error';
export interface LogOptions {
dest?: LogWritable<LogMessage>;
level?: LoggerLevel;
}
export const nodeLogOptions: Required<LogOptions> = {
dest: nodeLogDestination,
level: 'info',
};
export interface LogMessage {
type: string | null;
level: LoggerLevel;
message: string;
args: Array<any>;
}
export const levels: Record<LoggerLevel, number> = {
debug: 20,
info: 30,
warn: 40,
error: 50,
silent: 90,
};
const debuggers: Record<string, debugPackage.Debugger['log']> = {};
/**
* Emit a message only shown in debug mode.
* Astro (along with many of its dependencies) uses the `debug` package for debug logging.
* You can enable these logs with the `DEBUG=astro:*` environment variable.
* More info https://github.com/debug-js/debug#environment-variables
*/
export function debug(type: string, ...messages: Array<any>) {
const namespace = `astro:${type}`;
debuggers[namespace] = debuggers[namespace] || debugPackage(namespace);
return debuggers[namespace](...messages);
}
// This is gross, but necessary since we are depending on globals.
(globalThis as any)._astroGlobalDebug = debug;
// A default logger for when too lazy to pass LogOptions around.
export const logger = {
info: info.bind(null, nodeLogOptions),
warn: warn.bind(null, nodeLogOptions),
error: error.bind(null, nodeLogOptions),
};
export function enableVerboseLogging() {
//debugPackage.enable('*,-babel');
debug('cli', '--verbose flag enabled! Enabling: DEBUG="*,-babel"');
debug('cli', 'Tip: Set the DEBUG env variable directly for more control. Example: "DEBUG=astro:*,vite:* astro build".');
}

View file

@ -1,12 +1,12 @@
import type { AstroConfig } from '../../@types/astro';
import type { LogOptions } from '../logger';
import type { LogOptions } from '../logger/core';
import type { AddressInfo } from 'net';
import http from 'http';
import sirv from 'sirv';
import { performance } from 'perf_hooks';
import { fileURLToPath } from 'url';
import * as msg from '../messages.js';
import { error, info } from '../logger.js';
import { error, info } from '../logger/core.js';
import { subpathNotUsedTemplate, notFoundTemplate } from '../../template/4xx.js';
import { getResolvedHostForHttpServer } from './util.js';

View file

@ -1,8 +1,8 @@
import type { ComponentInstance, EndpointHandler, MarkdownRenderOptions, Params, Props, SSRLoadedRenderer, RouteData, SSRElement } from '../../@types/astro';
import type { LogOptions } from '../logger.js';
import type { LogOptions } from '../logger/core.js';
import { renderHead, renderPage } from '../../runtime/server/index.js';
import { getParams } from '../routing/index.js';
import { getParams } from '../routing/params.js';
import { createResult } from './result.js';
import { findPathItemByKey, RouteCache, callGetStaticPaths } from './route-cache.js';

View file

@ -2,16 +2,7 @@ import type * as vite from 'vite';
import path from 'path';
import { unwrapId, 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);
import { STYLE_EXTENSIONS } from '../util.js';
/** Given a filePath URL, crawl Vites module graph to find all style imports. */
export function getStylesForURL(filePath: URL, viteServer: vite.ViteDevServer): Set<string> {

View file

@ -1,7 +1,7 @@
import { fileURLToPath } from 'url';
import type * as vite from 'vite';
import type { AstroConfig, AstroRenderer, ComponentInstance, RouteData, RuntimeMode, SSRElement, SSRLoadedRenderer } from '../../../@types/astro';
import { LogOptions } from '../../logger.js';
import { LogOptions } from '../../logger/core.js';
import { render as coreRender } from '../core.js';
import { prependForwardSlash } from '../../../core/path.js';
import { RouteCache } from '../route-cache.js';

File diff suppressed because one or more lines are too long

View file

@ -1,9 +1,8 @@
import { bold } from 'kleur/colors';
import type { AstroGlobal, AstroGlobalPartial, MarkdownParser, MarkdownRenderOptions, Params, SSRElement, SSRLoadedRenderer, SSRResult } from '../../@types/astro';
import { renderSlot } from '../../runtime/server/index.js';
import { LogOptions, warn } from '../logger.js';
import { isCSSRequest } from './dev/css.js';
import { canonicalURL as utilCanonicalURL } from '../util.js';
import { LogOptions, warn } from '../logger/core.js';
import { createCanonicalURL, isCSSRequest } from './util.js';
import { isScriptRequest } from './script.js';
function onlyAvailableInSSR(name: string) {
@ -71,10 +70,10 @@ class Slots {
}
export function createResult(args: CreateResultArgs): SSRResult {
const { legacyBuild, markdownRender, origin, params, pathname, renderers, request, resolve, site } = args;
const { legacyBuild, markdownRender, params, pathname, renderers, request, resolve, site } = args;
const url = new URL(request.url);
const canonicalURL = utilCanonicalURL('.' + pathname, site ?? url.origin);
const canonicalURL = createCanonicalURL('.' + pathname, site ?? url.origin);
// Create the result object that will be passed into the render function.
// This object starts here as an empty shell (not yet the result) but then

View file

@ -1,8 +1,8 @@
import type { ComponentInstance, GetStaticPathsItem, GetStaticPathsResult, GetStaticPathsResultKeyed, Params, RouteData, RSS } from '../../@types/astro';
import { LogOptions, warn, debug } from '../logger.js';
import { LogOptions, warn, debug } from '../logger/core.js';
import { generatePaginateFunction } from './paginate.js';
import { validateGetStaticPathsModule, validateGetStaticPathsResult } from '../routing/index.js';
import { validateGetStaticPathsModule, validateGetStaticPathsResult } from '../routing/validation.js';
type RSSFn = (...args: any[]) => any;

View file

@ -1,7 +1,8 @@
import type { RSSFunction, RSS, RSSResult, RouteData } from '../../@types/astro';
import { XMLValidator } from 'fast-xml-parser';
import { canonicalURL, isValidURL, PRETTY_FEED_V3 } from '../util.js';
import { PRETTY_FEED_V3 } from './pretty-feed.js';
import { createCanonicalURL, isValidURL } from './util.js';
/** Validates getStaticPaths.rss */
export function validateRSS(args: GenerateRSSArgs): void {
@ -38,7 +39,7 @@ export function generateRSS(args: GenerateRSSArgs): string {
// title, description, customData
xml += `<title><![CDATA[${rssData.title}]]></title>`;
xml += `<description><![CDATA[${rssData.description}]]></description>`;
xml += `<link>${canonicalURL(site).href}</link>`;
xml += `<link>${createCanonicalURL(site).href}</link>`;
if (typeof rssData.customData === 'string') xml += rssData.customData;
// items
for (const result of rssData.items) {
@ -49,7 +50,7 @@ export function generateRSS(args: GenerateRSSArgs): string {
if (!result.link) throw new Error(`[${srcFile}] rss.items required "link" property is missing. got: "${JSON.stringify(result)}"`);
xml += `<title><![CDATA[${result.title}]]></title>`;
// If the item's link is already a valid URL, don't mess with it.
const itemLink = isValidURL(result.link) ? result.link : canonicalURL(result.link, site).href;
const itemLink = isValidURL(result.link) ? result.link : createCanonicalURL(result.link, site).href;
xml += `<link>${itemLink}</link>`;
xml += `<guid>${itemLink}</guid>`;
if (result.description) xml += `<description><![CDATA[${result.description}]]></description>`;

View file

@ -1,6 +1,6 @@
import type { SSRElement } from '../../@types/astro';
import npath from 'path';
import npath from 'path-browserify';
import { appendForwardSlash } from '../../core/path.js';
function getRootPath(site?: string): string {

View file

@ -0,0 +1,29 @@
import npath from 'path-browserify';
/** Normalize URL to its canonical form */
export function createCanonicalURL(url: string, base?: string): URL {
let pathname = url.replace(/\/index.html$/, ''); // index.html is not canonical
pathname = pathname.replace(/\/1\/?$/, ''); // neither is a trailing /1/ (impl. detail of collections)
if (!npath.extname(pathname)) pathname = pathname.replace(/(\/+)?$/, '/'); // add trailing slash if theres no extension
pathname = pathname.replace(/\/+/g, '/'); // remove duplicate slashes (URL() wont)
return new URL(pathname, base);
}
/** Check if a URL is already valid */
export function isValidURL(url: string): boolean {
try {
new URL(url);
return true;
} catch (e) {}
return false;
}
// 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);

View file

@ -1,6 +1,6 @@
import type { IncomingHttpHeaders } from 'http';
import type { LogOptions } from './logger';
import { warn } from './logger.js';
import type { LogOptions } from './logger/core';
import { warn } from './logger/core.js';
type HeaderType = Headers | Record<string, any> | IncomingHttpHeaders;
type RequestBody = ArrayBuffer | Blob | ReadableStream | URLSearchParams | FormData;

View file

@ -1,12 +1,12 @@
import type { AstroConfig, ManifestData, RouteData } from '../../../@types/astro';
import type { LogOptions } from '../../logger';
import type { LogOptions } from '../../logger/core';
import fs from 'fs';
import path from 'path';
import { compile } from 'path-to-regexp';
import slash from 'slash';
import { fileURLToPath } from 'url';
import { warn } from '../../logger.js';
import { warn } from '../../logger/core.js';
interface Part {
content: string;

View file

@ -1,6 +1,6 @@
import type { ComponentInstance, GetStaticPathsResult } from '../../@types/astro';
import type { LogOptions } from '../logger';
import { warn } from '../logger.js';
import type { LogOptions } from '../logger/core';
import { warn } from '../logger/core.js';
interface ValidationOptions {
ssr: boolean;

File diff suppressed because one or more lines are too long

View file

@ -3,6 +3,7 @@ import type { ViteDevServer } from 'vite';
import { AstroConfig, AstroRenderer, BuildConfig, RouteData } from '../@types/astro.js';
import { mergeConfig } from '../core/config.js';
import ssgAdapter from '../adapter-ssg/index.js';
import type { ViteConfigWithSSR } from '../core/create-vite.js';
export async function runHookConfigSetup({ config: _config, command }: { config: AstroConfig; command: 'dev' | 'build' }): Promise<AstroConfig> {
if (_config.adapter) {
@ -91,6 +92,14 @@ export async function runHookBuildStart({ config, buildConfig }: { config: Astro
}
}
export async function runHookBuildSetup({ config, vite, target }: { config: AstroConfig; vite: ViteConfigWithSSR; target: 'server' | 'client' }) {
for (const integration of config.integrations) {
if (integration.hooks['astro:build:setup']) {
await integration.hooks['astro:build:setup']({ vite, target });
}
}
}
export async function runHookBuildDone({ config, pages, routes }: { config: AstroConfig; pages: string[]; routes: RouteData[] }) {
for (const integration of config.integrations) {
if (integration.hooks['astro:build:done']) {

View file

@ -412,7 +412,8 @@ async function replaceHeadInjection(result: SSRResult, html: string): Promise<st
export async function renderToString(result: SSRResult, componentFactory: AstroComponentFactory, props: any, children: any): Promise<string> {
const Component = await componentFactory(result, props, children);
if (!isAstroComponent(Component)) {
throw new Error('Cannot return a Response from a nested component.');
const response: Response = Component;
throw response;
}
let template = await renderAstroComponent(Component);
@ -425,20 +426,31 @@ export async function renderPage(
props: any,
children: any
): Promise<{ type: 'html'; html: string } | { type: 'response'; response: Response }> {
const response = await componentFactory(result, props, children);
try {
const response = await componentFactory(result, props, children);
if (isAstroComponent(response)) {
let template = await renderAstroComponent(response);
const html = await replaceHeadInjection(result, template);
return {
type: 'html',
html,
};
} else {
return {
type: 'response',
response,
};
if (isAstroComponent(response)) {
let template = await renderAstroComponent(response);
const html = await replaceHeadInjection(result, template);
return {
type: 'html',
html,
};
} else {
return {
type: 'response',
response,
};
}
} catch (err) {
if (err instanceof Response) {
return {
type: 'response',
response: err,
};
} else {
throw err;
}
}
}

View file

@ -2,7 +2,7 @@ import type * as vite from 'vite';
import type http from 'http';
import type { AstroConfig, ManifestData } from '../@types/astro';
import type { RenderResponse, SSROptions } from '../core/render/dev/index';
import { debug, info, warn, error, LogOptions } from '../core/logger.js';
import { debug, info, warn, error, LogOptions } from '../core/logger/core.js';
import { getParamsAndProps, GetParamsAndPropsError } from '../core/render/core.js';
import { createRouteManifest, matchRoute } from '../core/routing/index.js';
import stripAnsi from 'strip-ansi';
@ -15,6 +15,7 @@ import serverErrorTemplate from '../template/5xx.js';
import { RouteCache } from '../core/render/route-cache.js';
import { fixViteErrorMessage } from '../core/errors.js';
import { createRequest } from '../core/request.js';
import { Readable } from 'stream';
interface AstroPluginOptions {
config: AstroConfig;
@ -44,12 +45,17 @@ async function writeWebResponse(res: http.ServerResponse, webResponse: Response)
const { status, headers, body } = webResponse;
res.writeHead(status, Object.fromEntries(headers.entries()));
if (body) {
const reader = body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (value) {
res.write(value);
if (body instanceof Readable) {
body.pipe(res);
return;
} else {
const reader = body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
if (value) {
res.write(value);
}
}
}
}
@ -134,7 +140,7 @@ async function handleRequest(
await new Promise((resolve) => {
req.setEncoding('utf-8');
req.on('data', (bts) => bytes.push(bts));
req.on('close', resolve);
req.on('end', resolve);
});
body = new TextEncoder().encode(bytes.join('')).buffer;
}

View file

@ -1,9 +1,9 @@
import type { AstroConfig } from '../@types/astro';
import type { LogOptions } from '../core/logger.js';
import type { LogOptions } from '../core/logger/core.js';
import type { ViteDevServer, ModuleNode, HmrContext } from 'vite';
import type { PluginContext as RollupPluginContext, ResolvedId } from 'rollup';
import { invalidateCompilation, isCached } from './compile.js';
import { info } from '../core/logger.js';
import { info } from '../core/logger/core.js';
import * as msg from '../core/messages.js';
interface TrackCSSDependenciesOptions {

View file

@ -1,7 +1,7 @@
import type * as vite from 'vite';
import type { PluginContext } from 'rollup';
import type { AstroConfig } from '../@types/astro';
import type { LogOptions } from '../core/logger.js';
import type { LogOptions } from '../core/logger/core.js';
import esbuild from 'esbuild';
import { fileURLToPath } from 'url';

View file

@ -1,6 +1,6 @@
import type * as vite from 'vite';
import { STYLE_EXTENSIONS } from '../core/render/dev/css.js';
import { STYLE_EXTENSIONS } from '../core/render/util.js';
export type TransformHook = (code: string, id: string, ssr?: boolean) => Promise<vite.TransformResult>;

View file

@ -4,9 +4,8 @@ import type { ModuleInfo, PluginContext } from 'rollup';
import * as path from 'path';
import esbuild from 'esbuild';
import { Plugin as VitePlugin } from 'vite';
import { isCSSRequest } from '../core/render/dev/css.js';
import { isCSSRequest } from '../core/render/util.js';
import { getPageDatasByChunk, getPageDataByViteID, hasPageDataByViteID } from '../core/build/internal.js';
import { resolvedVirtualModuleId as virtualPagesModuleId } from '../core/build/vite-plugin-pages.js';
const PLUGIN_NAME = '@astrojs/rollup-plugin-build-css';

View file

@ -8,7 +8,7 @@ import type { Plugin as VitePlugin, ViteDevServer } from 'vite';
import type { AstroConfig } from '../@types/astro';
import type { BuildInternals } from '../core/build/internal';
import type { AllPagesData } from '../core/build/types';
import type { LogOptions } from '../core/logger.js';
import type { LogOptions } from '../core/logger/core.js';
import { prependDotSlash } from '../core/path.js';
import { render as ssrRender } from '../core/render/dev/index.js';
import { RouteCache } from '../core/render/route-cache.js';

View file

@ -1,14 +1,14 @@
import type { TransformResult } from 'rollup';
import type { Plugin, ResolvedConfig } from 'vite';
import type { AstroConfig, AstroRenderer } from '../@types/astro';
import type { LogOptions } from '../core/logger.js';
import type { LogOptions } from '../core/logger/core.js';
import babel from '@babel/core';
import esbuild from 'esbuild';
import * as colors from 'kleur/colors';
import * as eslexer from 'es-module-lexer';
import path from 'path';
import { error } from '../core/logger.js';
import { error } from '../core/logger/core.js';
import { parseNpmName } from '../core/util.js';
const JSX_RENDERER_CACHE = new WeakMap<AstroConfig, Map<string, AstroRenderer>>();

View file

@ -0,0 +1,32 @@
import { expect } from 'chai';
import { load as cheerioLoad } from 'cheerio';
import { loadFixture } from './test-utils.js';
// Asset bundling
describe('Returning responses', () => {
let fixture;
/** @type {import('./test-utils').DevServer} */
let devServer;
before(async () => {
fixture = await loadFixture({
projectRoot: './fixtures/astro-response/',
});
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('Works from a page', async () => {
let response = await fixture.fetch('/not-found');
expect(response.status).to.equal(404);
});
it('Works from a component', async () => {
let response = await fixture.fetch('/not-found-component');
expect(response.status).to.equal(404);
});
});

View file

@ -0,0 +1,6 @@
---
return new Response(null, {
status: 404,
statusText: `Not found`
});
---

View file

@ -0,0 +1,4 @@
---
import NotFound from '../components/not-found.astro';
---
<NotFound />

View file

@ -0,0 +1,6 @@
---
return new Response(null, {
status: 404,
statusText: `Not found`
});
---

View file

@ -8,3 +8,13 @@ export function get() {
])
};
}
export async function post(params, request) {
const body = await request.text();
return new Response(body === `some data` ? `ok` : `not ok`, {
status: 200,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
}

View file

@ -36,4 +36,25 @@ describe('API routes in SSR', () => {
const body = await response.json();
expect(body.length).to.equal(3);
});
describe('Dev', () => {
let devServer;
before(async () => {
devServer = await fixture.startDevServer();
});
after(async () => {
await devServer.stop();
});
it('Can POST to API routes', async () => {
const response = await fixture.fetch('/food.json', {
method: 'POST',
body: `some data`,
});
expect(response.status).to.equal(200);
const text = await response.text();
expect(text).to.equal(`ok`);
});
});
});

View file

@ -6,7 +6,7 @@ import { resolveConfig, loadConfig } from '../dist/core/config.js';
import dev from '../dist/core/dev/index.js';
import build from '../dist/core/build/index.js';
import preview from '../dist/core/preview/index.js';
import { loadApp } from '../dist/core/app/node.js';
import { nodeLogDestination } from '../dist/core/logger/node.js';
import os from 'os';
import stripAnsi from 'strip-ansi';
@ -71,17 +71,23 @@ export async function loadFixture(inlineConfig) {
let config = await loadConfig({ cwd: fileURLToPath(cwd) });
config = merge(config, { ...inlineConfig, projectRoot: cwd });
/** @type {import('../src/core/logger/core').LogOptions} */
const logging = {
dest: nodeLogDestination,
level: 'error',
};
return {
build: (opts = {}) => build(config, { mode: 'development', logging: 'error', ...opts }),
build: (opts = {}) => build(config, { mode: 'development', logging, ...opts }),
startDevServer: async (opts = {}) => {
const devResult = await dev(config, { logging: 'error', ...opts });
const devResult = await dev(config, { logging, ...opts });
config.devOptions.port = devResult.address.port; // update port
return devResult;
},
config,
fetch: (url, init) => fetch(`http://${'127.0.0.1'}:${config.devOptions.port}${url.replace(/^\/?/, '/')}`, init),
preview: async (opts = {}) => {
const previewServer = await preview(config, { logging: 'error', ...opts });
const previewServer = await preview(config, { logging, ...opts });
return previewServer;
},
readFile: (filePath) => fs.promises.readFile(new URL(filePath.replace(/^\//, ''), config.dist), 'utf8'),

View file

@ -0,0 +1,7 @@
# @astrojs/node
## 0.0.2-next.0
### Patch Changes
- [#2873](https://github.com/withastro/astro/pull/2873) [`e4025d1f`](https://github.com/withastro/astro/commit/e4025d1f530310d6ab951109f4f53878a307471a) Thanks [@matthewp](https://github.com/matthewp)! - Improves the build by building to a single file for rendering

View file

@ -0,0 +1,30 @@
{
"name": "@astrojs/deno",
"description": "Deploy your site to a Deno server",
"version": "0.0.2-next.0",
"type": "module",
"types": "./dist/index.d.ts",
"author": "withastro",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/withastro/astro.git",
"directory": "packages/integrations/deno"
},
"bugs": "https://github.com/withastro/astro/issues",
"homepage": "https://astro.build",
"exports": {
".": "./dist/index.js",
"./server.js": "./dist/server.js",
"./package.json": "./package.json"
},
"scripts": {
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
"dev": "astro-scripts dev \"src/**/*.ts\"",
"test": "deno test --allow-run --allow-read --allow-net ./test/"
},
"devDependencies": {
"astro": "workspace:*",
"astro-scripts": "workspace:*"
}
}

View file

@ -0,0 +1,66 @@
# @astrojs/deno
A server-side rendering adapter for use with Deno targets. Write your code in Astro/Node and deploy to Deno servers.
In your astro.config.mjs use:
```js
import { defineConfig } from 'astro/config';
import deno from '@astrojs/deno';
export default defineConfig({
adapter: deno()
});
```
After performing a build there will be a `dist/server/entry.mjs` module. You can start a server simply by importing this module:
```js
import './dist/entry.mjs';
```
## API
### Adapter options
This adapter automatically starts a server when it is imported. You can configure this through options:
```js
import { defineConfig } from 'astro/config';
import deno from '@astrojs/deno';
export default defineConfig({
adapter: deno({
start: false
})
});
```
If disabling start you need to write your own web server and use `handle` to render requests:
```ts
import { serve } from "https://deno.land/std@0.132.0/http/server.ts";
import { handle } from './dist/entry.mjs';
serve((req: Request) => {
// Check the request, maybe do static file handling here.
return handle(req);
});
```
----
You an also pass in a port/hostname to use:
```js
import { defineConfig } from 'astro/config';
import deno from '@astrojs/deno';
export default defineConfig({
adapter: deno({
port: 8081,
hostname: 'myhost'
})
});
```

View file

@ -0,0 +1,33 @@
import type { AstroAdapter, AstroIntegration } from 'astro';
interface Options {
port?: number;
hostname?: string;
}
export function getAdapter(args?: Options): AstroAdapter {
return {
name: '@astrojs/deno',
serverEntrypoint: '@astrojs/deno/server.js',
args: args ?? {},
exports: ['stop', 'handle'],
};
}
export default function createIntegration(args?: Options): AstroIntegration {
return {
name: '@astrojs/deno',
hooks: {
'astro:config:done': ({ setAdapter }) => {
setAdapter(getAdapter(args));
},
'astro:build:setup': ({ vite, target }) => {
if (target === 'server') {
vite.ssr = {
noExternal: true,
};
}
},
},
};
}

View file

@ -0,0 +1,51 @@
import './shim.js';
import type { SSRManifest } from 'astro';
import { App } from 'astro/app';
// @ts-ignore
import { Server } from 'https://deno.land/std@0.132.0/http/server.ts';
interface Options {
port?: number;
hostname?: string;
start?: boolean;
}
let _server: Server | undefined = undefined;
let _startPromise: Promise<void> | undefined = undefined;
export function start(manifest: SSRManifest, options: Options) {
if (options.start === false) {
return;
}
const app = new App(manifest);
const handler = async (request: Request) => {
const response = await app.render(request);
return response;
};
_server = new Server({
port: options.port ?? 8085,
hostname: options.hostname ?? '0.0.0.0',
handler,
//onError: options.onError,
});
_startPromise = _server.listenAndServe();
}
export function createExports(manifest: SSRManifest) {
const app = new App(manifest);
return {
async stop() {
if (_server) {
_server.close();
}
await Promise.resolve(_startPromise);
},
async handle(request: Request) {
return app.render(request);
},
};
}

View file

@ -0,0 +1,4 @@
(globalThis as any).process = {
argv: [],
env: {},
};

View file

@ -0,0 +1,14 @@
import { runBuildAndStartApp } from './helpers.js';
import { assertEquals, assert } from './deps.js';
Deno.test({
name: 'Basics',
async fn() {
await runBuildAndStartApp('./fixtures/basics/', async () => {
const resp = await fetch('http://127.0.0.1:8085/');
assertEquals(resp.status, 200);
const html = await resp.text();
assert(html);
});
},
});

View file

@ -0,0 +1,2 @@
export * from 'https://deno.land/std@0.110.0/path/mod.ts';
export * from 'https://deno.land/std@0.132.0/testing/asserts.ts';

View file

@ -0,0 +1,6 @@
import { defineConfig } from 'astro/config';
import deno from '@astrojs/deno';
export default defineConfig({
adapter: deno()
})

View file

@ -0,0 +1,9 @@
{
"name": "@test/deno-astro-basic",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/deno": "workspace:*"
}
}

View file

@ -0,0 +1,11 @@
---
---
<html>
<head>
<title>Basic App on Deno</title>
</head>
<body>
<h1>Basic App on Deno</h1>
</body>
</html>

View file

@ -0,0 +1,20 @@
import { fromFileUrl } from './deps.js';
const dir = new URL('./', import.meta.url);
export async function runBuild(fixturePath) {
let proc = Deno.run({
cmd: ['node', '../../../../../astro/astro.js', 'build', '--silent'],
cwd: fromFileUrl(new URL(fixturePath, dir)),
});
await proc.status();
return async () => await proc.close();
}
export async function runBuildAndStartApp(fixturePath, cb) {
const url = new URL(fixturePath, dir);
const close = await runBuild(fixturePath);
const mod = await import(new URL('./dist/entry.mjs', url));
await cb();
await mod.stop();
await close();
}

View file

@ -0,0 +1,10 @@
{
"extends": "../../../tsconfig.base.json",
"include": ["src"],
"compilerOptions": {
"allowJs": true,
"module": "ES2020",
"outDir": "./dist",
"target": "ES2020"
}
}

View file

@ -1,6 +1,6 @@
# @astrojs/node
An experimental static-side rendering adapter for use with Node.js servers.
An experimental server-side rendering adapter for use with Node.js servers.
In your astro.config.mjs use:

View file

@ -467,6 +467,7 @@ importers:
'@types/mime': ^2.0.3
'@types/mocha': ^9.1.0
'@types/parse5': ^6.0.3
'@types/path-browserify': ^1.0.0
'@types/prettier': ^2.4.4
'@types/resolve': ^1.20.1
'@types/rimraf': ^3.0.2
@ -499,6 +500,7 @@ importers:
mocha: ^9.2.2
ora: ^6.1.0
parse5: ^6.0.1
path-browserify: ^1.0.1
path-to-regexp: ^6.2.0
postcss: ^8.4.12
postcss-load-config: ^3.1.4
@ -560,6 +562,7 @@ importers:
mime: 3.0.0
ora: 6.1.0
parse5: 6.0.1
path-browserify: 1.0.1
path-to-regexp: 6.2.0
postcss: 8.4.12
postcss-load-config: 3.1.4_postcss@8.4.12
@ -600,6 +603,7 @@ importers:
'@types/mime': 2.0.3
'@types/mocha': 9.1.0
'@types/parse5': 6.0.3
'@types/path-browserify': 1.0.0
'@types/prettier': 2.4.4
'@types/resolve': 1.20.1
'@types/rimraf': 3.0.2
@ -1190,6 +1194,22 @@ importers:
astro-scripts: link:../../scripts
uvu: 0.5.3
packages/integrations/deno:
specifiers:
astro: workspace:*
astro-scripts: workspace:*
devDependencies:
astro: link:../../astro
astro-scripts: link:../../../scripts
packages/integrations/deno/test/fixtures/basics:
specifiers:
'@astrojs/deno': workspace:*
astro: workspace:*
dependencies:
'@astrojs/deno': link:../../..
astro: link:../../../../../astro
packages/integrations/lit:
specifiers:
'@lit-labs/ssr': ^2.0.4
@ -3980,6 +4000,10 @@ packages:
/@types/parse5/6.0.3:
resolution: {integrity: sha512-SuT16Q1K51EAVPz1K29DJ/sXjhSQ0zjvsypYJ6tlwVsRV9jwW5Adq2ch8Dq8kDBCkYnELS7N7VNCSB5nC56t/g==}
/@types/path-browserify/1.0.0:
resolution: {integrity: sha512-XMCcyhSvxcch8b7rZAtFAaierBYdeHXVvg2iYnxOV0MCQHmPuRRmGZPFDRzPayxcGiiSL1Te9UIO+f3cuj0tfw==}
dev: true
/@types/prettier/2.4.4:
resolution: {integrity: sha512-ReVR2rLTV1kvtlWFyuot+d1pkpG2Fw/XKE3PDAdj57rbM97ttSp9JZ2UsP+2EHTylra9cUf6JA7tGwW1INzUrA==}
dev: true
@ -8472,6 +8496,10 @@ packages:
tslib: 2.3.1
dev: false
/path-browserify/1.0.1:
resolution: {integrity: sha512-b7uo2UCUOYZcnF/3ID0lulOJi/bafxa1xPe7ZPsammBSpjSWQkjNxlt635YGS2MiR9GjvuXCtz2emr3jbsz98g==}
dev: false
/path-exists/4.0.0:
resolution: {integrity: sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==}
engines: {node: '>=8'}

View file

@ -1,4 +1,5 @@
Date,Commits (24hr),Issues (24hr),Issues:BUG (24hr),Issues:RFC (24hr),Issues:DOC (24hr),PRs (24hr),Open PRs,Open Issues,Bugs: Needs Triage,Bugs: Accepted,RFC: In Progress,RFC: Accepted,Date (ISO)
"Wednesday, March 30, 2022",9,2,2,0,0,10,10,90,43,41,0,0,"2022-03-30T12:02:39.303Z"
"Tuesday, March 29, 2022",19,8,8,0,0,9,5,88,41,41,0,0,"2022-03-29T12:06:39.897Z"
"Monday, March 28, 2022",1,7,7,0,0,2,8,83,36,41,0,0,"2022-03-28T12:02:00.954Z"
"Sunday, March 27, 2022",1,2,2,0,0,2,6,77,29,41,0,0,"2022-03-27T12:01:52.463Z"

1 Date Commits (24hr) Issues (24hr) Issues:BUG (24hr) Issues:RFC (24hr) Issues:DOC (24hr) PRs (24hr) Open PRs Open Issues Bugs: Needs Triage Bugs: Accepted RFC: In Progress RFC: Accepted Date (ISO)
2 Wednesday, March 30, 2022 9 2 2 0 0 10 10 90 43 41 0 0 2022-03-30T12:02:39.303Z
3 Tuesday, March 29, 2022 19 8 8 0 0 9 5 88 41 41 0 0 2022-03-29T12:06:39.897Z
4 Monday, March 28, 2022 1 7 7 0 0 2 8 83 36 41 0 0 2022-03-28T12:02:00.954Z
5 Sunday, March 27, 2022 1 2 2 0 0 2 6 77 29 41 0 0 2022-03-27T12:01:52.463Z