postcss-units-to-px 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +19 -0
- package/dist/index.cjs +64 -52
- package/dist/index.d.cts +143 -11
- package/dist/index.d.mts +143 -11
- package/dist/index.mjs +65 -53
- package/package.json +15 -10
package/README.md
CHANGED
|
@@ -59,6 +59,25 @@ Type: `(value, unit, context) => number`
|
|
|
59
59
|
|
|
60
60
|
Global conversion function used when a unit does not have a per-unit rule (or when that rule is `null`).
|
|
61
61
|
|
|
62
|
+
Signature:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
type Transform = (value: number, unit: string, context: TransformContext) => number
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Example:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
const plugin = unitsToPx({
|
|
72
|
+
transform(value, unit, context) {
|
|
73
|
+
if (unit === 'em' && context.prop === 'letter-spacing') {
|
|
74
|
+
return value * 12
|
|
75
|
+
}
|
|
76
|
+
return value * 16
|
|
77
|
+
},
|
|
78
|
+
})
|
|
79
|
+
```
|
|
80
|
+
|
|
62
81
|
### unitPrecision
|
|
63
82
|
|
|
64
83
|
Type: `number`
|
package/dist/index.cjs
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
let postcss_plugin_shared = require("postcss-plugin-shared");
|
|
2
2
|
|
|
3
|
-
//#region package.json
|
|
4
|
-
var name = "postcss-units-to-px";
|
|
5
|
-
|
|
6
|
-
//#endregion
|
|
7
3
|
//#region src/defaults.ts
|
|
8
4
|
const defaultUnitMap = {
|
|
9
5
|
rem: 16,
|
|
@@ -26,11 +22,35 @@ const defaultOptions = {
|
|
|
26
22
|
disabled: false
|
|
27
23
|
};
|
|
28
24
|
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region package.json
|
|
27
|
+
var name = "postcss-units-to-px";
|
|
28
|
+
|
|
29
29
|
//#endregion
|
|
30
30
|
//#region src/shared.ts
|
|
31
|
+
/**
|
|
32
|
+
* PostCSS plugin name used by this package.
|
|
33
|
+
*
|
|
34
|
+
* @default package.json name
|
|
35
|
+
*/
|
|
31
36
|
const postcssPlugin = name;
|
|
37
|
+
/**
|
|
38
|
+
* Merge user options with defaults for postcss-units-to-px.
|
|
39
|
+
*
|
|
40
|
+
* Defaults:
|
|
41
|
+
* - unitMap: { rem: 16, em: 16, vw: 3.75, vh: 6.67, vmin: 3.75, vmax: 6.67, rpx: 0.5 }
|
|
42
|
+
* - unitPrecision: 5
|
|
43
|
+
* - propList: ['*']
|
|
44
|
+
* - replace: true
|
|
45
|
+
* - mediaQuery: false
|
|
46
|
+
* - minValue: 0
|
|
47
|
+
* - exclude: [/node_modules/i]
|
|
48
|
+
* - disabled: false
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const config = getConfig({ unitPrecision: 4 })
|
|
52
|
+
*/
|
|
32
53
|
const getConfig = (0, postcss_plugin_shared.createConfigGetter)(defaultOptions);
|
|
33
|
-
const blacklistedSelector = postcss_plugin_shared.maybeBlacklistedSelector;
|
|
34
54
|
|
|
35
55
|
//#endregion
|
|
36
56
|
//#region src/plugin.ts
|
|
@@ -66,62 +86,54 @@ function createReplace(unitMap, unitPrecision, minValue, transform, context) {
|
|
|
66
86
|
return `${roundedValue}px`;
|
|
67
87
|
};
|
|
68
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Convert multiple CSS units to px based on a unit map and optional transform.
|
|
91
|
+
*
|
|
92
|
+
* Defaults:
|
|
93
|
+
* - unitMap: { rem: 16, em: 16, vw: 3.75, vh: 6.67, vmin: 3.75, vmax: 6.67, rpx: 0.5 }
|
|
94
|
+
* - unitPrecision: 5
|
|
95
|
+
* - propList: ['*']
|
|
96
|
+
* - replace: true
|
|
97
|
+
* - mediaQuery: false
|
|
98
|
+
* - minValue: 0
|
|
99
|
+
* - exclude: [/node_modules/i]
|
|
100
|
+
* - disabled: false
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* import postcss from 'postcss'
|
|
104
|
+
* import unitsToPx from 'postcss-units-to-px'
|
|
105
|
+
*
|
|
106
|
+
* const result = await postcss([unitsToPx({
|
|
107
|
+
* unitMap: { rem: 16, vw: 3.75 },
|
|
108
|
+
* transform: (value, unit) => (unit === 'em' ? value * 12 : value * 16),
|
|
109
|
+
* })]).process('.a{margin:1rem 1vw}', { from: undefined })
|
|
110
|
+
*/
|
|
69
111
|
const plugin = (options = {}) => {
|
|
70
|
-
const {
|
|
112
|
+
const { transform, unitPrecision, minValue, selectorBlackList, propList, replace, mediaQuery, exclude, disabled } = getConfig(options);
|
|
113
|
+
const mergedUnitMap = {
|
|
114
|
+
...defaultUnitMap,
|
|
115
|
+
...options.unitMap ?? {}
|
|
116
|
+
};
|
|
71
117
|
if (disabled) return { postcssPlugin };
|
|
72
118
|
const normalizedUnitMap = normalizeUnitMap(mergedUnitMap);
|
|
73
119
|
const units = getUnitsForRegex(normalizedUnitMap);
|
|
74
120
|
if (units.length === 0) return { postcssPlugin };
|
|
75
121
|
const unitRegex = (0, postcss_plugin_shared.createUnitRegex)({ units });
|
|
76
|
-
const unitTestRegex = new RegExp(unitRegex.source, unitRegex.flags.replace("g", ""));
|
|
77
|
-
const satisfyPropList = (0, postcss_plugin_shared.createPropListMatcher)(propList);
|
|
78
|
-
const excludeFn = (0, postcss_plugin_shared.createExcludeMatcher)(exclude);
|
|
79
|
-
const hasSelectorBlacklist = selectorBlackList.length > 0;
|
|
80
122
|
return {
|
|
81
123
|
postcssPlugin,
|
|
82
124
|
Once(css) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
const replacer = createReplace(normalizedUnitMap, unitPrecision, minValue, transform, {
|
|
97
|
-
root: css,
|
|
98
|
-
input,
|
|
99
|
-
filePath,
|
|
100
|
-
decl,
|
|
101
|
-
rule,
|
|
102
|
-
atRule: rule.parent?.type === "atrule" ? rule.parent : void 0,
|
|
103
|
-
prop: decl.prop,
|
|
104
|
-
selector: rule.selector
|
|
105
|
-
});
|
|
106
|
-
const value = decl.value.replace(unitRegex, replacer);
|
|
107
|
-
if (value === decl.value) return;
|
|
108
|
-
if ((rule.nodes?.length ?? 0) > 1 && (0, postcss_plugin_shared.declarationExists)(rule, decl.prop, value)) return;
|
|
109
|
-
if (replace) {
|
|
110
|
-
decl.value = value;
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
decl.cloneAfter({ value });
|
|
114
|
-
});
|
|
115
|
-
css.walkAtRules((atRule) => {
|
|
116
|
-
if (!mediaQuery || atRule.name !== "media") return;
|
|
117
|
-
if (!unitTestRegex.test(atRule.params)) return;
|
|
118
|
-
const replacer = createReplace(normalizedUnitMap, unitPrecision, minValue, transform, {
|
|
119
|
-
root: css,
|
|
120
|
-
input,
|
|
121
|
-
filePath,
|
|
122
|
-
atRule
|
|
123
|
-
});
|
|
124
|
-
atRule.params = atRule.params.replace(unitRegex, replacer);
|
|
125
|
+
(0, postcss_plugin_shared.walkAndReplaceValues)({
|
|
126
|
+
root: css,
|
|
127
|
+
unitRegex,
|
|
128
|
+
propList,
|
|
129
|
+
selectorBlackList,
|
|
130
|
+
exclude,
|
|
131
|
+
replace,
|
|
132
|
+
mediaQuery,
|
|
133
|
+
createReplacer: (context) => {
|
|
134
|
+
return createReplace(normalizedUnitMap, unitPrecision, minValue, transform, context);
|
|
135
|
+
},
|
|
136
|
+
shouldProcessAtRule: (atRule) => atRule.name === "media"
|
|
125
137
|
});
|
|
126
138
|
}
|
|
127
139
|
};
|
package/dist/index.d.cts
CHANGED
|
@@ -1,34 +1,166 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PluginCreator } from "postcss";
|
|
2
|
+
import { ReplaceContext } from "postcss-plugin-shared";
|
|
2
3
|
|
|
3
4
|
//#region src/types.d.ts
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Context provided to conversion callbacks.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const transform = (value, unit, context) => {
|
|
10
|
+
* if (context.prop === 'letter-spacing') return value * 12
|
|
11
|
+
* return value * 16
|
|
12
|
+
* }
|
|
13
|
+
*/
|
|
14
|
+
type TransformContext = ReplaceContext;
|
|
15
|
+
/**
|
|
16
|
+
* Per-unit conversion callback.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const unitMap = {
|
|
20
|
+
* rem: (value, context) => value * 16,
|
|
21
|
+
* }
|
|
22
|
+
*/
|
|
14
23
|
type UnitTransform = (value: number, context: TransformContext) => number;
|
|
24
|
+
/**
|
|
25
|
+
* Global conversion callback when no per-unit rule is available.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* const transform = (value, unit, context) => {
|
|
29
|
+
* return unit === 'vw' ? value * 3.75 : value * 16
|
|
30
|
+
* }
|
|
31
|
+
*/
|
|
15
32
|
type GlobalTransform = (value: number, unit: string, context: TransformContext) => number;
|
|
33
|
+
/**
|
|
34
|
+
* Per-unit conversion rules.
|
|
35
|
+
*
|
|
36
|
+
* @default
|
|
37
|
+
* {
|
|
38
|
+
* rem: 16,
|
|
39
|
+
* em: 16,
|
|
40
|
+
* vw: 3.75,
|
|
41
|
+
* vh: 6.67,
|
|
42
|
+
* vmin: 3.75,
|
|
43
|
+
* vmax: 6.67,
|
|
44
|
+
* rpx: 0.5
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* const unitMap = {
|
|
49
|
+
* rem: 16,
|
|
50
|
+
* px: null,
|
|
51
|
+
* vh: (value) => value * 6.67,
|
|
52
|
+
* }
|
|
53
|
+
*/
|
|
16
54
|
type UnitMap = Record<string, number | UnitTransform | null>;
|
|
17
55
|
interface UserDefinedOptions {
|
|
56
|
+
/**
|
|
57
|
+
* Per-unit conversion rules. Numeric values are multipliers.
|
|
58
|
+
*
|
|
59
|
+
* @default
|
|
60
|
+
* {
|
|
61
|
+
* rem: 16,
|
|
62
|
+
* em: 16,
|
|
63
|
+
* vw: 3.75,
|
|
64
|
+
* vh: 6.67,
|
|
65
|
+
* vmin: 3.75,
|
|
66
|
+
* vmax: 6.67,
|
|
67
|
+
* rpx: 0.5
|
|
68
|
+
* }
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* unitMap: { rem: 10, vw: 3.2, px: null }
|
|
72
|
+
*/
|
|
18
73
|
unitMap?: UnitMap;
|
|
74
|
+
/**
|
|
75
|
+
* Global conversion callback when a unit has no rule or is set to null.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* transform: (value, unit) => (unit === 'em' ? value * 12 : value * 16)
|
|
79
|
+
*/
|
|
19
80
|
transform?: GlobalTransform;
|
|
81
|
+
/**
|
|
82
|
+
* Decimal precision for generated px values.
|
|
83
|
+
*
|
|
84
|
+
* @default 5
|
|
85
|
+
*/
|
|
20
86
|
unitPrecision?: number;
|
|
87
|
+
/**
|
|
88
|
+
* Minimum source value to convert.
|
|
89
|
+
*
|
|
90
|
+
* @default 0
|
|
91
|
+
*/
|
|
21
92
|
minValue?: number;
|
|
93
|
+
/**
|
|
94
|
+
* Selectors to ignore.
|
|
95
|
+
*
|
|
96
|
+
* @default []
|
|
97
|
+
*/
|
|
22
98
|
selectorBlackList?: (string | RegExp)[];
|
|
99
|
+
/**
|
|
100
|
+
* Only declarations with matching properties are processed.
|
|
101
|
+
*
|
|
102
|
+
* @default ['*']
|
|
103
|
+
*/
|
|
23
104
|
propList?: (string | RegExp)[];
|
|
105
|
+
/**
|
|
106
|
+
* Replace the original declaration value.
|
|
107
|
+
*
|
|
108
|
+
* @default true
|
|
109
|
+
*/
|
|
24
110
|
replace?: boolean;
|
|
111
|
+
/**
|
|
112
|
+
* Enable unit conversion inside @media params.
|
|
113
|
+
*
|
|
114
|
+
* @default false
|
|
115
|
+
*/
|
|
25
116
|
mediaQuery?: boolean;
|
|
117
|
+
/**
|
|
118
|
+
* Exclude files by path.
|
|
119
|
+
*
|
|
120
|
+
* @default [/node_modules/i]
|
|
121
|
+
*/
|
|
26
122
|
exclude?: (string | RegExp)[] | ((filePath: string) => boolean);
|
|
123
|
+
/**
|
|
124
|
+
* Disable this plugin.
|
|
125
|
+
*
|
|
126
|
+
* @default false
|
|
127
|
+
*/
|
|
27
128
|
disabled?: boolean;
|
|
28
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* PostCSS plugin creator for postcss-units-to-px.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* import postcss from 'postcss'
|
|
135
|
+
* import unitsToPx from 'postcss-units-to-px'
|
|
136
|
+
*
|
|
137
|
+
* const result = await postcss([unitsToPx()]).process('.a{margin:1rem}', { from: undefined })
|
|
138
|
+
*/
|
|
29
139
|
type PostcssUnitsToPx = PluginCreator<UserDefinedOptions>;
|
|
30
140
|
//#endregion
|
|
31
141
|
//#region src/plugin.d.ts
|
|
142
|
+
/**
|
|
143
|
+
* Convert multiple CSS units to px based on a unit map and optional transform.
|
|
144
|
+
*
|
|
145
|
+
* Defaults:
|
|
146
|
+
* - unitMap: { rem: 16, em: 16, vw: 3.75, vh: 6.67, vmin: 3.75, vmax: 6.67, rpx: 0.5 }
|
|
147
|
+
* - unitPrecision: 5
|
|
148
|
+
* - propList: ['*']
|
|
149
|
+
* - replace: true
|
|
150
|
+
* - mediaQuery: false
|
|
151
|
+
* - minValue: 0
|
|
152
|
+
* - exclude: [/node_modules/i]
|
|
153
|
+
* - disabled: false
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* import postcss from 'postcss'
|
|
157
|
+
* import unitsToPx from 'postcss-units-to-px'
|
|
158
|
+
*
|
|
159
|
+
* const result = await postcss([unitsToPx({
|
|
160
|
+
* unitMap: { rem: 16, vw: 3.75 },
|
|
161
|
+
* transform: (value, unit) => (unit === 'em' ? value * 12 : value * 16),
|
|
162
|
+
* })]).process('.a{margin:1rem 1vw}', { from: undefined })
|
|
163
|
+
*/
|
|
32
164
|
declare const plugin: PostcssUnitsToPx;
|
|
33
165
|
//#endregion
|
|
34
166
|
export { GlobalTransform, PostcssUnitsToPx, TransformContext, UnitMap, UnitTransform, UserDefinedOptions, plugin as default };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,34 +1,166 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ReplaceContext } from "postcss-plugin-shared";
|
|
2
|
+
import { PluginCreator } from "postcss";
|
|
2
3
|
|
|
3
4
|
//#region src/types.d.ts
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Context provided to conversion callbacks.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const transform = (value, unit, context) => {
|
|
10
|
+
* if (context.prop === 'letter-spacing') return value * 12
|
|
11
|
+
* return value * 16
|
|
12
|
+
* }
|
|
13
|
+
*/
|
|
14
|
+
type TransformContext = ReplaceContext;
|
|
15
|
+
/**
|
|
16
|
+
* Per-unit conversion callback.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const unitMap = {
|
|
20
|
+
* rem: (value, context) => value * 16,
|
|
21
|
+
* }
|
|
22
|
+
*/
|
|
14
23
|
type UnitTransform = (value: number, context: TransformContext) => number;
|
|
24
|
+
/**
|
|
25
|
+
* Global conversion callback when no per-unit rule is available.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* const transform = (value, unit, context) => {
|
|
29
|
+
* return unit === 'vw' ? value * 3.75 : value * 16
|
|
30
|
+
* }
|
|
31
|
+
*/
|
|
15
32
|
type GlobalTransform = (value: number, unit: string, context: TransformContext) => number;
|
|
33
|
+
/**
|
|
34
|
+
* Per-unit conversion rules.
|
|
35
|
+
*
|
|
36
|
+
* @default
|
|
37
|
+
* {
|
|
38
|
+
* rem: 16,
|
|
39
|
+
* em: 16,
|
|
40
|
+
* vw: 3.75,
|
|
41
|
+
* vh: 6.67,
|
|
42
|
+
* vmin: 3.75,
|
|
43
|
+
* vmax: 6.67,
|
|
44
|
+
* rpx: 0.5
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* const unitMap = {
|
|
49
|
+
* rem: 16,
|
|
50
|
+
* px: null,
|
|
51
|
+
* vh: (value) => value * 6.67,
|
|
52
|
+
* }
|
|
53
|
+
*/
|
|
16
54
|
type UnitMap = Record<string, number | UnitTransform | null>;
|
|
17
55
|
interface UserDefinedOptions {
|
|
56
|
+
/**
|
|
57
|
+
* Per-unit conversion rules. Numeric values are multipliers.
|
|
58
|
+
*
|
|
59
|
+
* @default
|
|
60
|
+
* {
|
|
61
|
+
* rem: 16,
|
|
62
|
+
* em: 16,
|
|
63
|
+
* vw: 3.75,
|
|
64
|
+
* vh: 6.67,
|
|
65
|
+
* vmin: 3.75,
|
|
66
|
+
* vmax: 6.67,
|
|
67
|
+
* rpx: 0.5
|
|
68
|
+
* }
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* unitMap: { rem: 10, vw: 3.2, px: null }
|
|
72
|
+
*/
|
|
18
73
|
unitMap?: UnitMap;
|
|
74
|
+
/**
|
|
75
|
+
* Global conversion callback when a unit has no rule or is set to null.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* transform: (value, unit) => (unit === 'em' ? value * 12 : value * 16)
|
|
79
|
+
*/
|
|
19
80
|
transform?: GlobalTransform;
|
|
81
|
+
/**
|
|
82
|
+
* Decimal precision for generated px values.
|
|
83
|
+
*
|
|
84
|
+
* @default 5
|
|
85
|
+
*/
|
|
20
86
|
unitPrecision?: number;
|
|
87
|
+
/**
|
|
88
|
+
* Minimum source value to convert.
|
|
89
|
+
*
|
|
90
|
+
* @default 0
|
|
91
|
+
*/
|
|
21
92
|
minValue?: number;
|
|
93
|
+
/**
|
|
94
|
+
* Selectors to ignore.
|
|
95
|
+
*
|
|
96
|
+
* @default []
|
|
97
|
+
*/
|
|
22
98
|
selectorBlackList?: (string | RegExp)[];
|
|
99
|
+
/**
|
|
100
|
+
* Only declarations with matching properties are processed.
|
|
101
|
+
*
|
|
102
|
+
* @default ['*']
|
|
103
|
+
*/
|
|
23
104
|
propList?: (string | RegExp)[];
|
|
105
|
+
/**
|
|
106
|
+
* Replace the original declaration value.
|
|
107
|
+
*
|
|
108
|
+
* @default true
|
|
109
|
+
*/
|
|
24
110
|
replace?: boolean;
|
|
111
|
+
/**
|
|
112
|
+
* Enable unit conversion inside @media params.
|
|
113
|
+
*
|
|
114
|
+
* @default false
|
|
115
|
+
*/
|
|
25
116
|
mediaQuery?: boolean;
|
|
117
|
+
/**
|
|
118
|
+
* Exclude files by path.
|
|
119
|
+
*
|
|
120
|
+
* @default [/node_modules/i]
|
|
121
|
+
*/
|
|
26
122
|
exclude?: (string | RegExp)[] | ((filePath: string) => boolean);
|
|
123
|
+
/**
|
|
124
|
+
* Disable this plugin.
|
|
125
|
+
*
|
|
126
|
+
* @default false
|
|
127
|
+
*/
|
|
27
128
|
disabled?: boolean;
|
|
28
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* PostCSS plugin creator for postcss-units-to-px.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* import postcss from 'postcss'
|
|
135
|
+
* import unitsToPx from 'postcss-units-to-px'
|
|
136
|
+
*
|
|
137
|
+
* const result = await postcss([unitsToPx()]).process('.a{margin:1rem}', { from: undefined })
|
|
138
|
+
*/
|
|
29
139
|
type PostcssUnitsToPx = PluginCreator<UserDefinedOptions>;
|
|
30
140
|
//#endregion
|
|
31
141
|
//#region src/plugin.d.ts
|
|
142
|
+
/**
|
|
143
|
+
* Convert multiple CSS units to px based on a unit map and optional transform.
|
|
144
|
+
*
|
|
145
|
+
* Defaults:
|
|
146
|
+
* - unitMap: { rem: 16, em: 16, vw: 3.75, vh: 6.67, vmin: 3.75, vmax: 6.67, rpx: 0.5 }
|
|
147
|
+
* - unitPrecision: 5
|
|
148
|
+
* - propList: ['*']
|
|
149
|
+
* - replace: true
|
|
150
|
+
* - mediaQuery: false
|
|
151
|
+
* - minValue: 0
|
|
152
|
+
* - exclude: [/node_modules/i]
|
|
153
|
+
* - disabled: false
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* import postcss from 'postcss'
|
|
157
|
+
* import unitsToPx from 'postcss-units-to-px'
|
|
158
|
+
*
|
|
159
|
+
* const result = await postcss([unitsToPx({
|
|
160
|
+
* unitMap: { rem: 16, vw: 3.75 },
|
|
161
|
+
* transform: (value, unit) => (unit === 'em' ? value * 12 : value * 16),
|
|
162
|
+
* })]).process('.a{margin:1rem 1vw}', { from: undefined })
|
|
163
|
+
*/
|
|
32
164
|
declare const plugin: PostcssUnitsToPx;
|
|
33
165
|
//#endregion
|
|
34
166
|
export { GlobalTransform, PostcssUnitsToPx, TransformContext, UnitMap, UnitTransform, UserDefinedOptions, plugin as default };
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import { createConfigGetter,
|
|
1
|
+
import { createConfigGetter, createUnitRegex, toFixed, walkAndReplaceValues } from "postcss-plugin-shared";
|
|
2
2
|
|
|
3
|
-
//#region package.json
|
|
4
|
-
var name = "postcss-units-to-px";
|
|
5
|
-
|
|
6
|
-
//#endregion
|
|
7
3
|
//#region src/defaults.ts
|
|
8
4
|
const defaultUnitMap = {
|
|
9
5
|
rem: 16,
|
|
@@ -26,11 +22,35 @@ const defaultOptions = {
|
|
|
26
22
|
disabled: false
|
|
27
23
|
};
|
|
28
24
|
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region package.json
|
|
27
|
+
var name = "postcss-units-to-px";
|
|
28
|
+
|
|
29
29
|
//#endregion
|
|
30
30
|
//#region src/shared.ts
|
|
31
|
+
/**
|
|
32
|
+
* PostCSS plugin name used by this package.
|
|
33
|
+
*
|
|
34
|
+
* @default package.json name
|
|
35
|
+
*/
|
|
31
36
|
const postcssPlugin = name;
|
|
37
|
+
/**
|
|
38
|
+
* Merge user options with defaults for postcss-units-to-px.
|
|
39
|
+
*
|
|
40
|
+
* Defaults:
|
|
41
|
+
* - unitMap: { rem: 16, em: 16, vw: 3.75, vh: 6.67, vmin: 3.75, vmax: 6.67, rpx: 0.5 }
|
|
42
|
+
* - unitPrecision: 5
|
|
43
|
+
* - propList: ['*']
|
|
44
|
+
* - replace: true
|
|
45
|
+
* - mediaQuery: false
|
|
46
|
+
* - minValue: 0
|
|
47
|
+
* - exclude: [/node_modules/i]
|
|
48
|
+
* - disabled: false
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const config = getConfig({ unitPrecision: 4 })
|
|
52
|
+
*/
|
|
32
53
|
const getConfig = createConfigGetter(defaultOptions);
|
|
33
|
-
const blacklistedSelector = maybeBlacklistedSelector;
|
|
34
54
|
|
|
35
55
|
//#endregion
|
|
36
56
|
//#region src/plugin.ts
|
|
@@ -66,62 +86,54 @@ function createReplace(unitMap, unitPrecision, minValue, transform, context) {
|
|
|
66
86
|
return `${roundedValue}px`;
|
|
67
87
|
};
|
|
68
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Convert multiple CSS units to px based on a unit map and optional transform.
|
|
91
|
+
*
|
|
92
|
+
* Defaults:
|
|
93
|
+
* - unitMap: { rem: 16, em: 16, vw: 3.75, vh: 6.67, vmin: 3.75, vmax: 6.67, rpx: 0.5 }
|
|
94
|
+
* - unitPrecision: 5
|
|
95
|
+
* - propList: ['*']
|
|
96
|
+
* - replace: true
|
|
97
|
+
* - mediaQuery: false
|
|
98
|
+
* - minValue: 0
|
|
99
|
+
* - exclude: [/node_modules/i]
|
|
100
|
+
* - disabled: false
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* import postcss from 'postcss'
|
|
104
|
+
* import unitsToPx from 'postcss-units-to-px'
|
|
105
|
+
*
|
|
106
|
+
* const result = await postcss([unitsToPx({
|
|
107
|
+
* unitMap: { rem: 16, vw: 3.75 },
|
|
108
|
+
* transform: (value, unit) => (unit === 'em' ? value * 12 : value * 16),
|
|
109
|
+
* })]).process('.a{margin:1rem 1vw}', { from: undefined })
|
|
110
|
+
*/
|
|
69
111
|
const plugin = (options = {}) => {
|
|
70
|
-
const {
|
|
112
|
+
const { transform, unitPrecision, minValue, selectorBlackList, propList, replace, mediaQuery, exclude, disabled } = getConfig(options);
|
|
113
|
+
const mergedUnitMap = {
|
|
114
|
+
...defaultUnitMap,
|
|
115
|
+
...options.unitMap ?? {}
|
|
116
|
+
};
|
|
71
117
|
if (disabled) return { postcssPlugin };
|
|
72
118
|
const normalizedUnitMap = normalizeUnitMap(mergedUnitMap);
|
|
73
119
|
const units = getUnitsForRegex(normalizedUnitMap);
|
|
74
120
|
if (units.length === 0) return { postcssPlugin };
|
|
75
121
|
const unitRegex = createUnitRegex({ units });
|
|
76
|
-
const unitTestRegex = new RegExp(unitRegex.source, unitRegex.flags.replace("g", ""));
|
|
77
|
-
const satisfyPropList = createPropListMatcher(propList);
|
|
78
|
-
const excludeFn = createExcludeMatcher(exclude);
|
|
79
|
-
const hasSelectorBlacklist = selectorBlackList.length > 0;
|
|
80
122
|
return {
|
|
81
123
|
postcssPlugin,
|
|
82
124
|
Once(css) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
const replacer = createReplace(normalizedUnitMap, unitPrecision, minValue, transform, {
|
|
97
|
-
root: css,
|
|
98
|
-
input,
|
|
99
|
-
filePath,
|
|
100
|
-
decl,
|
|
101
|
-
rule,
|
|
102
|
-
atRule: rule.parent?.type === "atrule" ? rule.parent : void 0,
|
|
103
|
-
prop: decl.prop,
|
|
104
|
-
selector: rule.selector
|
|
105
|
-
});
|
|
106
|
-
const value = decl.value.replace(unitRegex, replacer);
|
|
107
|
-
if (value === decl.value) return;
|
|
108
|
-
if ((rule.nodes?.length ?? 0) > 1 && declarationExists(rule, decl.prop, value)) return;
|
|
109
|
-
if (replace) {
|
|
110
|
-
decl.value = value;
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
decl.cloneAfter({ value });
|
|
114
|
-
});
|
|
115
|
-
css.walkAtRules((atRule) => {
|
|
116
|
-
if (!mediaQuery || atRule.name !== "media") return;
|
|
117
|
-
if (!unitTestRegex.test(atRule.params)) return;
|
|
118
|
-
const replacer = createReplace(normalizedUnitMap, unitPrecision, minValue, transform, {
|
|
119
|
-
root: css,
|
|
120
|
-
input,
|
|
121
|
-
filePath,
|
|
122
|
-
atRule
|
|
123
|
-
});
|
|
124
|
-
atRule.params = atRule.params.replace(unitRegex, replacer);
|
|
125
|
+
walkAndReplaceValues({
|
|
126
|
+
root: css,
|
|
127
|
+
unitRegex,
|
|
128
|
+
propList,
|
|
129
|
+
selectorBlackList,
|
|
130
|
+
exclude,
|
|
131
|
+
replace,
|
|
132
|
+
mediaQuery,
|
|
133
|
+
createReplacer: (context) => {
|
|
134
|
+
return createReplace(normalizedUnitMap, unitPrecision, minValue, transform, context);
|
|
135
|
+
},
|
|
136
|
+
shouldProcessAtRule: (atRule) => atRule.name === "media"
|
|
125
137
|
});
|
|
126
138
|
}
|
|
127
139
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postcss-units-to-px",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"description": "Convert multiple CSS units to px with PostCSS.",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -25,20 +25,25 @@
|
|
|
25
25
|
"sideEffects": false,
|
|
26
26
|
"exports": {
|
|
27
27
|
".": {
|
|
28
|
-
"types":
|
|
29
|
-
|
|
30
|
-
"
|
|
31
|
-
"default": "./dist/index.mjs"
|
|
28
|
+
"types": {
|
|
29
|
+
"import": "./dist/index.d.mts",
|
|
30
|
+
"require": "./dist/index.d.cts"
|
|
32
31
|
},
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
"default": "./dist/index.cjs"
|
|
36
|
-
}
|
|
32
|
+
"import": "./dist/index.mjs",
|
|
33
|
+
"require": "./dist/index.cjs"
|
|
37
34
|
}
|
|
38
35
|
},
|
|
39
36
|
"main": "./dist/index.cjs",
|
|
40
37
|
"module": "./dist/index.mjs",
|
|
41
38
|
"types": "./dist/index.d.mts",
|
|
39
|
+
"typesVersions": {
|
|
40
|
+
"<4.7": {
|
|
41
|
+
"*": [
|
|
42
|
+
"./dist/*",
|
|
43
|
+
"./dist/index.d.cts"
|
|
44
|
+
]
|
|
45
|
+
}
|
|
46
|
+
},
|
|
42
47
|
"files": [
|
|
43
48
|
"dist"
|
|
44
49
|
],
|
|
@@ -46,7 +51,7 @@
|
|
|
46
51
|
"postcss": "^8"
|
|
47
52
|
},
|
|
48
53
|
"dependencies": {
|
|
49
|
-
"postcss-plugin-shared": "^1.
|
|
54
|
+
"postcss-plugin-shared": "^1.1.0"
|
|
50
55
|
},
|
|
51
56
|
"scripts": {
|
|
52
57
|
"dev": "tsdown -w",
|