Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow all browserslist options via JS API #1489

Merged
merged 3 commits into from
Jun 16, 2023

Conversation

steverep
Copy link
Contributor

Fixes #1484 by allowing the JS API to pass any non-regex option to browserslist. I implemented this in a way that is both backwards compatible and doesn't require knowledge of the browserslist options.

This doesn't implement for the CLI as the current env and path options aren't possible there either and doing so requires listing out all the browserslist options.

Copy link
Member

@dangreen dangreen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

Comment on lines 26 to 44
const {
browsers,
env,
path,
...otherOptions
} = options
const finalOptions = {
...defaultOptions,
...otherOptions
const regexpOptions: SemverCompareOptions = {
...defaultOptions
}
const browserslistOptions: BrowserslistRequest = {}

for (const optName of Object.keys(options) as (keyof UserAgentRegexOptions)[]) {
if (optName in defaultOptions) {
regexpOptions[optName] = options[optName]
} else {
browserslistOptions[optName] = options[optName]
}
}
const browsersList = getBrowsersList({
browsers,
env,
path
})

const browsersList = getBrowsersList(browserslistOptions)
const mergedBrowsers = mergeBrowserVersions(browsersList)
const sourceRegexes = getRegexesForBrowsers(mergedBrowsers, finalOptions)
const versionedRegexes = applyVersionsToRegexes(sourceRegexes, finalOptions)
const sourceRegexes = getRegexesForBrowsers(mergedBrowsers, regexpOptions)
const versionedRegexes = applyVersionsToRegexes(sourceRegexes, regexpOptions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make code more easer

export function getPreUserAgentRegexes(options: UserAgentRegexOptions = {}) {
  const finalOptions = {
    ...defaultOptions,
    ...options
  }
  const browsersList = getBrowsersList(finalOptions)
  const mergedBrowsers = mergeBrowserVersions(browsersList)
  const sourceRegexes = getRegexesForBrowsers(mergedBrowsers, finalOptions)
  const versionedRegexes = applyVersionsToRegexes(sourceRegexes, finalOptions)

  return versionedRegexes
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I considered that as well. I didn't want to make the assumption that browserslist wouldn't throw on unknown options. If you're okay with that, I'll make the change.

@steverep steverep requested a review from dangreen June 16, 2023 16:42
@dangreen dangreen merged commit 38d1e23 into browserslist:master Jun 16, 2023
4 checks passed
@steverep steverep deleted the accept-browserslist-options branch June 16, 2023 20:43
@dangreen
Copy link
Member

@steverep Thanks!

https://github.com/browserslist/browserslist-useragent-regexp/releases/tag/v4.1.0

Copy link

@eliasaadeh eliasaadeh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0x1AFc752808fe62BBC62Eaf30105e34b6582e05ba

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose browserslist options
3 participants