@colorye/react-native-css 0.2.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 +3 -0
- package/.yarnrc.yml +8 -0
- package/dist/babel.js +201 -0
- package/dist/features/build-transform.js +562 -0
- package/dist/features/css-calc.js +378 -0
- package/dist/features/css-media.js +77 -0
- package/dist/features/css-transform.js +398 -0
- package/dist/features/css-vars.js +107 -0
- package/dist/features/stylesheet.js +94 -0
- package/dist/index.js +158 -0
- package/dist/transformer-runtime.js +310 -0
- package/dist/utils/babel.js +336 -0
- package/dist/utils/css.js +239 -0
- package/dist/utils/helper.js +11 -0
- package/package.json +26 -0
- package/src/babel.js +274 -0
- package/src/features/build-transform.js +524 -0
- package/src/features/css-calc.js +326 -0
- package/src/features/css-media.js +78 -0
- package/src/features/css-transform.js +419 -0
- package/src/features/css-vars.js +93 -0
- package/src/features/stylesheet.js +82 -0
- package/src/index.js +123 -0
- package/src/transformer-runtime.js +287 -0
- package/src/utils/babel.js +354 -0
- package/src/utils/css.js +254 -0
- package/src/utils/helper.js +3 -0
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.areAllClassesStatic = areAllClassesStatic;
|
|
7
|
+
exports.computeStaticStyles = computeStaticStyles;
|
|
8
|
+
exports.getInheritStyleExpression = getInheritStyleExpression;
|
|
9
|
+
exports.getRootInheritStyleExpression = getRootInheritStyleExpression;
|
|
10
|
+
exports.getStaticClassNameValue = getStaticClassNameValue;
|
|
11
|
+
exports.getStaticMergeExpression = getStaticMergeExpression;
|
|
12
|
+
exports.getStaticStylesFromClass = getStaticStylesFromClass;
|
|
13
|
+
exports.getStyleExpression = getStyleExpression;
|
|
14
|
+
exports.isClassStatic = isClassStatic;
|
|
15
|
+
exports.isFragmentElement = isFragmentElement;
|
|
16
|
+
exports.isImportOrRequire = isImportOrRequire;
|
|
17
|
+
exports.isRootLevelJSXElement = isRootLevelJSXElement;
|
|
18
|
+
exports.isStaticClassName = isStaticClassName;
|
|
19
|
+
exports.objectToAST = objectToAST;
|
|
20
|
+
exports.tryGetStaticStyleInfo = tryGetStaticStyleInfo;
|
|
21
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
|
|
22
|
+
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
23
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
24
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
25
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
26
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
27
|
+
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
28
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
29
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
30
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
31
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
32
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
33
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
34
|
+
function isImportOrRequire(statement) {
|
|
35
|
+
var _statement$node;
|
|
36
|
+
return statement.isImportDeclaration() || ((_statement$node = statement.node) === null || _statement$node === void 0 || (_statement$node = _statement$node.declarations) === null || _statement$node === void 0 || (_statement$node = _statement$node[0]) === null || _statement$node === void 0 || (_statement$node = _statement$node.init) === null || _statement$node === void 0 || (_statement$node = _statement$node.callee) === null || _statement$node === void 0 ? void 0 : _statement$node.name) === "require";
|
|
37
|
+
}
|
|
38
|
+
function isFragmentElement(t, elementName) {
|
|
39
|
+
return t.isJSXIdentifier(elementName) && (elementName.name === "Fragment" || elementName.name === "React.Fragment") || t.isJSXMemberExpression(elementName) && elementName.object.name === "React" && elementName.property.name === "Fragment" || t.isJSXIdentifier(elementName, {
|
|
40
|
+
name: ""
|
|
41
|
+
}) // JSX shorthand fragment
|
|
42
|
+
;
|
|
43
|
+
}
|
|
44
|
+
function isRootLevelJSXElement(path) {
|
|
45
|
+
var currentPath = path;
|
|
46
|
+
while (currentPath.parentPath) {
|
|
47
|
+
var parent = currentPath.parentPath;
|
|
48
|
+
if (parent.isReturnStatement()) return true;
|
|
49
|
+
if (parent.isArrowFunctionExpression() && parent.node.body === currentPath.node) return true;
|
|
50
|
+
if (parent.isJSXElement() && parent !== path) return false;
|
|
51
|
+
if (parent.isConditionalExpression() || parent.isLogicalExpression() || parent.isParenthesizedExpression()) {
|
|
52
|
+
currentPath = parent;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
currentPath = parent;
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ============================================================================
|
|
61
|
+
// Static style detection helpers
|
|
62
|
+
// ============================================================================
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Check if className is a static string literal
|
|
66
|
+
*/
|
|
67
|
+
function isStaticClassName(t, classNameAttr) {
|
|
68
|
+
if (!classNameAttr) return false;
|
|
69
|
+
if (t.isStringLiteral(classNameAttr.value)) return true;
|
|
70
|
+
if (t.isJSXExpressionContainer(classNameAttr.value) && t.isStringLiteral(classNameAttr.value.expression)) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Get static className string value
|
|
78
|
+
*/
|
|
79
|
+
function getStaticClassNameValue(t, classNameAttr) {
|
|
80
|
+
if (!classNameAttr) return null;
|
|
81
|
+
if (t.isStringLiteral(classNameAttr.value)) return classNameAttr.value.value;
|
|
82
|
+
if (t.isJSXExpressionContainer(classNameAttr.value) && t.isStringLiteral(classNameAttr.value.expression)) {
|
|
83
|
+
return classNameAttr.value.expression.value;
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Check if a class is fully static (no _dynamic property)
|
|
90
|
+
*/
|
|
91
|
+
function isClassStatic(stylesheet, className) {
|
|
92
|
+
var decl = stylesheet[className];
|
|
93
|
+
if (!decl) return true; // Non-existent class is "static" (no-op)
|
|
94
|
+
if (decl._dynamic && Object.keys(decl._dynamic).length > 0) return false;
|
|
95
|
+
if (decl._static) return true; // Has _static wrapper
|
|
96
|
+
// Check if it's a plain object (fully static, no wrapper)
|
|
97
|
+
return _typeof(decl) === "object" && !decl._dynamic;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Get static styles from a class declaration
|
|
102
|
+
*/
|
|
103
|
+
function getStaticStylesFromClass(stylesheet, className) {
|
|
104
|
+
var decl = stylesheet[className];
|
|
105
|
+
if (!decl) return {};
|
|
106
|
+
|
|
107
|
+
// If has _static wrapper, use that
|
|
108
|
+
if (decl._static) return _objectSpread({}, decl._static);
|
|
109
|
+
|
|
110
|
+
// If plain object (fully static), use directly
|
|
111
|
+
if (_typeof(decl) === "object" && !decl._dynamic) {
|
|
112
|
+
// Filter out any metadata keys
|
|
113
|
+
var result = {};
|
|
114
|
+
for (var _i = 0, _Object$entries = Object.entries(decl); _i < _Object$entries.length; _i++) {
|
|
115
|
+
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
116
|
+
key = _Object$entries$_i[0],
|
|
117
|
+
value = _Object$entries$_i[1];
|
|
118
|
+
if (!key.startsWith("_") && !key.startsWith("@media")) {
|
|
119
|
+
result[key] = value;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
return {};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Check if all classes in className string are static
|
|
129
|
+
*/
|
|
130
|
+
function areAllClassesStatic(stylesheet, classNameValue) {
|
|
131
|
+
if (!classNameValue || !stylesheet) return false;
|
|
132
|
+
var classes = classNameValue.trim().split(/\s+/).filter(Boolean);
|
|
133
|
+
return classes.every(function (cls) {
|
|
134
|
+
return isClassStatic(stylesheet, cls);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Compute merged static styles from className string
|
|
140
|
+
*/
|
|
141
|
+
function computeStaticStyles(stylesheet, classNameValue) {
|
|
142
|
+
if (!classNameValue || !stylesheet) return {};
|
|
143
|
+
var classes = classNameValue.trim().split(/\s+/).filter(Boolean);
|
|
144
|
+
var result = {};
|
|
145
|
+
var _iterator = _createForOfIteratorHelper(classes),
|
|
146
|
+
_step;
|
|
147
|
+
try {
|
|
148
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
149
|
+
var cls = _step.value;
|
|
150
|
+
var styles = getStaticStylesFromClass(stylesheet, cls);
|
|
151
|
+
result = _objectSpread(_objectSpread({}, result), styles);
|
|
152
|
+
}
|
|
153
|
+
} catch (err) {
|
|
154
|
+
_iterator.e(err);
|
|
155
|
+
} finally {
|
|
156
|
+
_iterator.f();
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Convert JavaScript object to Babel AST
|
|
163
|
+
*/
|
|
164
|
+
function objectToAST(t, obj) {
|
|
165
|
+
if (obj === null || obj === undefined) return t.nullLiteral();
|
|
166
|
+
if (Array.isArray(obj)) {
|
|
167
|
+
return t.arrayExpression(obj.map(function (item) {
|
|
168
|
+
return objectToAST(t, item);
|
|
169
|
+
}));
|
|
170
|
+
}
|
|
171
|
+
if (_typeof(obj) === "object") {
|
|
172
|
+
return t.objectExpression(Object.entries(obj).map(function (_ref) {
|
|
173
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
174
|
+
key = _ref2[0],
|
|
175
|
+
value = _ref2[1];
|
|
176
|
+
return t.objectProperty(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? t.identifier(key) : t.stringLiteral(key), objectToAST(t, value));
|
|
177
|
+
}));
|
|
178
|
+
}
|
|
179
|
+
if (typeof obj === "string") return t.stringLiteral(obj);
|
|
180
|
+
if (typeof obj === "number") return t.numericLiteral(obj);
|
|
181
|
+
if (typeof obj === "boolean") return t.booleanLiteral(obj);
|
|
182
|
+
return t.nullLiteral();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Try to get static style info for an element
|
|
187
|
+
* Returns { staticStyles, hasInheritStyle, inlineStyleExpr } or null if not static
|
|
188
|
+
*/
|
|
189
|
+
function tryGetStaticStyleInfo(path, state, t) {
|
|
190
|
+
var openingElement = path.node.openingElement;
|
|
191
|
+
var classNameAttr = openingElement.attributes.find(function (attr) {
|
|
192
|
+
var _attr$name;
|
|
193
|
+
return t.isJSXAttribute(attr) && ((_attr$name = attr.name) === null || _attr$name === void 0 ? void 0 : _attr$name.name) === "className";
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
// Check if className is static
|
|
197
|
+
if (!isStaticClassName(t, classNameAttr)) return null;
|
|
198
|
+
|
|
199
|
+
// Get stylesheet
|
|
200
|
+
var stylesheet = state.stylesheetData;
|
|
201
|
+
if (!stylesheet) return null;
|
|
202
|
+
var classNameValue = getStaticClassNameValue(t, classNameAttr);
|
|
203
|
+
if (!classNameValue) return null;
|
|
204
|
+
|
|
205
|
+
// Check if all classes are fully static
|
|
206
|
+
if (!areAllClassesStatic(stylesheet, classNameValue)) return null;
|
|
207
|
+
|
|
208
|
+
// Compute static styles from className
|
|
209
|
+
var staticStyles = computeStaticStyles(stylesheet, classNameValue);
|
|
210
|
+
|
|
211
|
+
// Get inheritStyle attribute (for mergeStyles call)
|
|
212
|
+
var inheritStyleAttr = openingElement.attributes.find(function (attr) {
|
|
213
|
+
var _attr$name2;
|
|
214
|
+
return t.isJSXAttribute(attr) && ((_attr$name2 = attr.name) === null || _attr$name2 === void 0 ? void 0 : _attr$name2.name) === "inheritStyle";
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// Get style attribute
|
|
218
|
+
var styleAttr = openingElement.attributes.find(function (attr) {
|
|
219
|
+
var _attr$name3;
|
|
220
|
+
return t.isJSXAttribute(attr) && ((_attr$name3 = attr.name) === null || _attr$name3 === void 0 ? void 0 : _attr$name3.name) === "style";
|
|
221
|
+
});
|
|
222
|
+
return {
|
|
223
|
+
staticStyles: staticStyles,
|
|
224
|
+
inheritStyleAttr: inheritStyleAttr,
|
|
225
|
+
styleAttr: styleAttr
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Generate mergeStyles(inheritStyle, staticStyles, inlineStyle) expression
|
|
231
|
+
*/
|
|
232
|
+
function getStaticMergeExpression(path, state, t, staticInfo) {
|
|
233
|
+
var _inheritStyleAttr$val, _styleAttr$value;
|
|
234
|
+
var staticStyles = staticInfo.staticStyles,
|
|
235
|
+
inheritStyleAttr = staticInfo.inheritStyleAttr,
|
|
236
|
+
styleAttr = staticInfo.styleAttr;
|
|
237
|
+
|
|
238
|
+
// Get inheritStyle expression
|
|
239
|
+
var propInheritStyle = getMemoizedInheritStyleExpression(path, t);
|
|
240
|
+
var inheritStyleExpr = inheritStyleAttr === null || inheritStyleAttr === void 0 || (_inheritStyleAttr$val = inheritStyleAttr.value) === null || _inheritStyleAttr$val === void 0 ? void 0 : _inheritStyleAttr$val.expression;
|
|
241
|
+
|
|
242
|
+
// Build inheritStyle array: [props.inheritStyle, explicitInheritStyle]
|
|
243
|
+
var inheritArg;
|
|
244
|
+
if (propInheritStyle || inheritStyleExpr) {
|
|
245
|
+
inheritArg = t.arrayExpression([propInheritStyle || t.nullLiteral(), inheritStyleExpr || t.nullLiteral()]);
|
|
246
|
+
} else {
|
|
247
|
+
inheritArg = t.nullLiteral();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Static styles as inline object
|
|
251
|
+
var staticArg = Object.keys(staticStyles).length > 0 ? objectToAST(t, staticStyles) : t.nullLiteral();
|
|
252
|
+
|
|
253
|
+
// Inline style expression
|
|
254
|
+
var inlineArg = (styleAttr === null || styleAttr === void 0 || (_styleAttr$value = styleAttr.value) === null || _styleAttr$value === void 0 ? void 0 : _styleAttr$value.expression) || t.nullLiteral();
|
|
255
|
+
|
|
256
|
+
// mergeStyles(inheritStyle, staticStyles, inlineStyle)
|
|
257
|
+
return t.callExpression(state.mergeStylesId, [inheritArg, staticArg, inlineArg]);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// ============================================================================
|
|
261
|
+
// InheritStyle helpers
|
|
262
|
+
// ============================================================================
|
|
263
|
+
|
|
264
|
+
var inheritStyleMemo = new WeakMap();
|
|
265
|
+
function getMemoizedInheritStyleExpression(path, t) {
|
|
266
|
+
var currentPath = path;
|
|
267
|
+
var funcPath = null;
|
|
268
|
+
while (currentPath && !funcPath) {
|
|
269
|
+
if (currentPath.isFunction()) {
|
|
270
|
+
funcPath = currentPath;
|
|
271
|
+
break;
|
|
272
|
+
}
|
|
273
|
+
currentPath = currentPath.parentPath;
|
|
274
|
+
}
|
|
275
|
+
if (!funcPath) return undefined;
|
|
276
|
+
if (inheritStyleMemo.has(funcPath.node)) {
|
|
277
|
+
return inheritStyleMemo.get(funcPath.node);
|
|
278
|
+
}
|
|
279
|
+
var result;
|
|
280
|
+
var params = funcPath.node.params;
|
|
281
|
+
var parentPath = funcPath.parentPath;
|
|
282
|
+
if (parentPath && parentPath.isCallExpression() && parentPath.node.callee && parentPath.node.callee.type === "Identifier" && ["useMemo", "useCallback"].includes(parentPath.node.callee.name)) {
|
|
283
|
+
result = undefined;
|
|
284
|
+
} else if (params.length === 0) {
|
|
285
|
+
funcPath.node.params = [t.identifier("props")];
|
|
286
|
+
result = t.memberExpression(t.identifier("props"), t.identifier("inheritStyle"), false, false);
|
|
287
|
+
} else if (params.length > 0) {
|
|
288
|
+
var firstParam = params[0];
|
|
289
|
+
if (t.isIdentifier(firstParam)) {
|
|
290
|
+
result = t.memberExpression(firstParam, t.identifier("inheritStyle"), false, false);
|
|
291
|
+
} else if (t.isObjectPattern(firstParam)) {
|
|
292
|
+
var restElement = firstParam.properties.find(function (p) {
|
|
293
|
+
return t.isRestElement(p);
|
|
294
|
+
});
|
|
295
|
+
if (restElement) {
|
|
296
|
+
result = t.memberExpression(restElement.argument, t.identifier("inheritStyle"), false, false);
|
|
297
|
+
} else {
|
|
298
|
+
var restProp = t.restElement(t.identifier("rest"));
|
|
299
|
+
firstParam.properties.push(restProp);
|
|
300
|
+
result = t.memberExpression(t.identifier("rest"), t.identifier("inheritStyle"), false, false);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
inheritStyleMemo.set(funcPath.node, result);
|
|
305
|
+
return result;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// ============================================================================
|
|
309
|
+
// Style expression generators (for dynamic/fallback cases)
|
|
310
|
+
// ============================================================================
|
|
311
|
+
|
|
312
|
+
function getStyleExpression(path, state, t) {
|
|
313
|
+
var openingElement = path.node.openingElement;
|
|
314
|
+
var elementName = openingElement.name;
|
|
315
|
+
var propInheritStyle = getMemoizedInheritStyleExpression(path, t);
|
|
316
|
+
var inheritStyle = openingElement.attributes.find(function (attr) {
|
|
317
|
+
var _attr$name4;
|
|
318
|
+
return ((_attr$name4 = attr.name) === null || _attr$name4 === void 0 ? void 0 : _attr$name4.name) === "inheritStyle";
|
|
319
|
+
});
|
|
320
|
+
var className = openingElement.attributes.find(function (attr) {
|
|
321
|
+
var _attr$name5;
|
|
322
|
+
return ((_attr$name5 = attr.name) === null || _attr$name5 === void 0 ? void 0 : _attr$name5.name) === "className";
|
|
323
|
+
});
|
|
324
|
+
var style = openingElement.attributes.find(function (attr) {
|
|
325
|
+
var _attr$name6;
|
|
326
|
+
return ((_attr$name6 = attr.name) === null || _attr$name6 === void 0 ? void 0 : _attr$name6.name) === "style";
|
|
327
|
+
});
|
|
328
|
+
return t.callExpression(state.getStyleId, [state.stylesheetId, t.arrayExpression([t.arrayExpression([propInheritStyle || t.nullLiteral(), inheritStyle && inheritStyle.value.expression || t.nullLiteral()]), className && (t.isStringLiteral(className.value) ? t.stringLiteral(className.value.value) : className.value.expression) || t.nullLiteral(), style && style.value.expression || t.nullLiteral(), t.isJSXIdentifier(elementName) ? t.stringLiteral(elementName.name) : t.isJSXMemberExpression(elementName) ? t.stringLiteral("".concat(elementName.object.name, ".").concat(elementName.property.name)) : t.stringLiteral("Unknown")])]);
|
|
329
|
+
}
|
|
330
|
+
function getInheritStyleExpression(path, state, t) {
|
|
331
|
+
return t.callExpression(state.getInheritStyleId, [getStyleExpression(path, state, t)]);
|
|
332
|
+
}
|
|
333
|
+
function getRootInheritStyleExpression(path, t) {
|
|
334
|
+
var propInheritStyle = getMemoizedInheritStyleExpression(path, t);
|
|
335
|
+
return propInheritStyle || t.identifier("undefined");
|
|
336
|
+
}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.preprocessTailwindCss = preprocessTailwindCss;
|
|
7
|
+
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
8
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
9
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
10
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
11
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
12
|
+
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
13
|
+
function flattenLayersAndSupports(cssStr) {
|
|
14
|
+
var result = "";
|
|
15
|
+
var i = 0;
|
|
16
|
+
var len = cssStr.length;
|
|
17
|
+
while (i < len) {
|
|
18
|
+
// Skip comments
|
|
19
|
+
if (cssStr[i] === "/" && cssStr[i + 1] === "*") {
|
|
20
|
+
var commentEnd = cssStr.indexOf("*/", i + 2);
|
|
21
|
+
if (commentEnd === -1) {
|
|
22
|
+
result += cssStr.substring(i);
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
result += cssStr.substring(i, commentEnd + 2);
|
|
26
|
+
i = commentEnd + 2;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Skip strings
|
|
31
|
+
if (cssStr[i] === '"' || cssStr[i] === "'") {
|
|
32
|
+
var _char = cssStr[i];
|
|
33
|
+
var strEnd = i + 1;
|
|
34
|
+
while (strEnd < len) {
|
|
35
|
+
if (cssStr[strEnd] === "\\") {
|
|
36
|
+
strEnd += 2;
|
|
37
|
+
} else if (cssStr[strEnd] === _char) {
|
|
38
|
+
strEnd++;
|
|
39
|
+
break;
|
|
40
|
+
} else {
|
|
41
|
+
strEnd++;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
result += cssStr.substring(i, strEnd);
|
|
45
|
+
i = strEnd;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Check for @layer or @supports
|
|
50
|
+
if (cssStr[i] === "@") {
|
|
51
|
+
var remaining = cssStr.substring(i);
|
|
52
|
+
var layerMatch = remaining.match(/^@layer\s+[^{]+\{/i);
|
|
53
|
+
var supportsMatch = remaining.match(/^@supports\s+[^{]+\{/i);
|
|
54
|
+
if (layerMatch || supportsMatch) {
|
|
55
|
+
var match = layerMatch || supportsMatch;
|
|
56
|
+
var matchStr = match[0];
|
|
57
|
+
var braceCount = 1;
|
|
58
|
+
var j = i + matchStr.length;
|
|
59
|
+
var innerContent = "";
|
|
60
|
+
while (j < len && braceCount > 0) {
|
|
61
|
+
if (cssStr[j] === "/" && cssStr[j + 1] === "*") {
|
|
62
|
+
var _commentEnd = cssStr.indexOf("*/", j + 2);
|
|
63
|
+
if (_commentEnd === -1) {
|
|
64
|
+
innerContent += cssStr.substring(j);
|
|
65
|
+
j = len;
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
innerContent += cssStr.substring(j, _commentEnd + 2);
|
|
69
|
+
j = _commentEnd + 2;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (cssStr[j] === '"' || cssStr[j] === "'") {
|
|
73
|
+
var _char2 = cssStr[j];
|
|
74
|
+
var _strEnd = j + 1;
|
|
75
|
+
while (_strEnd < len) {
|
|
76
|
+
if (cssStr[_strEnd] === "\\") {
|
|
77
|
+
_strEnd += 2;
|
|
78
|
+
} else if (cssStr[_strEnd] === _char2) {
|
|
79
|
+
_strEnd++;
|
|
80
|
+
break;
|
|
81
|
+
} else {
|
|
82
|
+
_strEnd++;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
innerContent += cssStr.substring(j, _strEnd);
|
|
86
|
+
j = _strEnd;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (cssStr[j] === "{") {
|
|
90
|
+
braceCount++;
|
|
91
|
+
} else if (cssStr[j] === "}") {
|
|
92
|
+
braceCount--;
|
|
93
|
+
}
|
|
94
|
+
if (braceCount > 0) {
|
|
95
|
+
innerContent += cssStr[j];
|
|
96
|
+
}
|
|
97
|
+
j++;
|
|
98
|
+
}
|
|
99
|
+
var flattenedInner = flattenLayersAndSupports(innerContent);
|
|
100
|
+
result += flattenedInner;
|
|
101
|
+
i = j;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
result += cssStr[i];
|
|
106
|
+
i++;
|
|
107
|
+
}
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
function convertRangeMediaQueries(cssStr) {
|
|
111
|
+
var result = cssStr.replace(/\(\s*width\s*>=\s*([^)]+)\)/gi, "(min-width: $1)");
|
|
112
|
+
result = result.replace(/\(\s*width\s*>\s*([^)]+)\)/gi, "(min-width: $1)");
|
|
113
|
+
result = result.replace(/\(\s*width\s*<=\s*([^)]+)\)/gi, "(max-width: $1)");
|
|
114
|
+
result = result.replace(/\(\s*width\s*<\s*([^)]+)\)/gi, "(max-width: $1)");
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
function simplifyDoubleSelectors(cssStr) {
|
|
118
|
+
return cssStr.replace(/(\.[a-z0-9_\\\-:]+)\1/gi, "$1");
|
|
119
|
+
}
|
|
120
|
+
function linearToSrgb(c) {
|
|
121
|
+
return c > 0.0031308 ? 1.055 * Math.pow(c, 1 / 2.4) - 0.055 : 12.92 * c;
|
|
122
|
+
}
|
|
123
|
+
function oklchToRgb(l, c, h) {
|
|
124
|
+
var hRad = h * Math.PI / 180;
|
|
125
|
+
var a = c * Math.cos(hRad);
|
|
126
|
+
var b = c * Math.sin(hRad);
|
|
127
|
+
var l_ = l + 0.3963377774 * a + 0.2158037573 * b;
|
|
128
|
+
var m_ = l - 0.1055613458 * a - 0.0638541728 * b;
|
|
129
|
+
var s_ = l - 0.0894841775 * a - 1.291485548 * b;
|
|
130
|
+
var l3 = l_ * l_ * l_;
|
|
131
|
+
var m3 = m_ * m_ * m_;
|
|
132
|
+
var s3 = s_ * s_ * s_;
|
|
133
|
+
var r = +4.0767416621 * l3 - 3.3077115913 * m3 + 0.2309699292 * s3;
|
|
134
|
+
var g = -1.2684380046 * l3 + 2.6097574011 * m3 - 0.3413193965 * s3;
|
|
135
|
+
var b_rgb = -0.0041960863 * l3 - 0.7034186147 * m3 + 1.707614701 * s3;
|
|
136
|
+
r = linearToSrgb(r);
|
|
137
|
+
g = linearToSrgb(g);
|
|
138
|
+
b_rgb = linearToSrgb(b_rgb);
|
|
139
|
+
r = Math.max(0, Math.min(255, Math.round(r * 255)));
|
|
140
|
+
g = Math.max(0, Math.min(255, Math.round(g * 255)));
|
|
141
|
+
b_rgb = Math.max(0, Math.min(255, Math.round(b_rgb * 255)));
|
|
142
|
+
return {
|
|
143
|
+
r: r,
|
|
144
|
+
g: g,
|
|
145
|
+
b: b_rgb
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function convertOklchToRgbOrHex(cssStr) {
|
|
149
|
+
var oklchRegex = /oklch\(\s*([\d.]+%?)\s+([\d.]+)\s+([\d.]+)(?:\s*\/\s*([\d.]+%?))?\s*\)/gi;
|
|
150
|
+
return cssStr.replace(oklchRegex, function (match, lStr, cStr, hStr, aStr) {
|
|
151
|
+
var l = parseFloat(lStr);
|
|
152
|
+
if (lStr.includes("%")) {
|
|
153
|
+
l = l / 100;
|
|
154
|
+
}
|
|
155
|
+
var c = parseFloat(cStr);
|
|
156
|
+
var h = parseFloat(hStr);
|
|
157
|
+
var _oklchToRgb = oklchToRgb(l, c, h),
|
|
158
|
+
r = _oklchToRgb.r,
|
|
159
|
+
g = _oklchToRgb.g,
|
|
160
|
+
b = _oklchToRgb.b;
|
|
161
|
+
if (aStr) {
|
|
162
|
+
var a = parseFloat(aStr);
|
|
163
|
+
if (aStr.includes("%")) {
|
|
164
|
+
a = a / 100;
|
|
165
|
+
}
|
|
166
|
+
return "rgba(".concat(r, ", ").concat(g, ", ").concat(b, ", ").concat(a, ")");
|
|
167
|
+
}
|
|
168
|
+
return "#" + [r, g, b].map(function (x) {
|
|
169
|
+
return x.toString(16).padStart(2, "0");
|
|
170
|
+
}).join("");
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
function removeContainerClass(cssStr) {
|
|
174
|
+
var index = cssStr.indexOf(".container {");
|
|
175
|
+
if (index === -1) return cssStr;
|
|
176
|
+
var braceCount = 1;
|
|
177
|
+
var j = index + ".container {".length;
|
|
178
|
+
while (j < cssStr.length && braceCount > 0) {
|
|
179
|
+
if (cssStr[j] === "{") {
|
|
180
|
+
braceCount++;
|
|
181
|
+
} else if (cssStr[j] === "}") {
|
|
182
|
+
braceCount--;
|
|
183
|
+
}
|
|
184
|
+
j++;
|
|
185
|
+
}
|
|
186
|
+
return cssStr.substring(0, index) + cssStr.substring(j);
|
|
187
|
+
}
|
|
188
|
+
function extractPropertiesAndInjectToRoot(cssStr) {
|
|
189
|
+
var properties = {};
|
|
190
|
+
var propertyRegex = /@property\s+(--[a-zA-Z0-9_-]+)\s*\{([\s\S]*?)\}/g;
|
|
191
|
+
var match;
|
|
192
|
+
while ((match = propertyRegex.exec(cssStr)) !== null) {
|
|
193
|
+
var name = match[1];
|
|
194
|
+
var body = match[2];
|
|
195
|
+
var valMatch = body.match(/initial-value:\s*([^;]+);/);
|
|
196
|
+
if (valMatch) {
|
|
197
|
+
properties[name] = valMatch[1].trim();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
var rootRegex = /(:root|:root\s*,\s*:host)\s*\{/g;
|
|
201
|
+
var rootMatch = rootRegex.exec(cssStr);
|
|
202
|
+
if (rootMatch) {
|
|
203
|
+
var insertIndex = rootMatch.index + rootMatch[0].length;
|
|
204
|
+
var injectedProps = "\n";
|
|
205
|
+
for (var _i = 0, _Object$entries = Object.entries(properties); _i < _Object$entries.length; _i++) {
|
|
206
|
+
var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
|
|
207
|
+
_name = _Object$entries$_i[0],
|
|
208
|
+
val = _Object$entries$_i[1];
|
|
209
|
+
injectedProps += " ".concat(_name, ": ").concat(val, ";\n");
|
|
210
|
+
}
|
|
211
|
+
return cssStr.substring(0, insertIndex) + injectedProps + cssStr.substring(insertIndex);
|
|
212
|
+
}
|
|
213
|
+
return cssStr;
|
|
214
|
+
}
|
|
215
|
+
function flattenNestedAmpersandRules(cssStr) {
|
|
216
|
+
var current = cssStr;
|
|
217
|
+
var prev;
|
|
218
|
+
var nestedRuleRe = /([^{}@][^{]*?)\s*\{\s*(&[^{]*?)\s*\{([^{}]*)\}\s*\}/g;
|
|
219
|
+
do {
|
|
220
|
+
prev = current;
|
|
221
|
+
current = current.replace(nestedRuleRe, function (_match, parentSelector, childSelector, declarations) {
|
|
222
|
+
var parent = parentSelector.trim();
|
|
223
|
+
var child = childSelector.trim();
|
|
224
|
+
var merged = child.replace(/&/g, parent);
|
|
225
|
+
return "".concat(merged, " {").concat(declarations, "}");
|
|
226
|
+
});
|
|
227
|
+
} while (current !== prev);
|
|
228
|
+
return current;
|
|
229
|
+
}
|
|
230
|
+
function preprocessTailwindCss(css) {
|
|
231
|
+
var preprocessedCss = extractPropertiesAndInjectToRoot(css);
|
|
232
|
+
preprocessedCss = flattenLayersAndSupports(preprocessedCss);
|
|
233
|
+
preprocessedCss = flattenNestedAmpersandRules(preprocessedCss);
|
|
234
|
+
preprocessedCss = removeContainerClass(preprocessedCss);
|
|
235
|
+
preprocessedCss = simplifyDoubleSelectors(preprocessedCss);
|
|
236
|
+
preprocessedCss = convertOklchToRgbOrHex(preprocessedCss);
|
|
237
|
+
preprocessedCss = convertRangeMediaQueries(preprocessedCss);
|
|
238
|
+
return preprocessedCss;
|
|
239
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@colorye/react-native-css",
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"author": "Rye Nguyen <ryenguyen7411@gmail.com>",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"packageManager": "yarn@4.18.0+sha512.fcb8716fe7cd0eece141ffc18b92193a9df9204c1ba83189c288835223fc0bbe64af473bab0d5e9927a7daeb5caf2bb07eb2787cc9338ca040ea125f2a1f2f7e",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "rm -rf ./dist && babel ./src -d ./dist --copy-files"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@babel/cli": "^7.22.9",
|
|
13
|
+
"@babel/core": "^7.22.9",
|
|
14
|
+
"@babel/generator": "^7.25.0",
|
|
15
|
+
"@babel/preset-env": "^7.22.9"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"css": "^3.0.0"
|
|
19
|
+
},
|
|
20
|
+
"exports": {
|
|
21
|
+
".": "./dist/index.js",
|
|
22
|
+
"./babel": "./dist/babel.js",
|
|
23
|
+
"./dist/transformer-runtime": "./dist/transformer-runtime.js",
|
|
24
|
+
"./dist/transformer-runtime.js": "./dist/transformer-runtime.js"
|
|
25
|
+
}
|
|
26
|
+
}
|