Skip to content

Commit 34b67a4

Browse files
committed
Add API to convert browserslist to targets
1 parent 8f49928 commit 34b67a4

File tree

7 files changed

+75
-7
lines changed

7 files changed

+75
-7
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ A CSS parser, transformer, and minifier written in Rust.
4545

4646
### From JavaScript
4747

48-
More docs to come, but here is a simple example:
48+
See the [TypeScript definitions](blob/master/node/index.d.ts) for full API docs.
49+
50+
Here is a simple example that compiles the input CSS for Safari 13.2, and minifies the output.
4951

5052
```js
5153
const css = require('@parcel/css');
@@ -63,6 +65,15 @@ let {code, map} = css.transform({
6365
});
6466
```
6567

68+
You can also convert the results of running `browserslist` into targets which can be passed to `@parcel/css`:
69+
70+
```js
71+
const browserslist = require('browserslist');
72+
const css = require('@parcel/css');
73+
74+
let targets = css.browserslistToTargets(browserslist('>= 0.25%'));
75+
```
76+
6677
### From Rust
6778

6879
See the Rust API docs on [docs.rs](https://docs.rs/parcel_css).

bench.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const css = require('./native');
1+
const css = require('./');
22
const cssnano = require('cssnano');
33
const postcss = require('postcss');
44
const esbuild = require('esbuild');

node/browserslistToTargets.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const BROWSER_MAPPING = {
2+
and_chr: 'chrome',
3+
and_ff: 'firefox',
4+
ie_mob: 'ie',
5+
op_mob: 'opera',
6+
and_qq: null,
7+
and_uc: null,
8+
baidu: null,
9+
bb: null,
10+
kaios: null,
11+
op_mini: null,
12+
};
13+
14+
function browserslistToTargets(browserslist) {
15+
let targets = {};
16+
for (let browser of browserslist) {
17+
let [name, v] = browser.split(' ');
18+
if (BROWSER_MAPPING[name] === null) {
19+
continue;
20+
}
21+
22+
let version = parseVersion(v);
23+
if (version == null) {
24+
continue;
25+
}
26+
27+
if (targets[name] == null || version < targets[name]) {
28+
targets[name] = version;
29+
}
30+
}
31+
32+
return targets;
33+
}
34+
35+
function parseVersion(version) {
36+
let [major, minor = 0, patch = 0] = version
37+
.split('-')[0]
38+
.split('.')
39+
.map(v => parseInt(v, 10));
40+
41+
if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
42+
return null;
43+
}
44+
45+
return (major << 16) | (minor << 8) | patch;
46+
}
47+
48+
module.exports = browserslistToTargets;

node/index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,9 @@ export interface TransformAttributeResult {
163163
* Compiles a single CSS declaration list, such as an inline style attribute in HTML.
164164
*/
165165
export declare function transformStyleAttribute(options: TransformAttributeOptions): TransformAttributeResult;
166+
167+
/**
168+
* Converts a browserslist result into targets that can be passed to @parcel/css.
169+
* @param browserslist the result of calling `browserslist`
170+
*/
171+
export declare function browserslistToTargets(browserslist: string[]): Targets;

native.js renamed to node/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ if (process.platform === 'linux') {
1414

1515
let name = `parcel-css.${parts.join('-')}.node`;
1616
if (process.env.CSS_TRANSFORMER_WASM) {
17-
module.exports = require(`./pkg`);
17+
module.exports = require(`../pkg`);
1818
} else {
19-
module.exports = require(`./${name}`);
19+
module.exports = require(`../${name}`);
2020
}
21+
22+
module.exports.browserslistToTargets = require('./browserslistToTargets');

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0-alpha.8",
44
"license": "MIT",
55
"description": "A CSS parser, transformer, and minifier written in Rust",
6-
"main": "native.js",
6+
"main": "node/index.js",
77
"types": "node/index.d.ts",
88
"targets": {
99
"main": false,
@@ -28,7 +28,8 @@
2828
"name": "parcel-css"
2929
},
3030
"files": [
31-
"native.js",
31+
"node/*.js",
32+
"node/*.d.ts",
3233
"*.node"
3334
],
3435
"dependencies": {

src/rules/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::vendor_prefix::VendorPrefix;
2828
use crate::prefixes::Feature;
2929
use crate::targets::Browsers;
3030
use std::collections::{HashMap, HashSet};
31-
use crate::selector::{is_equivalent, get_prefix, get_necessary_prefixes, is_unused};
31+
use crate::selector::{is_equivalent, get_prefix, get_necessary_prefixes};
3232
use crate::error::PrinterError;
3333
use crate::logical::LogicalProperties;
3434

0 commit comments

Comments
 (0)