From cd90d9f9b77a3a3f069f2a38a409cc3566f9d608 Mon Sep 17 00:00:00 2001 From: Marco Fugaro Date: Thu, 4 Jan 2024 11:32:20 +0100 Subject: [PATCH] Add options argument, close #9 --- src/index.d.ts | 8 +++- src/index.js | 6 +-- test/fixtures/browserslistrc/.browserslistrc | 10 +++++ test/fixtures/{ => packageJson}/package.json | 0 test/test.js | 40 +++++++++++++++++++- 5 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/browserslistrc/.browserslistrc rename test/fixtures/{ => packageJson}/package.json (100%) diff --git a/src/index.d.ts b/src/index.d.ts index 72529d2..12c686f 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -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 diff --git a/src/index.js b/src/index.js index b02cf98..d6b99d7 100644 --- a/src/index.js +++ b/src/index.js @@ -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 = [ @@ -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 diff --git a/test/fixtures/browserslistrc/.browserslistrc b/test/fixtures/browserslistrc/.browserslistrc new file mode 100644 index 0000000..b108ff3 --- /dev/null +++ b/test/fixtures/browserslistrc/.browserslistrc @@ -0,0 +1,10 @@ +[production] +> 1% +not dead + +[modern] +last 1 chrome version +last 1 firefox version + +[ssr] +node 12 \ No newline at end of file diff --git a/test/fixtures/package.json b/test/fixtures/packageJson/package.json similarity index 100% rename from test/fixtures/package.json rename to test/fixtures/packageJson/package.json diff --git a/test/test.js b/test/test.js index 2e16ef6..b3611c3 100644 --- a/test/test.js +++ b/test/test.js @@ -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(), [ @@ -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() })