[cli] Run middleware as a plugin instead of running directly (#6941)

* Move loadPlugins to a utils file to be shared w/ dev server.

* Update loadCliPlugins to also return startDevServer and runDevMiddleware based plugins.

* Move plugins back to dependencies.

These can't be bundled as it interferes with plugin resolution.

* Hook up middleware plugins to dev server.

* Pass output object to loadCliPlugins instead of a logging function.

* Allow more than one runDevMiddleware defining plugins.

* Bundle esbuild w/ middleware plugin.

* Keep esbuild as an external

* Update middleware's esbuild.

* set old space size

* Revert "set old space size"

This reverts commit b579194a862949a11769e9087f01c31f2e1f3b60.

* Use --max-old-space-size for CLI unit tests

* Increase memory

* Use `run.js` to set the memory

* Make NODE_OPTIONS optional

Co-authored-by: Leo Lamprecht <leo@vercel.com>
Co-authored-by: Andy <AndyBitz@users.noreply.github.com>
Co-authored-by: Andy Bitz <artzbitz@gmail.com>
This commit is contained in:
Gary Borton
2021-11-08 10:38:13 -08:00
committed by GitHub
parent bf4e77110f
commit d3e98cdb73
8 changed files with 201 additions and 150 deletions

View File

@@ -22,8 +22,6 @@ import deepEqual from 'fast-deep-equal';
import which from 'which';
import npa from 'npm-package-arg';
import { runDevMiddleware } from 'vercel-plugin-middleware';
import { getVercelIgnore, fileNameSymbol } from '@vercel/client';
import {
getTransformedRoutes,
@@ -91,6 +89,7 @@ import {
} from './types';
import { ProjectEnvVariable, ProjectSettings } from '../../types';
import exposeSystemEnvs from './expose-system-envs';
import { loadCliPlugins } from '../plugins';
const frontendRuntimeSet = new Set(
frameworkList.map(f => f.useRuntime?.use || '@vercel/static-build')
@@ -1351,6 +1350,23 @@ export default class DevServer {
return false;
};
runDevMiddleware = async (
req: http.IncomingMessage,
res: http.ServerResponse
) => {
const { devMiddlewarePlugins } = await loadCliPlugins(
this.cwd,
this.output
);
for (let plugin of devMiddlewarePlugins) {
const result = await plugin.plugin.runDevMiddleware(req, res, this.cwd);
if (result.finished) {
return result;
}
}
return { finished: false };
};
/**
* Serve project directory as a v2 deployment.
*/
@@ -1418,7 +1434,7 @@ export default class DevServer {
let prevUrl = req.url;
let prevHeaders: HttpHeadersConfig = {};
const middlewareResult = await runDevMiddleware(req, res, this.cwd);
const middlewareResult = await this.runDevMiddleware(req, res);
if (middlewareResult) {
if (middlewareResult.error) {