UNPKG

1.21 kBJavaScriptView Raw
1'use strict';
2const transform = require('./lib/transform.js');
3
4/**
5 * @typedef {{precision?: number | false,
6 * preserve?: boolean,
7 * warnWhenCannotResolve?: boolean,
8 * mediaQueries?: boolean,
9 * selectors?: boolean}} PostCssCalcOptions
10 */
11/**
12 * @type {import('postcss').PluginCreator<PostCssCalcOptions>}
13 * @param {PostCssCalcOptions} opts
14 * @return {import('postcss').Plugin}
15 */
16function pluginCreator(opts) {
17 const options = Object.assign(
18 {
19 precision: 5,
20 preserve: false,
21 warnWhenCannotResolve: false,
22 mediaQueries: false,
23 selectors: false,
24 },
25 opts
26 );
27
28 return {
29 postcssPlugin: 'postcss-calc',
30 OnceExit(css, { result }) {
31 css.walk((node) => {
32 const { type } = node;
33 if (type === 'decl') {
34 transform(node, 'value', options, result);
35 }
36
37 if (type === 'atrule' && options.mediaQueries) {
38 transform(node, 'params', options, result);
39 }
40
41 if (type === 'rule' && options.selectors) {
42 transform(node, 'selector', options, result);
43 }
44 });
45 },
46 };
47}
48
49pluginCreator.postcss = true;
50
51module.exports = pluginCreator;