Unverified Commit c86a9b99 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Code refactoring for webpack-config (#40942)

Clean up some utils, global variables and RegExp usages from the places
I touched recently.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a 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 a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent da8d2991
...@@ -9,6 +9,7 @@ import path from 'path' ...@@ -9,6 +9,7 @@ import path from 'path'
import { promises as fs } from 'fs' import { promises as fs } from 'fs'
import { isValidElementType } from 'next/dist/compiled/react-is' import { isValidElementType } from 'next/dist/compiled/react-is'
import stripAnsi from 'next/dist/compiled/strip-ansi' import stripAnsi from 'next/dist/compiled/strip-ansi'
import browserslist from 'next/dist/compiled/browserslist'
import { import {
Redirect, Redirect,
Rewrite, Rewrite,
...@@ -22,6 +23,7 @@ import { ...@@ -22,6 +23,7 @@ import {
MIDDLEWARE_FILENAME, MIDDLEWARE_FILENAME,
SERVER_RUNTIME, SERVER_RUNTIME,
} from '../lib/constants' } from '../lib/constants'
import { MODERN_BROWSERSLIST_TARGET } from '../shared/lib/constants'
import prettyBytes from '../lib/pretty-bytes' import prettyBytes from '../lib/pretty-bytes'
import { getRouteRegex } from '../shared/lib/router/utils/route-regex' import { getRouteRegex } from '../shared/lib/router/utils/route-regex'
import { getRouteMatcher } from '../shared/lib/router/utils/route-matcher' import { getRouteMatcher } from '../shared/lib/router/utils/route-matcher'
...@@ -1725,3 +1727,32 @@ export class NestedMiddlewareError extends Error { ...@@ -1725,3 +1727,32 @@ export class NestedMiddlewareError extends Error {
) )
} }
} }
export function getSupportedBrowsers(
dir: string,
isDevelopment: boolean,
config: NextConfigComplete
): string[] | undefined {
let browsers: any
try {
const browsersListConfig = browserslist.loadConfig({
path: dir,
env: isDevelopment ? 'development' : 'production',
})
// Running `browserslist` resolves `extends` and other config features into a list of browsers
if (browsersListConfig && browsersListConfig.length > 0) {
browsers = browserslist(browsersListConfig)
}
} catch {}
// When user has browserslist use that target
if (browsers && browsers.length > 0) {
return browsers
}
// When user does not have browserslist use the default target
// When `experimental.legacyBrowsers: false` the modern default is used
return config.experimental.legacyBrowsers
? undefined
: MODERN_BROWSERSLIST_TARGET
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment