Skip to content

Commit afb3c03

Browse files
committed
support more css units
1 parent bc25aa4 commit afb3c03

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

lib/parseComponent.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@ const traverse = require('@babel/traverse')
44
const generator = require('@babel/generator')
55
const t = require('@babel/types')
66
const posthtml = require('posthtml')
7+
const { UNITS_RE } = require('./units')
78

89
const LANGS = ['css', 'stylus', 'less', 'sass', 'scss']
910

10-
const CSS_UNITS = [
11-
...['cm', 'mm', 'in', 'px', 'pt', 'pc'],
12-
...['em', 'eh', 'ex', 'rem', 'vw', 'vh', 'vmin', 'vmax', '%']
13-
]
14-
15-
const UNIT_RE = new RegExp(`(${CSS_UNITS.join('|')});?(\\s|\\n|$)`)
16-
1711
module.exports = (content, opts) => {
1812
const sfc = parseComponent(content, opts)
1913

@@ -72,8 +66,8 @@ module.exports = (content, opts) => {
7266
let unit
7367
const nextQuasis = quasi.quasis[i + 1]
7468
if (nextQuasis) {
75-
// Test the next 5 chars
76-
const match = UNIT_RE.exec(nextQuasis.value.raw.slice(0, 5))
69+
// Test the next 6 chars
70+
const match = UNITS_RE.exec(nextQuasis.value.raw.slice(0, 6))
7771
unit = match && match[1]
7872
if (unit) {
7973
nextQuasis.value.raw = nextQuasis.value.raw.slice(

lib/units.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// https://www.w3.org/TR/css-values-4/
2+
const UNITS = [
3+
// font relative lengths
4+
'em',
5+
'ex',
6+
'cap',
7+
'ch',
8+
'ic',
9+
'rem',
10+
'lh',
11+
'rlh',
12+
13+
// viewport percentage lengths
14+
'vw',
15+
'vh',
16+
'vi',
17+
'vb',
18+
'vmin',
19+
'vmax',
20+
21+
// absolute lengths
22+
'cm',
23+
'mm',
24+
'Q',
25+
'in',
26+
'pc',
27+
'pt',
28+
'px',
29+
30+
// angle units
31+
'deg',
32+
'grad',
33+
'rad',
34+
'turn',
35+
36+
// duration units
37+
's',
38+
'ms',
39+
40+
// frequency units
41+
'Hz',
42+
'kHz',
43+
44+
// resolution units
45+
'dpi',
46+
'dpcm',
47+
'dppx',
48+
'x',
49+
50+
// https://www.w3.org/TR/css-grid-1/#fr-unit
51+
'fr',
52+
53+
// percentages
54+
'%'
55+
]
56+
57+
const UNITS_RE = new RegExp(`(${UNITS.join('|')});?(\\s|\\n|$)`)
58+
59+
module.exports = {
60+
UNITS,
61+
UNITS_RE
62+
}

0 commit comments

Comments
 (0)