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: add support for with only options argument #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ declare function browserslistToEsbuild(
options?: browserslist.Options
): string[]

declare function browserslistToEsbuild(
options?: browserslist.Options
): string[]

export default browserslistToEsbuild
11 changes: 10 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import browserslist from 'browserslist'

// convert the browserslist field in package.json to
// esbuild compatible array of browsers
export default function browserslistToEsbuild(browserslistConfig, options = {}) {
export default function browserslistToEsbuild(browserslistConfig, options) {
if (
options === undefined &&
typeof browserslistConfig === 'object' &&
!Array.isArray(browserslistConfig)
) {
options = browserslistConfig
browserslistConfig = undefined
}

if (!browserslistConfig) {
// the path from where the script is run
const path = process.cwd()
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ test('the options argument works', (t) => {
cwdStub.restore()
})

test('works with only options', (t) => {
const browserslistrcDir = path.resolve(__dirname, './fixtures/browserslistrc')

// makes process.cwd() use that folder
if (!t.context.cwd) t.context.cwd = sinon.stub(process, 'cwd')
const cwdStub = t.context.cwd.returns(browserslistrcDir)

t.deepEqual(browserslistToEsbuild({ env: 'ssr' }), ['node12.22'])

cwdStub.restore()
})

test('works with ios', (t) => {
const target = browserslistToEsbuild('ios >= 9')

Expand Down