Skip to content

Commit a12c228

Browse files
shudingnatew
authored andcommitted
Refactor base server to remove native dependencies (vercel#33499)
Part of vercel#31506, this PR removes `loadEnvConfig` and `chalk` from the base server while keeping the same behavior for the node server. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
1 parent 01aaaed commit a12c228

File tree

16 files changed

+97
-50
lines changed

16 files changed

+97
-50
lines changed

packages/next/build/entries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { stringify } from 'querystring'
44
import { API_ROUTE, DOT_NEXT_ALIAS, PAGES_DIR_ALIAS } from '../lib/constants'
55
import { MIDDLEWARE_ROUTE } from '../lib/constants'
66
import { __ApiPreviewProps } from '../server/api-utils'
7-
import { isTargetLikeServerless } from '../server/config'
7+
import { isTargetLikeServerless } from '../server/utils'
88
import { normalizePagePath } from '../server/normalize-page-path'
99
import { warn } from './output/log'
1010
import { MiddlewareLoaderOptions } from './webpack/loaders/next-middleware-loader'

packages/next/build/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ import {
5757
isDynamicRoute,
5858
} from '../shared/lib/router/utils'
5959
import { __ApiPreviewProps } from '../server/api-utils'
60-
import loadConfig, { isTargetLikeServerless } from '../server/config'
60+
import loadConfig from '../server/config'
61+
import { isTargetLikeServerless } from '../server/utils'
6162
import { BuildManifest } from '../server/get-page-files'
6263
import { normalizePagePath } from '../server/normalize-page-path'
6364
import { getPagePath } from '../server/require'

packages/next/build/output/log.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import chalk from 'next/dist/compiled/chalk'
1+
import chalk from '../../lib/chalk'
22

33
export const prefixes = {
44
wait: chalk.cyan('wait') + ' -',

packages/next/build/webpack/loaders/next-serverless-loader/utils.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { IncomingMessage, ServerResponse } from 'http'
1+
import type { IncomingMessage, ServerResponse } from 'http'
2+
import type { Rewrite } from '../../../../lib/load-custom-routes'
3+
import type { BuildManifest } from '../../../../server/get-page-files'
4+
import type { NextConfig } from '../../../../server/config'
5+
import type {
6+
GetServerSideProps,
7+
GetStaticPaths,
8+
GetStaticProps,
9+
} from '../../../../types'
10+
211
import { format as formatUrl, UrlWithParsedQuery, parse as parseUrl } from 'url'
312
import { parse as parseQs, ParsedUrlQuery } from 'querystring'
4-
import { Rewrite } from '../../../../lib/load-custom-routes'
513
import { normalizeLocalePath } from '../../../../shared/lib/i18n/normalize-locale-path'
614
import pathMatch from '../../../../shared/lib/router/utils/path-match'
715
import { getRouteRegex } from '../../../../shared/lib/router/utils/route-regex'
@@ -11,19 +19,12 @@ import {
1119
prepareDestination,
1220
} from '../../../../shared/lib/router/utils/prepare-destination'
1321
import { __ApiPreviewProps } from '../../../../server/api-utils'
14-
import { BuildManifest } from '../../../../server/get-page-files'
15-
import {
16-
GetServerSideProps,
17-
GetStaticPaths,
18-
GetStaticProps,
19-
} from '../../../../types'
2022
import { acceptLanguage } from '../../../../server/accept-header'
2123
import { detectLocaleCookie } from '../../../../shared/lib/i18n/detect-locale-cookie'
2224
import { detectDomainLocale } from '../../../../shared/lib/i18n/detect-domain-locale'
2325
import { denormalizePagePath } from '../../../../server/denormalize-page-path'
2426
import cookie from 'next/dist/compiled/cookie'
2527
import { TEMPORARY_REDIRECT_STATUS } from '../../../../shared/lib/constants'
26-
import { NextConfig } from '../../../../server/config'
2728
import { addRequestMeta } from '../../../../server/request-meta'
2829
import { BaseNextRequest } from '../../../../server/base-http'
2930

packages/next/export/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import {
2828
SERVERLESS_DIRECTORY,
2929
SERVER_DIRECTORY,
3030
} from '../shared/lib/constants'
31-
import loadConfig, { isTargetLikeServerless } from '../server/config'
31+
import loadConfig from '../server/config'
32+
import { isTargetLikeServerless } from '../server/utils'
3233
import { NextConfigComplete } from '../server/config-shared'
3334
import { eventCliSession } from '../telemetry/events'
3435
import { hasNextSupport } from '../telemetry/ci-info'

packages/next/lib/chalk.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let chalk: typeof import('next/dist/compiled/chalk')
2+
3+
if (typeof window === 'undefined') {
4+
chalk = require('next/dist/compiled/chalk')
5+
} else {
6+
chalk = require('./web/chalk').default
7+
}
8+
9+
export default chalk

packages/next/lib/load-custom-routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { NextConfig } from '../server/config'
22

3-
import chalk from 'next/dist/compiled/chalk'
3+
import chalk from './chalk'
44
import { parse as parseUrl } from 'url'
55
import * as pathToRegexp from 'next/dist/compiled/path-to-regexp'
66
import { escapeStringRegexp } from '../shared/lib/escape-regexp'

packages/next/lib/web/chalk.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// In the web runtime, we create an alternative object that just outputs the
2+
// message to the console without any styling. The same APIs are supported
3+
// for compatibility:
4+
// - chalk.red('error')
5+
// - chalk.bold.cyan('message')
6+
// - chalk.hex('#fff').underline('hello')
7+
const log = console.log
8+
const chalk: any = new Proxy(log, {
9+
get(_, prop: string) {
10+
if (
11+
['hex', 'rgb', 'ansi256', 'bgHex', 'bgRgb', 'bgAnsi256'].includes(prop)
12+
) {
13+
return () => chalk
14+
}
15+
return chalk
16+
},
17+
})
18+
19+
export default chalk

packages/next/server/api-utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { IncomingMessage, ServerResponse } from 'http'
1+
import type { IncomingMessage, ServerResponse } from 'http'
2+
23
import { parse } from 'next/dist/compiled/content-type'
34
import { CookieSerializeOptions } from 'next/dist/compiled/cookie'
4-
import getRawBody from 'next/dist/compiled/raw-body'
55
import { PageConfig, PreviewData } from 'next/types'
66
import { Stream } from 'stream'
77
import { isResSent, NextApiRequest, NextApiResponse } from '../shared/lib/utils'
@@ -157,6 +157,8 @@ export async function parseBody(
157157
let buffer
158158

159159
try {
160+
const getRawBody =
161+
require('next/dist/compiled/raw-body') as typeof import('next/dist/compiled/raw-body')
160162
buffer = await getRawBody(req, { encoding, limit })
161163
} catch (e) {
162164
if (isError(e) && e.type === 'entity.too.large') {

packages/next/server/base-http.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ServerResponse, IncomingMessage, IncomingHttpHeaders } from 'http'
22
import type { Writable, Readable } from 'stream'
3+
34
import { PERMANENT_REDIRECT_STATUS } from '../shared/lib/constants'
45
import {
56
getCookieParser,

0 commit comments

Comments
 (0)