Skip to content

Commit

Permalink
Add options argument, close #9
Browse files Browse the repository at this point in the history
  • Loading branch information
marcofugaro committed Jan 4, 2024
1 parent ea3eb7e commit cd90d9f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
declare const browserslistToEsbuild: (browserslistConfig?: readonly string[] | string) => string[]
import browserslist from 'browserslist'

declare function browserslistToEsbuild(
browserslistConfig?: string | readonly string[],
options?: browserslist.Options
): string[]

export default browserslistToEsbuild
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import browserslist from 'browserslist'

// convert the browserslist field in package.json to
// esbuild compatible array of browsers
export default function browserslistToEsbuild(browserslistConfig) {
export default function browserslistToEsbuild(browserslistConfig, options = {}) {
if (!browserslistConfig) {
// the path from where the script is run
const path = process.cwd()

// read config if none is passed
browserslistConfig = browserslist.loadConfig({ path })
browserslistConfig = browserslist.loadConfig({ path, ...options })
}

const SUPPORTED_ESBUILD_TARGETS = [
Expand All @@ -34,7 +34,7 @@ export default function browserslistToEsbuild(browserslistConfig) {
const separator = ' '

return (
browserslist(browserslistConfig)
browserslist(browserslistConfig, options)
// filter out the unsupported ones
.filter((b) => !UNSUPPORTED.some((u) => b.startsWith(u)))
// replaces safari TP with latest safari version
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/browserslistrc/.browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[production]
> 1%
not dead

[modern]
last 1 chrome version
last 1 firefox version

[ssr]
node 12
File renamed without changes.
40 changes: 38 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ test('works by passing browsers as string', (t) => {
})

test('works by loading package.json config', (t) => {
const packageJsonDir = path.resolve(__dirname, './fixtures')
const packageJsonDir = path.resolve(__dirname, './fixtures/packageJson')

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

process.env.NODE_ENV = 'production'
t.deepEqual(browserslistToEsbuild(), [
Expand All @@ -45,6 +46,41 @@ test('works by loading package.json config', (t) => {
process.env.NODE_ENV = 'development'
t.deepEqual(browserslistToEsbuild(), ['chrome120', 'firefox121', 'safari17.2'])

cwdStub.restore()
process.env.NODE_ENV = ''
})

test('works by loading .browserslist config', (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(), [
'chrome109',
'edge119',
'firefox119',
'ios16.6',
'safari16.6',
])

process.env.BROWSERSLIST_ENV = 'ssr'
t.deepEqual(browserslistToEsbuild(), ['node12.22'])

cwdStub.restore()
process.env.BROWSERSLIST_ENV = ''
})

test('the options argument works', (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(undefined, { env: 'ssr' }), ['node12.22'])

cwdStub.restore()
})

Expand Down

0 comments on commit cd90d9f

Please sign in to comment.