[next] Use "esbuild" to build package (#10482)

### Before

```
$ time pnpm run build

> @vercel/next@4.0.6 build /Users/nrajlich/Code/vercel/vercel/packages/next
> node build.js

ncc: Version 0.24.0
ncc: Compiling file index.js
ncc: Using typescript@4.9.5 (local user-provided)
1506kB  dist/main/index.js
1506kB  [3345ms] - ncc 0.24.0

real    0m5.210s
user    0m9.083s
sys     0m0.506s

$ ls -l dist/
total 1700
-rw-r--r-- 1 nrajlich staff    2176 Sep 20 15:18 ___get-nextjs-edge-function.js
-rw-r--r-- 1 nrajlich staff    3283 Sep 20 15:18 create-serverless-config.js
drwxr-xr-x 6 nrajlich staff     192 Sep 20 15:18 edge-function-source/
-rw-r--r-- 1 nrajlich staff 1542314 Sep 20 15:18 index.js
-rw-r--r-- 1 nrajlich staff     728 Sep 20 15:18 legacy-launcher.js
-rw-r--r-- 1 nrajlich staff    6807 Sep 20 15:18 legacy-versions.js
-rw-r--r-- 1 nrajlich staff   66662 Sep 20 15:18 server-build.js
-rw-r--r-- 1 nrajlich staff    1583 Sep 20 15:18 server-launcher.js
-rw-r--r-- 1 nrajlich staff    5167 Sep 20 15:18 sourcemapped.js
-rw-r--r-- 1 nrajlich staff    1003 Sep 20 15:18 templated-launcher-shared.js
-rw-r--r-- 1 nrajlich staff     799 Sep 20 15:18 templated-launcher.js
-rw-r--r-- 1 nrajlich staff   83876 Sep 20 15:18 utils.js

$ pnpm pack && ls -lh vercel-next-4.0.6.tgz 
-rw-r--r-- 1 nrajlich staff 373K Sep 20 15:19 vercel-next-4.0.6.tgz
```

### After

```
$ time pnpm run build

> @vercel/next@4.0.6 build /Users/nrajlich/Code/vercel/vercel/packages/next
> node build.mjs

real    0m1.144s
user    0m0.550s
sys     0m0.171s

$ ls -l dist/
total 540
-rw-r--r-- 1 nrajlich staff   2176 Sep 20 15:15 ___get-nextjs-edge-function.js
-rw-r--r-- 1 nrajlich staff 528575 Sep 20 15:15 index.js
-rw-r--r-- 1 nrajlich staff   1680 Sep 20 15:15 legacy-launcher.js
-rw-r--r-- 1 nrajlich staff    901 Sep 20 15:15 server-launcher.js
-rw-r--r-- 1 nrajlich staff    532 Sep 20 15:15 templated-launcher-shared.js
-rw-r--r-- 1 nrajlich staff    316 Sep 20 15:15 templated-launcher.js

$ pnpm pack && ls -lh vercel-next-4.0.6.tgz 
-rw-r--r-- 1 nrajlich staff 104K Sep 20 15:15 vercel-next-4.0.6.tgz
```
This commit is contained in:
Nathan Rajlich
2023-09-20 17:01:03 -03:00
committed by GitHub
parent 9bb3067de2
commit d0d0520111
18 changed files with 115 additions and 97 deletions

View File

@@ -28,10 +28,10 @@ import { Sema } from 'async-sema';
import crc32 from 'buffer-crc32';
import fs, { lstat, stat } from 'fs-extra';
import path from 'path';
import resolveFrom from 'resolve-from';
import semver from 'semver';
import zlib from 'zlib';
import url from 'url';
import { createRequire } from 'module';
import escapeStringRegexp from 'escape-string-regexp';
import { htmlContentType } from '.';
import textTable from 'text-table';
@@ -39,14 +39,18 @@ import { getNextjsEdgeFunctionSource } from './edge-function-source/get-edge-fun
import type { LambdaOptionsWithFiles } from '@vercel/build-utils/dist/lambda';
import { stringifySourceMap } from './sourcemapped';
import type { RawSourceMap } from 'source-map';
import bytes from 'bytes';
import { prettyBytes } from './pretty-bytes';
import {
MIB,
KIB,
MAX_UNCOMPRESSED_LAMBDA_SIZE,
LAMBDA_RESERVED_COMPRESSED_SIZE,
LAMBDA_RESERVED_UNCOMPRESSED_SIZE,
} from './constants';
type stringMap = { [key: string]: string };
export const KIB = 1024;
export const MIB = 1024 * KIB;
export const prettyBytes = (n: number) => bytes(n, { unitSeparator: ' ' });
export const require_ = createRequire(__filename);
export const RSC_CONTENT_TYPE = 'x-component';
export const RSC_PREFETCH_SUFFIX = '.prefetch.rsc';
@@ -420,10 +424,10 @@ export async function getDynamicRoutes(
let getSortedRoutes: ((normalizedPages: string[]) => string[]) | undefined;
try {
// NOTE: `eval('require')` is necessary to avoid bad transpilation to `__webpack_require__`
({ getRouteRegex, getSortedRoutes } = eval('require')(
resolveFrom(entryPath, 'next-server/dist/lib/router/utils')
));
const resolved = require_.resolve('next-server/dist/lib/router/utils', {
paths: [entryPath],
});
({ getRouteRegex, getSortedRoutes } = require_(resolved));
if (typeof getRouteRegex !== 'function') {
getRouteRegex = undefined;
}
@@ -431,10 +435,11 @@ export async function getDynamicRoutes(
if (!getRouteRegex || !getSortedRoutes) {
try {
// NOTE: `eval('require')` is necessary to avoid bad transpilation to `__webpack_require__`
({ getRouteRegex, getSortedRoutes } = eval('require')(
resolveFrom(entryPath, 'next/dist/next-server/lib/router/utils')
));
const resolved = require_.resolve(
'next/dist/next-server/lib/router/utils',
{ paths: [entryPath] }
);
({ getRouteRegex, getSortedRoutes } = require_(resolved));
if (typeof getRouteRegex !== 'function') {
getRouteRegex = undefined;
}
@@ -1419,10 +1424,6 @@ export type LambdaGroup = {
pseudoLayerUncompressedBytes: number;
};
export const MAX_UNCOMPRESSED_LAMBDA_SIZE = 250 * MIB;
const LAMBDA_RESERVED_UNCOMPRESSED_SIZE = 2.5 * MIB;
const LAMBDA_RESERVED_COMPRESSED_SIZE = 250 * KIB;
export async function getPageLambdaGroups({
entryPath,
config,