更新
This commit is contained in:
+9
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Marco Fugaro <marco.fugaro@gmail.com> (https://marcofuga.ro)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
# browserslist-to-esbuild
|
||||
|
||||
> Use [browserslist](https://github.com/browserslist/browserslist) with [esbuild](https://esbuild.github.io/).
|
||||
|
||||
Allows you to use use browserslist and pass the correct browsers to esbuild's [target](https://esbuild.github.io/api/#target) option.
|
||||
|
||||
## Install
|
||||
|
||||
You have to install the `browserslist` package as well in your project:
|
||||
|
||||
```
|
||||
npm install --save-dev browserslist browserslist-to-esbuild
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```
|
||||
yarn add --dev browserslist browserslist-to-esbuild
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
You can call `browserslistToEsbuild()` directly in your `esbuild.mjs` script, it will look for your browserslist config in either `package.json` or the `.browserslistrc`.
|
||||
|
||||
It will return an esbuild-compatible array of browsers.
|
||||
|
||||
```js
|
||||
import { build } from 'esbuild'
|
||||
import browserslistToEsbuild from 'browserslist-to-esbuild'
|
||||
|
||||
await build({
|
||||
entryPoints: ['input.js'],
|
||||
outfile: 'output.js',
|
||||
bundle: true,
|
||||
target: browserslistToEsbuild(), // --> ["chrome79", "edge92", "firefox91", "safari13.1"]
|
||||
})
|
||||
```
|
||||
|
||||
Otherwise, you can pass yourself a browserslist array or string to the function.
|
||||
|
||||
```js
|
||||
browserslistToEsbuild(['>0.2%', 'not dead', 'not op_mini all'])
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### browserslistToEsbuild(browserslistConfig?, options?)
|
||||
|
||||
#### browserslistConfig
|
||||
|
||||
Type: `array | string | undefined`
|
||||
|
||||
An array of string of browsers [compatible with browserslist](https://github.com/browserslist/browserslist#full-list). If none is passed, a browserslist config is searched in the script running directory.
|
||||
|
||||
#### options
|
||||
|
||||
Type: `object | undefined`
|
||||
|
||||
An object containing the options that will be forwarded to browserslist. You can check out the [browserslist options documentation](https://github.com/browserslist/browserslist?tab=readme-ov-file#js-api) to see all the options available.
|
||||
|
||||
## CLI
|
||||
|
||||
You can also use this package on the cli to test out the command in your project.
|
||||
If no argument is passed, the browserslist config is searched in the script running directory.
|
||||
|
||||
Here is some example usage:
|
||||
|
||||
```bash
|
||||
$ npx browserslist-to-esbuild
|
||||
chrome109 edge118 firefox115 ios15.6 opera102 safari15.6
|
||||
|
||||
$ npx browserslist-to-esbuild '>0.2%, not dead'
|
||||
chrome103 edge87 firefox115 ios12.2 opera102 safari14.1
|
||||
|
||||
$ npx browserslist-to-esbuild '>0.2%' 'not dead'
|
||||
chrome103 edge87 firefox115 ios12.2 opera102 safari14.1
|
||||
```
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env node
|
||||
import meow from 'meow'
|
||||
import browserslistToEsbuild from '../src/index.js'
|
||||
|
||||
const cli = meow(
|
||||
`
|
||||
Usage
|
||||
$ npx browserslist-to-esbuild [browsers]
|
||||
|
||||
Options
|
||||
[browsers] Optional browsers string, if not specified defaults to
|
||||
the ones specified in the package.json.
|
||||
|
||||
Examples
|
||||
$ npx browserslist-to-esbuild
|
||||
chrome109 edge118 firefox115 ios15.6 opera102 safari15.6
|
||||
|
||||
$ npx browserslist-to-esbuild '>0.2%, not dead'
|
||||
chrome103 edge87 firefox115 ios12.2 opera102 safari14.1
|
||||
|
||||
$ npx browserslist-to-esbuild '>0.2%' 'not dead'
|
||||
chrome103 edge87 firefox115 ios12.2 opera102 safari14.1
|
||||
`,
|
||||
{
|
||||
importMeta: import.meta,
|
||||
}
|
||||
)
|
||||
|
||||
const targets = cli.input.length > 0 ? browserslistToEsbuild(cli.input) : browserslistToEsbuild()
|
||||
|
||||
console.log(...targets)
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"name": "browserslist-to-esbuild",
|
||||
"version": "2.1.1",
|
||||
"description": "Get esbuild-compatible targets from a browserlist config",
|
||||
"license": "MIT",
|
||||
"repository": "marcofugaro/browserslist-to-esbuild",
|
||||
"author": {
|
||||
"name": "Marco Fugaro",
|
||||
"email": "marco.fugaro@gmail.com",
|
||||
"url": "https://marcofuga.ro"
|
||||
},
|
||||
"keywords": [
|
||||
"browserslist",
|
||||
"browserlist",
|
||||
"esbuild",
|
||||
"browsers"
|
||||
],
|
||||
"type": "module",
|
||||
"exports": {
|
||||
"types": "./src/index.d.ts",
|
||||
"default": "./src/index.js"
|
||||
},
|
||||
"types": "./src/index.d.ts",
|
||||
"bin": {
|
||||
"browserslist-to-esbuild": "./cli/index.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "ava"
|
||||
},
|
||||
"files": [
|
||||
"src/",
|
||||
"cli/"
|
||||
],
|
||||
"peerDependencies": {
|
||||
"browserslist": "*"
|
||||
},
|
||||
"dependencies": {
|
||||
"meow": "^13.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ava": "^6.0.1",
|
||||
"browserslist": "^4.22.2",
|
||||
"sinon": "^17.0.1"
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import browserslist from 'browserslist'
|
||||
|
||||
declare function browserslistToEsbuild(
|
||||
browserslistConfig?: string | readonly string[],
|
||||
options?: browserslist.Options
|
||||
): string[]
|
||||
|
||||
export default browserslistToEsbuild
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
import browserslist from 'browserslist'
|
||||
|
||||
// convert the browserslist field in package.json to
|
||||
// esbuild compatible array of browsers
|
||||
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, ...options })
|
||||
}
|
||||
|
||||
const SUPPORTED_ESBUILD_TARGETS = [
|
||||
'es',
|
||||
'chrome',
|
||||
'edge',
|
||||
'firefox',
|
||||
'ios',
|
||||
'node',
|
||||
'safari',
|
||||
'opera',
|
||||
'ie',
|
||||
]
|
||||
|
||||
// https://github.com/eBay/browserslist-config/issues/16#issuecomment-863870093
|
||||
const UNSUPPORTED = ['android 4']
|
||||
|
||||
const replaces = {
|
||||
ios_saf: 'ios',
|
||||
android: 'chrome',
|
||||
}
|
||||
|
||||
const separator = ' '
|
||||
|
||||
return (
|
||||
browserslist(browserslistConfig, options)
|
||||
// filter out the unsupported ones
|
||||
.filter((b) => !UNSUPPORTED.some((u) => b.startsWith(u)))
|
||||
// replaces safari TP with latest safari version
|
||||
.map((b) => {
|
||||
if (b === 'safari TP') {
|
||||
return browserslist('last 1 safari version')[0]
|
||||
}
|
||||
|
||||
return b
|
||||
})
|
||||
// transform into ['chrome', '88']
|
||||
.map((b) => b.split(separator))
|
||||
// replace the similar browser
|
||||
.map((b) => {
|
||||
if (replaces[b[0]]) {
|
||||
b[0] = replaces[b[0]]
|
||||
}
|
||||
|
||||
return b
|
||||
})
|
||||
// 11.0-12.0 --> 11.0
|
||||
.map((b) => {
|
||||
if (b[1].includes('-')) {
|
||||
b[1] = b[1].slice(0, b[1].indexOf('-'))
|
||||
}
|
||||
|
||||
return b
|
||||
})
|
||||
// 11.0 --> 11
|
||||
.map((b) => {
|
||||
if (b[1].endsWith('.0')) {
|
||||
b[1] = b[1].slice(0, -2)
|
||||
}
|
||||
|
||||
return b
|
||||
})
|
||||
// removes invalid versions that will break esbuild
|
||||
// https://github.com/evanw/esbuild/blob/35c0d65b9d4f29a26176404d2890d1b499634e9f/compat-table/src/caniuse.ts#L119-L122
|
||||
.filter((b) => /^\d+(\.\d+)*$/.test(b[1]))
|
||||
// only get the targets supported by esbuild
|
||||
.filter((b) => SUPPORTED_ESBUILD_TARGETS.includes(b[0]))
|
||||
// only get the oldest version, assuming that the older version
|
||||
// is last in the array
|
||||
.reduce((acc, b) => {
|
||||
const existingIndex = acc.findIndex((br) => br[0] === b[0])
|
||||
|
||||
if (existingIndex !== -1) {
|
||||
acc[existingIndex][1] = b[1]
|
||||
} else {
|
||||
acc.push(b)
|
||||
}
|
||||
return acc
|
||||
}, [])
|
||||
// remove separator
|
||||
.map((b) => b.join(''))
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user