1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 |
|
---|
8 | var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
|
---|
9 |
|
---|
10 | var _minifyWeight = _interopRequireDefault(require("./lib/minify-weight"));
|
---|
11 |
|
---|
12 | var _minifyFamily = _interopRequireDefault(require("./lib/minify-family"));
|
---|
13 |
|
---|
14 | var _minifyFont = _interopRequireDefault(require("./lib/minify-font"));
|
---|
15 |
|
---|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
17 |
|
---|
18 | function hasVariableFunction(value) {
|
---|
19 | const lowerCasedValue = value.toLowerCase();
|
---|
20 | return lowerCasedValue.includes('var(') || lowerCasedValue.includes('env(');
|
---|
21 | }
|
---|
22 |
|
---|
23 | function transform(prop, value, opts) {
|
---|
24 | let lowerCasedProp = prop.toLowerCase();
|
---|
25 |
|
---|
26 | if (lowerCasedProp === 'font-weight' && !hasVariableFunction(value)) {
|
---|
27 | return (0, _minifyWeight.default)(value);
|
---|
28 | } else if (lowerCasedProp === 'font-family' && !hasVariableFunction(value)) {
|
---|
29 | const tree = (0, _postcssValueParser.default)(value);
|
---|
30 | tree.nodes = (0, _minifyFamily.default)(tree.nodes, opts);
|
---|
31 | return tree.toString();
|
---|
32 | } else if (lowerCasedProp === 'font') {
|
---|
33 | const tree = (0, _postcssValueParser.default)(value);
|
---|
34 | tree.nodes = (0, _minifyFont.default)(tree.nodes, opts);
|
---|
35 | return tree.toString();
|
---|
36 | }
|
---|
37 |
|
---|
38 | return value;
|
---|
39 | }
|
---|
40 |
|
---|
41 | function pluginCreator(opts) {
|
---|
42 | opts = Object.assign({}, {
|
---|
43 | removeAfterKeyword: false,
|
---|
44 | removeDuplicates: true,
|
---|
45 | removeQuotes: true
|
---|
46 | }, opts);
|
---|
47 | return {
|
---|
48 | postcssPlugin: 'postcss-minify-font-values',
|
---|
49 |
|
---|
50 | prepare() {
|
---|
51 | const cache = {};
|
---|
52 | return {
|
---|
53 | OnceExit(css) {
|
---|
54 | css.walkDecls(/font/i, decl => {
|
---|
55 | const value = decl.value;
|
---|
56 |
|
---|
57 | if (!value) {
|
---|
58 | return;
|
---|
59 | }
|
---|
60 |
|
---|
61 | const prop = decl.prop;
|
---|
62 | const cacheKey = `${prop}|${value}`;
|
---|
63 |
|
---|
64 | if (cache[cacheKey]) {
|
---|
65 | decl.value = cache[cacheKey];
|
---|
66 | return;
|
---|
67 | }
|
---|
68 |
|
---|
69 | const newValue = transform(prop, value, opts);
|
---|
70 | decl.value = newValue;
|
---|
71 | cache[cacheKey] = newValue;
|
---|
72 | });
|
---|
73 | }
|
---|
74 |
|
---|
75 | };
|
---|
76 | }
|
---|
77 |
|
---|
78 | };
|
---|
79 | }
|
---|
80 |
|
---|
81 | pluginCreator.postcss = true;
|
---|
82 | var _default = pluginCreator;
|
---|
83 | exports.default = _default;
|
---|
84 | module.exports = exports.default; |
---|