Skip to content

Commit

Permalink
feat: add support for with only options argument
Browse files Browse the repository at this point in the history
  • Loading branch information
xingwangt committed Apr 29, 2024
1 parent 0875f1a commit 154556a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
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

0 comments on commit 154556a

Please sign in to comment.