@stylexjs/shared 0.1.0-beta.1
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/.babelrc +16 -0
- package/__tests__/stylex-create-test.js +386 -0
- package/__tests__/stylex-keyframes-test.js +59 -0
- package/lib/common-types.js +1 -0
- package/lib/convert-to-className.js +40 -0
- package/lib/expand-shorthands.js +190 -0
- package/lib/generate-css-rule.js +64 -0
- package/lib/hash.js +61 -0
- package/lib/index.d.ts +90 -0
- package/lib/index.js +40 -0
- package/lib/messages.js +56 -0
- package/lib/physical-rtl/generate-ltr.js +135 -0
- package/lib/physical-rtl/generate-rtl.js +216 -0
- package/lib/stylex-create.js +128 -0
- package/lib/stylex-first-that-works.js +22 -0
- package/lib/stylex-include.js +38 -0
- package/lib/stylex-keyframes.js +63 -0
- package/lib/transform-value.js +58 -0
- package/lib/utils/dashify.js +17 -0
- package/lib/utils/genCSSRule.js +24 -0
- package/lib/utils/normalize-value.js +32 -0
- package/lib/utils/normalizers/detect-unclosed-fns.js +29 -0
- package/lib/utils/normalizers/font-size-px-to-rem.js +34 -0
- package/lib/utils/normalizers/leading-zero.js +36 -0
- package/lib/utils/normalizers/quotes.js +30 -0
- package/lib/utils/normalizers/timings.js +39 -0
- package/lib/utils/normalizers/whitespace.js +59 -0
- package/lib/utils/normalizers/zero-dimensions.js +44 -0
- package/lib/utils/object-utils.js +94 -0
- package/lib/validate.js +30 -0
- package/package.json +24 -0
- package/tsconfig.json +103 -0
@@ -0,0 +1,128 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.default = styleXCreateSet;
|
7
|
+
var _convertToClassName = _interopRequireDefault(require("./convert-to-className"));
|
8
|
+
var _expandShorthands = _interopRequireDefault(require("./expand-shorthands"));
|
9
|
+
var _objectUtils = require("./utils/object-utils");
|
10
|
+
var messages = _interopRequireWildcard(require("./messages"));
|
11
|
+
var _stylexInclude = require("./stylex-include");
|
12
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
13
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15
|
+
/**
|
16
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
17
|
+
*
|
18
|
+
* This source code is licensed under the MIT license found in the
|
19
|
+
* LICENSE file in the root directory of this source tree.
|
20
|
+
*
|
21
|
+
*
|
22
|
+
*/
|
23
|
+
|
24
|
+
// This takes the object of styles passed to `stylex.create` and transforms it.
|
25
|
+
// The transformation replaces style values with classNames.
|
26
|
+
//
|
27
|
+
// It also collects all injected styles along the way.
|
28
|
+
// It then returns a tuple of the transformed style Object and an object of injected styles.
|
29
|
+
//
|
30
|
+
// This function does some basic validation, and then uses `styleXCreateNamespace` to transform
|
31
|
+
// each namespace within,
|
32
|
+
//
|
33
|
+
// Before returning, it ensures that there are no duplicate styles being injected.
|
34
|
+
function styleXCreateSet(namespaces) {
|
35
|
+
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
36
|
+
const resolvedNamespaces = {};
|
37
|
+
const injectedStyles = {};
|
38
|
+
for (const namespaceName of Object.keys(namespaces)) {
|
39
|
+
const namespace = namespaces[namespaceName];
|
40
|
+
if (typeof namespace !== 'object' || Array.isArray(namespace)) {
|
41
|
+
throw new Error(messages.ILLEGAL_NAMESPACE_VALUE);
|
42
|
+
}
|
43
|
+
const [resolvedNamespace, injected] = styleXCreateNamespace(namespace, options);
|
44
|
+
resolvedNamespaces[namespaceName] = (0, _objectUtils.flattenObject)(resolvedNamespace);
|
45
|
+
for (const cn of Object.keys(injected)) {
|
46
|
+
if (injectedStyles[cn] == null) {
|
47
|
+
injectedStyles[cn] = injected[cn];
|
48
|
+
}
|
49
|
+
}
|
50
|
+
}
|
51
|
+
return [resolvedNamespaces, injectedStyles];
|
52
|
+
}
|
53
|
+
|
54
|
+
// Transforms a single style namespace.
|
55
|
+
// e.g. Something along the lines of:
|
56
|
+
// {color: 'red', margin: '10px'} =>
|
57
|
+
// {
|
58
|
+
// color: 'color-red',
|
59
|
+
// marginTop: 'margin-top-10px',
|
60
|
+
// marginBottom: 'margin-bottom-10px',
|
61
|
+
// marginStart: 'margin-start-10px',
|
62
|
+
// marginEnd: 'margin-end-10px'
|
63
|
+
// }
|
64
|
+
//
|
65
|
+
// First, it expands shorthand properties. (margin => marginTop, marginBottom, marginStart, marginEnd)
|
66
|
+
// Then, it converts each style value to a className.
|
67
|
+
// Then, it returns the transformed style Object and an object of injected styles.
|
68
|
+
function styleXCreateNamespace(style, options) {
|
69
|
+
const namespaceEntries = (0, _objectUtils.objEntries)(style);
|
70
|
+
|
71
|
+
// First the shorthand properties are expanded.
|
72
|
+
// e.g. `margin` gets expanded to `marginTop`, `marginBottom`, `marginStart`, `marginEnd`.
|
73
|
+
// `entries` is an array of [key, value] pairs.
|
74
|
+
const entries = namespaceEntries.flatMap(_ref => {
|
75
|
+
let [key, value] = _ref;
|
76
|
+
if (value instanceof _stylexInclude.IncludedStyles) {
|
77
|
+
return [[key, value]];
|
78
|
+
}
|
79
|
+
if (value != null && typeof value === 'object' && !Array.isArray(value)) {
|
80
|
+
if (!key.startsWith(':') && !key.startsWith('@')) {
|
81
|
+
throw new Error(messages.INVALID_PSEUDO);
|
82
|
+
}
|
83
|
+
return [[key, (0, _objectUtils.objFromEntries)((0, _objectUtils.objEntries)(value).flatMap(_ref2 => {
|
84
|
+
let [innerKey, innerValue] = _ref2;
|
85
|
+
if (innerValue != null && typeof innerValue === 'object' && !Array.isArray(innerValue)) {
|
86
|
+
throw new Error(messages.ILLEGAL_NESTED_PSEUDO);
|
87
|
+
}
|
88
|
+
return (0, _expandShorthands.default)([innerKey, innerValue]);
|
89
|
+
}))]];
|
90
|
+
} else {
|
91
|
+
if (typeof value !== 'string' && typeof value !== 'number' && !Array.isArray(value)) {
|
92
|
+
throw new Error(messages.ILLEGAL_PROP_VALUE);
|
93
|
+
}
|
94
|
+
if (Array.isArray(value) && value.some(val => typeof val === 'object')) {
|
95
|
+
throw new Error(messages.ILLEGAL_PROP_ARRAY_VALUE);
|
96
|
+
}
|
97
|
+
return (0, _expandShorthands.default)([key, value]);
|
98
|
+
}
|
99
|
+
});
|
100
|
+
|
101
|
+
// Now each [key, value] pair is considered a single atomic style.
|
102
|
+
// This atomic style is converted to a className by hashing
|
103
|
+
//
|
104
|
+
// The [key, className] pair is then added to the output Object: `resolvedNamespace`.
|
105
|
+
// While hashing, the CSS rule that the className is generated from is also added to the output Object: `injectedStyles`.
|
106
|
+
const resolvedNamespace = {};
|
107
|
+
const injectedStyles = {};
|
108
|
+
for (const [key, val] of entries) {
|
109
|
+
if (val instanceof _stylexInclude.IncludedStyles) {
|
110
|
+
resolvedNamespace[key] = val;
|
111
|
+
} else if (val != null && typeof val === 'object' && !Array.isArray(val)) {
|
112
|
+
const pseudo = key;
|
113
|
+
const innerObj = {};
|
114
|
+
for (const [innerKey, innerVal] of (0, _objectUtils.objEntries)(val)) {
|
115
|
+
const [updatedKey, className, cssRule] = (0, _convertToClassName.default)([innerKey, innerVal], pseudo, options);
|
116
|
+
innerObj[updatedKey] = className;
|
117
|
+
injectedStyles[updatedKey + pseudo] = [className, cssRule];
|
118
|
+
}
|
119
|
+
resolvedNamespace[key] = innerObj;
|
120
|
+
} else {
|
121
|
+
const [updatedKey, className, cssRule] = (0, _convertToClassName.default)([key, val], undefined, options);
|
122
|
+
resolvedNamespace[updatedKey] = className;
|
123
|
+
injectedStyles[updatedKey] = [className, cssRule];
|
124
|
+
}
|
125
|
+
}
|
126
|
+
const finalInjectedStyles = (0, _objectUtils.objFromEntries)((0, _objectUtils.objValues)(injectedStyles));
|
127
|
+
return [resolvedNamespace, finalInjectedStyles];
|
128
|
+
}
|
@@ -0,0 +1,22 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.default = stylexFirstThatWorks;
|
7
|
+
/**
|
8
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
9
|
+
*
|
10
|
+
* This source code is licensed under the MIT license found in the
|
11
|
+
* LICENSE file in the root directory of this source tree.
|
12
|
+
*
|
13
|
+
*
|
14
|
+
*
|
15
|
+
*/
|
16
|
+
|
17
|
+
function stylexFirstThatWorks() {
|
18
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
19
|
+
args[_key] = arguments[_key];
|
20
|
+
}
|
21
|
+
return [...args].reverse();
|
22
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.IncludedStyles = void 0;
|
7
|
+
exports.default = stylexInclude;
|
8
|
+
var messages = _interopRequireWildcard(require("./messages"));
|
9
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
10
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
11
|
+
/**
|
12
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
13
|
+
*
|
14
|
+
* This source code is licensed under the MIT license found in the
|
15
|
+
* LICENSE file in the root directory of this source tree.
|
16
|
+
*
|
17
|
+
*
|
18
|
+
*
|
19
|
+
*/
|
20
|
+
|
21
|
+
let number = 0;
|
22
|
+
function uuid() {
|
23
|
+
return `__included_${++number}__`;
|
24
|
+
}
|
25
|
+
class IncludedStyles {
|
26
|
+
constructor(astNode) {
|
27
|
+
this.astNode = astNode;
|
28
|
+
}
|
29
|
+
}
|
30
|
+
exports.IncludedStyles = IncludedStyles;
|
31
|
+
function stylexInclude(firstArg) {
|
32
|
+
if ((arguments.length <= 1 ? 0 : arguments.length - 1) > 0) {
|
33
|
+
throw new Error(messages.ILLEGAL_ARGUMENT_LENGTH);
|
34
|
+
}
|
35
|
+
return {
|
36
|
+
[uuid()]: new IncludedStyles(firstArg.node)
|
37
|
+
};
|
38
|
+
}
|
@@ -0,0 +1,63 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.default = styleXKeyframes;
|
7
|
+
var _hash = _interopRequireDefault(require("./hash"));
|
8
|
+
var _expandShorthands = _interopRequireDefault(require("./expand-shorthands"));
|
9
|
+
var _generateLtr = _interopRequireDefault(require("./physical-rtl/generate-ltr"));
|
10
|
+
var _generateRtl = _interopRequireDefault(require("./physical-rtl/generate-rtl"));
|
11
|
+
var _transformValue = _interopRequireDefault(require("./transform-value"));
|
12
|
+
var _dashify = _interopRequireDefault(require("./utils/dashify"));
|
13
|
+
var _objectUtils = require("./utils/object-utils");
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15
|
+
/**
|
16
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
17
|
+
*
|
18
|
+
* This source code is licensed under the MIT license found in the
|
19
|
+
* LICENSE file in the root directory of this source tree.
|
20
|
+
*
|
21
|
+
*
|
22
|
+
*/
|
23
|
+
|
24
|
+
// Similar to `stylex.create` it takes an object of keyframes
|
25
|
+
// and returns a string after hashing it.
|
26
|
+
//
|
27
|
+
// It also expands shorthand properties to maintain parity with
|
28
|
+
// `stylex.create`.
|
29
|
+
function styleXKeyframes(frames) {
|
30
|
+
let {
|
31
|
+
stylexSheetName = '<>',
|
32
|
+
classNamePrefix = 'x'
|
33
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
34
|
+
const expandedObject = (0, _objectUtils.objMap)(frames, frame => _objectUtils.Pipe.create(frame).pipe(expandFrameShorthands).pipe(x => (0, _objectUtils.objMapKeys)(x, _dashify.default)).pipe(x => (0, _objectUtils.objMap)(x, (value, key) => (0, _transformValue.default)(key, value))).done());
|
35
|
+
const ltrStyles = (0, _objectUtils.objMap)(expandedObject, frame => (0, _objectUtils.objMapEntry)(frame, _generateLtr.default));
|
36
|
+
const rtlStyles = (0, _objectUtils.objMap)(expandedObject, frame => (0, _objectUtils.objMapEntry)(frame, entry => (0, _generateRtl.default)(entry) ?? entry));
|
37
|
+
const ltrString = constructKeyframesObj(ltrStyles);
|
38
|
+
const rtlString = constructKeyframesObj(rtlStyles);
|
39
|
+
|
40
|
+
// This extra `-B` is kept for some idiosyncratic legacy compatibility for now.
|
41
|
+
const animationName = classNamePrefix + (0, _hash.default)(stylexSheetName + ltrString) + '-B';
|
42
|
+
const ltr = `@keyframes ${animationName}{${ltrString}}`;
|
43
|
+
const rtl = ltrString === rtlString ? null : `@keyframes ${animationName}{${rtlString}}`;
|
44
|
+
return [animationName, {
|
45
|
+
ltr,
|
46
|
+
rtl,
|
47
|
+
priority: 1
|
48
|
+
}];
|
49
|
+
}
|
50
|
+
function expandFrameShorthands(frame) {
|
51
|
+
return (0, _objectUtils.objFromEntries)((0, _objectUtils.objEntries)(frame).flatMap(pair => (0, _expandShorthands.default)(pair)));
|
52
|
+
}
|
53
|
+
|
54
|
+
// Create t
|
55
|
+
function constructKeyframesObj(frames) {
|
56
|
+
return (0, _objectUtils.objEntries)(frames).map(_ref => {
|
57
|
+
let [key, value] = _ref;
|
58
|
+
return `${key}{${(0, _objectUtils.objEntries)(value).map(_ref2 => {
|
59
|
+
let [k, v] = _ref2;
|
60
|
+
return `${k}:${v};`;
|
61
|
+
}).join('')}}`;
|
62
|
+
}).join('');
|
63
|
+
}
|
@@ -0,0 +1,58 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.default = transformValue;
|
7
|
+
var _normalizeValue = _interopRequireDefault(require("./utils/normalize-value"));
|
8
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
9
|
+
/**
|
10
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
11
|
+
*
|
12
|
+
* This source code is licensed under the MIT license found in the
|
13
|
+
* LICENSE file in the root directory of this source tree.
|
14
|
+
*
|
15
|
+
*/
|
16
|
+
|
17
|
+
/**
|
18
|
+
* Convert a CSS value in JS to the final CSS string value
|
19
|
+
*/
|
20
|
+
function transformValue(key, rawValue) {
|
21
|
+
const value = typeof rawValue === 'number' ? String(Math.round(rawValue * 10000) / 10000) + getNumberSuffix(key) : rawValue;
|
22
|
+
|
23
|
+
// content is one of the values that needs to wrapped in quotes.
|
24
|
+
// Users may write `''` without thinking about it, so we fix that.
|
25
|
+
if (key === 'content' && typeof value === 'string') {
|
26
|
+
const val = value.trim();
|
27
|
+
if (val.match(/^attr\([a-zA-Z0-9-]+\)$/)) {
|
28
|
+
return val;
|
29
|
+
}
|
30
|
+
if (!(val.startsWith('"') && val.endsWith('"') || val.startsWith("'") && val.endsWith("'"))) {
|
31
|
+
return `"${val}"`;
|
32
|
+
}
|
33
|
+
}
|
34
|
+
return (0, _normalizeValue.default)(value, key);
|
35
|
+
}
|
36
|
+
function getNumberSuffix(key) {
|
37
|
+
if (unitlessNumberProperties.has(key)) {
|
38
|
+
return '';
|
39
|
+
}
|
40
|
+
const suffix = numberPropertySuffixes[key];
|
41
|
+
if (suffix == null) {
|
42
|
+
return 'px';
|
43
|
+
} else {
|
44
|
+
return suffix;
|
45
|
+
}
|
46
|
+
}
|
47
|
+
const unitlessNumberProperties = new Set(['animationIterationCount', 'borderImageOutset', 'borderImageSlice', 'borderImageWidth', 'columnCount', 'flex',
|
48
|
+
// Unsupported
|
49
|
+
'flexGrow', 'flexPositive', 'flexShrink', 'flexOrder', 'gridRow', 'gridColumn', 'fontWeight', 'lineClamp', 'lineHeight', 'opacity', 'order', 'orphans', 'tabSize', 'widows', 'zIndex', 'fillOpacity', 'floodOpacity', 'stopOpacity', 'strokeDasharray', 'strokeDashoffset', 'strokeMiterlimit', 'strokeOpacity', 'strokeWidth']);
|
50
|
+
|
51
|
+
// List of properties that have custom suffixes for numbers
|
52
|
+
const numberPropertySuffixes = {
|
53
|
+
animationDelay: 'ms',
|
54
|
+
animationDuration: 'ms',
|
55
|
+
transitionDelay: 'ms',
|
56
|
+
transitionDuration: 'ms',
|
57
|
+
voiceDuration: 'ms'
|
58
|
+
};
|
@@ -0,0 +1,17 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.default = dashify;
|
7
|
+
/**
|
8
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
9
|
+
*
|
10
|
+
* This source code is licensed under the MIT license found in the
|
11
|
+
* LICENSE file in the root directory of this source tree.
|
12
|
+
*
|
13
|
+
*/
|
14
|
+
|
15
|
+
function dashify(str) {
|
16
|
+
return str.replace(/(^|[a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
17
|
+
}
|
@@ -0,0 +1,24 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*
|
7
|
+
*
|
8
|
+
*
|
9
|
+
*/
|
10
|
+
|
11
|
+
'use strict';
|
12
|
+
|
13
|
+
Object.defineProperty(exports, "__esModule", {
|
14
|
+
value: true
|
15
|
+
});
|
16
|
+
exports.default = generateCSSRule;
|
17
|
+
function generateCSSRule(className, decls, pseudo) {
|
18
|
+
if (pseudo === '::thumb') {
|
19
|
+
const selector = THUMB_VARIANTS.map(suffix => '.' + className + suffix).join(', ');
|
20
|
+
return `${selector}{${decls}}`;
|
21
|
+
}
|
22
|
+
return pseudo != null && pseudo[0] === '@' ? `${pseudo}{.${className}.${className}{${decls}}}` : pseudo != null && pseudo[0] === ':' ? `.${className}${pseudo}{${decls}}` : `.${className}{${decls}}`;
|
23
|
+
}
|
24
|
+
const THUMB_VARIANTS = ['::-webkit-slider-thumb', '::-moz-range-thumb', '::-ms-thumb'];
|
@@ -0,0 +1,32 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*
|
7
|
+
*
|
8
|
+
*
|
9
|
+
*/
|
10
|
+
|
11
|
+
'use strict';
|
12
|
+
|
13
|
+
Object.defineProperty(exports, "__esModule", {
|
14
|
+
value: true
|
15
|
+
});
|
16
|
+
exports.default = normalizeValue;
|
17
|
+
var _fontSizePxToRem = _interopRequireDefault(require("./normalizers/font-size-px-to-rem"));
|
18
|
+
var _leadingZero = _interopRequireDefault(require("./normalizers/leading-zero"));
|
19
|
+
var _quotes = _interopRequireDefault(require("./normalizers/quotes"));
|
20
|
+
var _timings = _interopRequireDefault(require("./normalizers/timings"));
|
21
|
+
var _whitespace = _interopRequireDefault(require("./normalizers/whitespace"));
|
22
|
+
var _zeroDimensions = _interopRequireDefault(require("./normalizers/zero-dimensions"));
|
23
|
+
var _detectUnclosedFns = _interopRequireDefault(require("./normalizers/detect-unclosed-fns"));
|
24
|
+
var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
|
25
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
26
|
+
// `Timings` should be before `LeadingZero`, because it
|
27
|
+
// changes 500ms to 0.5s, then `LeadingZero` makes it .5s
|
28
|
+
const normalizers = [_detectUnclosedFns.default, _whitespace.default, _timings.default, _zeroDimensions.default, _leadingZero.default, _quotes.default, _fontSizePxToRem.default];
|
29
|
+
function normalizeValue(value, key) {
|
30
|
+
const parsedAST = (0, _postcssValueParser.default)(value);
|
31
|
+
return normalizers.reduce((ast, fn) => fn(ast, key), parsedAST).toString();
|
32
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*
|
7
|
+
*
|
8
|
+
*/
|
9
|
+
|
10
|
+
'use strict';
|
11
|
+
|
12
|
+
Object.defineProperty(exports, "__esModule", {
|
13
|
+
value: true
|
14
|
+
});
|
15
|
+
exports.default = detectUnclosedFns;
|
16
|
+
var messages = _interopRequireWildcard(require("../../messages"));
|
17
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
18
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
19
|
+
/**
|
20
|
+
* Remove leading zeros from numbers
|
21
|
+
*/
|
22
|
+
function detectUnclosedFns(ast, _) {
|
23
|
+
ast.walk(node => {
|
24
|
+
if (node.type === 'function' && node.unclosed) {
|
25
|
+
throw new Error(messages.LINT_UNCLOSED_FUNCTION);
|
26
|
+
}
|
27
|
+
});
|
28
|
+
return ast;
|
29
|
+
}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*
|
7
|
+
*
|
8
|
+
*/
|
9
|
+
|
10
|
+
'use strict';
|
11
|
+
|
12
|
+
const parser = require('postcss-value-parser');
|
13
|
+
const ROOT_FONT_SIZE = 16;
|
14
|
+
|
15
|
+
/**
|
16
|
+
* Convert font sizes from absolute unit `px` to relative unit `rem`.
|
17
|
+
* This will allow developers to continue thinking and using what's familiar
|
18
|
+
* while we output font sizes that are adjustable
|
19
|
+
*/
|
20
|
+
module.exports = function convertFontSizeToRem(ast, key) {
|
21
|
+
if (key !== 'fontSize') {
|
22
|
+
return ast;
|
23
|
+
}
|
24
|
+
ast.walk(node => {
|
25
|
+
if (node.type !== 'word') {
|
26
|
+
return;
|
27
|
+
}
|
28
|
+
const dimension = parser.unit(node.value);
|
29
|
+
if (dimension && dimension.unit === 'px') {
|
30
|
+
node.value = `${parseFloat(dimension.number) / ROOT_FONT_SIZE}rem`;
|
31
|
+
}
|
32
|
+
});
|
33
|
+
return ast;
|
34
|
+
};
|
@@ -0,0 +1,36 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*
|
7
|
+
*
|
8
|
+
*/
|
9
|
+
|
10
|
+
'use strict';
|
11
|
+
|
12
|
+
Object.defineProperty(exports, "__esModule", {
|
13
|
+
value: true
|
14
|
+
});
|
15
|
+
exports.default = normalizeLeadingZero;
|
16
|
+
var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
|
17
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
18
|
+
/**
|
19
|
+
* Remove leading zeros from numbers
|
20
|
+
*/
|
21
|
+
function normalizeLeadingZero(ast, _) {
|
22
|
+
ast.walk(node => {
|
23
|
+
if (node.type !== 'word') {
|
24
|
+
return;
|
25
|
+
}
|
26
|
+
const value = Number.parseFloat(node.value);
|
27
|
+
if (Number.isNaN(value)) {
|
28
|
+
return;
|
29
|
+
}
|
30
|
+
const dimension = _postcssValueParser.default.unit(node.value);
|
31
|
+
if (value < 1 && value >= 0) {
|
32
|
+
node.value = value.toString().replace('0.', '.') + (dimension ? dimension.unit : '');
|
33
|
+
}
|
34
|
+
});
|
35
|
+
return ast;
|
36
|
+
}
|
@@ -0,0 +1,30 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*
|
7
|
+
*
|
8
|
+
*
|
9
|
+
*/
|
10
|
+
|
11
|
+
'use strict';
|
12
|
+
|
13
|
+
/**
|
14
|
+
* Make empty strings use consistent double quotes
|
15
|
+
*/
|
16
|
+
Object.defineProperty(exports, "__esModule", {
|
17
|
+
value: true
|
18
|
+
});
|
19
|
+
exports.default = normalizeQuotes;
|
20
|
+
function normalizeQuotes(ast, _) {
|
21
|
+
ast.walk(node => {
|
22
|
+
if (node.type !== 'string') {
|
23
|
+
return;
|
24
|
+
}
|
25
|
+
if (node.value === '') {
|
26
|
+
node.quote = '"';
|
27
|
+
}
|
28
|
+
});
|
29
|
+
return ast;
|
30
|
+
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*
|
7
|
+
*
|
8
|
+
*
|
9
|
+
*/
|
10
|
+
|
11
|
+
'use strict';
|
12
|
+
|
13
|
+
Object.defineProperty(exports, "__esModule", {
|
14
|
+
value: true
|
15
|
+
});
|
16
|
+
exports.default = normalizeTimings;
|
17
|
+
var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
|
18
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
19
|
+
/**
|
20
|
+
* Turn millisecond values to seconds (shorter), except when < 10ms
|
21
|
+
*/
|
22
|
+
|
23
|
+
function normalizeTimings(ast, _) {
|
24
|
+
ast.walk(node => {
|
25
|
+
if (node.type !== 'word') {
|
26
|
+
return;
|
27
|
+
}
|
28
|
+
const value = Number.parseFloat(node.value);
|
29
|
+
if (Number.isNaN(value)) {
|
30
|
+
return;
|
31
|
+
}
|
32
|
+
const dimension = _postcssValueParser.default.unit(node.value);
|
33
|
+
if (!dimension || dimension.unit !== 'ms' || value < 10) {
|
34
|
+
return;
|
35
|
+
}
|
36
|
+
node.value = value / 1000 + 's';
|
37
|
+
});
|
38
|
+
return ast;
|
39
|
+
}
|
@@ -0,0 +1,59 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*
|
7
|
+
*
|
8
|
+
*
|
9
|
+
*/
|
10
|
+
|
11
|
+
'use strict';
|
12
|
+
|
13
|
+
/**
|
14
|
+
* Use single spaces and remove spaces when not needed: around functions,
|
15
|
+
* commas. But preserve spece around + and - as they are required in calc()
|
16
|
+
*/
|
17
|
+
Object.defineProperty(exports, "__esModule", {
|
18
|
+
value: true
|
19
|
+
});
|
20
|
+
exports.default = normalizeWhitespace;
|
21
|
+
function normalizeWhitespace(ast, _) {
|
22
|
+
// trim
|
23
|
+
if (ast.nodes[0].type === 'space') {
|
24
|
+
ast.nodes.shift();
|
25
|
+
}
|
26
|
+
if (ast.nodes[ast.nodes.length - 1].type === 'space') {
|
27
|
+
ast.nodes.pop();
|
28
|
+
}
|
29
|
+
ast.walk((node, idx) => {
|
30
|
+
switch (node.type) {
|
31
|
+
case 'space':
|
32
|
+
{
|
33
|
+
node.value = ' ';
|
34
|
+
break;
|
35
|
+
}
|
36
|
+
case 'div':
|
37
|
+
case 'function':
|
38
|
+
{
|
39
|
+
node.before = '';
|
40
|
+
node.after = '';
|
41
|
+
break;
|
42
|
+
}
|
43
|
+
case 'word':
|
44
|
+
{
|
45
|
+
if (node.value === '!important') {
|
46
|
+
if (ast.nodes[idx - 1] && ast.nodes[idx - 1].type === 'space') {
|
47
|
+
ast.nodes.splice(idx - 1, 1);
|
48
|
+
}
|
49
|
+
}
|
50
|
+
break;
|
51
|
+
}
|
52
|
+
default:
|
53
|
+
{
|
54
|
+
break;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
});
|
58
|
+
return ast;
|
59
|
+
}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*
|
7
|
+
*
|
8
|
+
*/
|
9
|
+
|
10
|
+
'use strict';
|
11
|
+
|
12
|
+
Object.defineProperty(exports, "__esModule", {
|
13
|
+
value: true
|
14
|
+
});
|
15
|
+
exports.default = normalizeZeroDimensions;
|
16
|
+
var _postcssValueParser = _interopRequireDefault(require("postcss-value-parser"));
|
17
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
18
|
+
const angles = ['deg', 'grad', 'turn', 'rad'];
|
19
|
+
const timings = ['ms', 's'];
|
20
|
+
|
21
|
+
/**
|
22
|
+
* Remove units in zero values, except when required: in angles and timings,
|
23
|
+
* in which case make them consistent 0deg and 0s.
|
24
|
+
*/
|
25
|
+
|
26
|
+
function normalizeZeroDimensions(ast, _) {
|
27
|
+
ast.walk(node => {
|
28
|
+
if (node.type !== 'word') {
|
29
|
+
return;
|
30
|
+
}
|
31
|
+
const dimension = _postcssValueParser.default.unit(node.value);
|
32
|
+
if (!dimension || dimension.number !== '0') {
|
33
|
+
return;
|
34
|
+
}
|
35
|
+
if (angles.indexOf(dimension.unit) !== -1) {
|
36
|
+
node.value = '0deg';
|
37
|
+
} else if (timings.indexOf(dimension.unit) !== -1) {
|
38
|
+
node.value = '0s';
|
39
|
+
} else {
|
40
|
+
node.value = '0';
|
41
|
+
}
|
42
|
+
});
|
43
|
+
return ast;
|
44
|
+
}
|