Skip to content

Commit 38d1e23

Browse files
authored
feat: allow all browserslist options via JS API (#1489)
1 parent a067f9a commit 38d1e23

File tree

4 files changed

+12
-24
lines changed

4 files changed

+12
-24
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,13 @@ Compile browserslist query to one regex.
150150
| Option | Type | Default | Description |
151151
|--------|------|---------|-------------|
152152
| browsers | `string \| string[]` | — | Manually provide a browserslist query (or an array of queries). Specifying this overrides the browserslist configuration specified in your project. |
153-
| env | `string` | — | When multiple browserslist [environments](https://github.com/ai/browserslist#environments) are specified, pick the config belonging to this environment. |
154153
| ignorePatch | `boolean` | `true` | Ignore differences in patch browser numbers. |
155154
| ignoreMinor | `boolean` | `false` | Ignore differences in minor browser versions. |
156155
| allowHigherVersions | `boolean` | `false` | For all the browsers in the browserslist query, return a match if the useragent version is equal to or higher than the one specified in browserslist. |
157156
| allowZeroSubversions | `boolean` | `false` | Ignore match of patch or patch and minor, if they are 0. |
158157
158+
Any of the [`browserslist` API options](https://github.com/browserslist/browserslist#js-api) may also be provided.
159+
159160
#### Regex info object
160161
161162
| Property | Type | Description |

src/browsers/browserslist.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,9 @@ export function parseBrowsersList(browsersList: string[]) {
4444
export function getBrowsersList(options: BrowserslistRequest = {}) {
4545
const {
4646
browsers,
47-
env,
48-
path
47+
...browserslistOptions
4948
} = options
50-
const browsersList = browserslist(browsers, {
51-
env,
52-
path
53-
})
49+
const browsersList = browserslist(browsers, browserslistOptions)
5450
const parsedBrowsers = parseBrowsersList(browsersList)
5551

5652
return parsedBrowsers

src/browsers/types.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type browserslist from 'browserslist'
2+
import type { Options } from 'browserslist'
13
import type {
24
Semver,
35
RangedSemver
@@ -8,10 +10,8 @@ export interface Browser {
810
version: Semver
911
}
1012

11-
export interface BrowserslistRequest {
12-
browsers?: string | string[]
13-
env?: string
14-
path?: string
13+
export interface BrowserslistRequest extends Options {
14+
browsers?: Parameters<typeof browserslist>[0]
1515
}
1616

1717
export type BrowsersVersions = Map<string, Semver[]>

src/useragentRegex/useragentRegex.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { SemverCompareOptions } from '../semver/index.js'
12
import { getRegexesForBrowsers } from '../useragent/index.js'
23
import {
34
getBrowsersList,
@@ -15,29 +16,19 @@ export const defaultOptions = {
1516
ignorePatch: true,
1617
allowZeroSubversions: false,
1718
allowHigherVersions: false
18-
}
19+
} as const satisfies Required<SemverCompareOptions>
1920

2021
/**
2122
* Get source regexes objects from browserslist query.
2223
* @param options - Browserslist and semver compare options.
2324
* @returns Source regexes objects.
2425
*/
2526
export function getPreUserAgentRegexes(options: UserAgentRegexOptions = {}) {
26-
const {
27-
browsers,
28-
env,
29-
path,
30-
...otherOptions
31-
} = options
3227
const finalOptions = {
3328
...defaultOptions,
34-
...otherOptions
29+
...options
3530
}
36-
const browsersList = getBrowsersList({
37-
browsers,
38-
env,
39-
path
40-
})
31+
const browsersList = getBrowsersList(finalOptions)
4132
const mergedBrowsers = mergeBrowserVersions(browsersList)
4233
const sourceRegexes = getRegexesForBrowsers(mergedBrowsers, finalOptions)
4334
const versionedRegexes = applyVersionsToRegexes(sourceRegexes, finalOptions)

0 commit comments

Comments
 (0)