[now dev] Add support for proxying to builder-specific dev servers (#4089)

This is an extension to the Runtime API, where a runtime can optionally define a `startDevServer()` function which is responsible for spawning a single-serve dev server for an individual HTTP request (the dev server is booted up upon receiving an HTTP request, and gets shut down by `now dev` after the HTTP request is completed). For runtimes that define this function, the `build()` function will never be executed, which avoids a lot of unnecessary processing for a dev environment.

Some things this accomplishes:

 * Retains the proper stack trace for errors.
 * Ensures that if a source code file is changed, and then an HTTP request is sent, it's guaranteed to be using the latest code (no file watching, or re-compilations).
 * Avoids creating a Lambda zip file (just to immediately unpack it for dev).
 * Avoids `@zeit/fun` completely, which loses some "correctness" (i.e. function is not frozen in between requests).
 * Backwards compatible with older Now CLIs - versions that don't know about `startDevServer()` will just invoke `build()`, and there's no version change required in the Runtime.
This commit is contained in:
Nathan Rajlich
2020-04-17 13:55:44 -07:00
committed by GitHub
parent a03b8689e9
commit 6fd1b50924
15 changed files with 274 additions and 134 deletions

View File

@@ -1,5 +1,6 @@
import { basename, extname, join } from 'path';
import { BuilderParams, BuildResult, ShouldServeParams } from './types';
import { FileFsRef, BuildOptions, ShouldServeOptions } from '@now/build-utils';
import { BuildResult } from './types';
export const version = 2;
@@ -7,7 +8,7 @@ export function build({
files,
entrypoint,
config,
}: BuilderParams): BuildResult {
}: BuildOptions): BuildResult {
let path = entrypoint;
const outputDir = config.zeroConfig ? config.outputDirectory : '';
const outputMatch = outputDir + '/';
@@ -16,7 +17,7 @@ export function build({
path = path.slice(outputMatch.length);
}
const output = {
[path]: files[entrypoint],
[path]: files[entrypoint] as FileFsRef,
};
const watch = [path];
@@ -28,7 +29,7 @@ export function shouldServe({
files,
requestPath,
config = {},
}: ShouldServeParams) {
}: ShouldServeOptions) {
let outputPrefix = '';
const outputDir = config.zeroConfig ? config.outputDirectory : '';
const outputMatch = outputDir + '/';