@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
package/.babelrc
ADDED
package/.yarnrc.yml
ADDED
package/dist/babel.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = _default;
|
|
7
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
|
9
|
+
var _babel = require("./utils/babel.js");
|
|
10
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
11
|
+
var libRoot = _path["default"].dirname(__filename);
|
|
12
|
+
|
|
13
|
+
// Cache for loaded stylesheets
|
|
14
|
+
var stylesheetCache = new Map();
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Load pre-computed stylesheet JSON (generated by Metro transformer)
|
|
18
|
+
*/
|
|
19
|
+
function loadStylesheet(cssPath) {
|
|
20
|
+
if (stylesheetCache.has(cssPath)) {
|
|
21
|
+
return stylesheetCache.get(cssPath);
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
// Try the exported-stylesheet.json from lib directory
|
|
25
|
+
var exportedPath = _path["default"].join(libRoot, "exported-stylesheet.json");
|
|
26
|
+
if (_fs["default"].existsSync(exportedPath)) {
|
|
27
|
+
var content = _fs["default"].readFileSync(exportedPath, "utf-8");
|
|
28
|
+
var stylesheet = JSON.parse(content);
|
|
29
|
+
stylesheetCache.set(cssPath, stylesheet);
|
|
30
|
+
return stylesheet;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Try .json next to the CSS file
|
|
34
|
+
var cssJsonPath = "".concat(cssPath, ".json");
|
|
35
|
+
if (_fs["default"].existsSync(cssJsonPath)) {
|
|
36
|
+
var _content = _fs["default"].readFileSync(cssJsonPath, "utf-8");
|
|
37
|
+
var _stylesheet = JSON.parse(_content);
|
|
38
|
+
stylesheetCache.set(cssPath, _stylesheet);
|
|
39
|
+
return _stylesheet;
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
} catch (_unused) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function _default(_ref) {
|
|
47
|
+
var t = _ref.types;
|
|
48
|
+
return {
|
|
49
|
+
name: "@colorye/react-native-css",
|
|
50
|
+
visitor: {
|
|
51
|
+
Program: {
|
|
52
|
+
enter: function enter(path, state) {
|
|
53
|
+
if (!this.opts.css) {
|
|
54
|
+
console.warn("No css file provided");
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
var filename = state.file.opts.filename;
|
|
58
|
+
var options = {
|
|
59
|
+
paths: this.opts.paths || [],
|
|
60
|
+
excludes: this.opts.excludes || [],
|
|
61
|
+
css: this.opts.css
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// Normalize filename for cross-platform regex matching
|
|
65
|
+
var normalizedFilename = filename.replace(/\\/g, "/");
|
|
66
|
+
var regex = options.paths.map(function (p) {
|
|
67
|
+
return new RegExp(p);
|
|
68
|
+
});
|
|
69
|
+
var excludesRegex = options.excludes.map(function (p) {
|
|
70
|
+
return new RegExp(p);
|
|
71
|
+
});
|
|
72
|
+
if (!regex.some(function (re) {
|
|
73
|
+
return normalizedFilename.match(re) || filename.match(re);
|
|
74
|
+
}) || excludesRegex.some(function (re) {
|
|
75
|
+
return normalizedFilename.match(re) || filename.match(re);
|
|
76
|
+
})) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
state.enabled = true;
|
|
80
|
+
|
|
81
|
+
// Load pre-computed stylesheet for static inlining
|
|
82
|
+
state.stylesheetData = loadStylesheet(options.css);
|
|
83
|
+
|
|
84
|
+
// Track which runtime helpers we need
|
|
85
|
+
state.needsGetStyle = false;
|
|
86
|
+
state.needsGetInheritStyle = false;
|
|
87
|
+
state.needsMergeStyles = false;
|
|
88
|
+
|
|
89
|
+
// Prepare runtime helper declarations
|
|
90
|
+
state.stylesheetId = path.scope.generateUidIdentifier();
|
|
91
|
+
state.stylesheet = t.variableDeclaration("var", [t.variableDeclarator(state.stylesheetId, t.callExpression(t.identifier("require"), [t.stringLiteral(options.css)]))]);
|
|
92
|
+
state.getStyleId = path.scope.generateUidIdentifier();
|
|
93
|
+
state.getStyle = t.variableDeclaration("var", [t.variableDeclarator(state.getStyleId, t.memberExpression(t.memberExpression(t.callExpression(t.identifier("require"), [t.stringLiteral(_path["default"].join(libRoot, "./transformer-runtime"))]), t.identifier("default")), t.identifier("getStyle")))]);
|
|
94
|
+
state.getInheritStyleId = path.scope.generateUidIdentifier();
|
|
95
|
+
state.getInheritStyle = t.variableDeclaration("var", [t.variableDeclarator(state.getInheritStyleId, t.memberExpression(t.memberExpression(t.callExpression(t.identifier("require"), [t.stringLiteral(_path["default"].join(libRoot, "./transformer-runtime"))]), t.identifier("default")), t.identifier("getInheritStyle")))]);
|
|
96
|
+
state.mergeStylesId = path.scope.generateUidIdentifier();
|
|
97
|
+
state.mergeStyles = t.variableDeclaration("var", [t.variableDeclarator(state.mergeStylesId, t.memberExpression(t.memberExpression(t.callExpression(t.identifier("require"), [t.stringLiteral(_path["default"].join(libRoot, "./transformer-runtime"))]), t.identifier("default")), t.identifier("mergeStyles")))]);
|
|
98
|
+
},
|
|
99
|
+
exit: function exit(path, state) {
|
|
100
|
+
if (!state.enabled) return;
|
|
101
|
+
var helpers = [];
|
|
102
|
+
|
|
103
|
+
// Only inject helpers that are actually used
|
|
104
|
+
if (state.needsGetStyle) {
|
|
105
|
+
helpers.push(state.stylesheet);
|
|
106
|
+
helpers.push(state.getStyle);
|
|
107
|
+
}
|
|
108
|
+
if (state.needsGetInheritStyle) {
|
|
109
|
+
helpers.push(state.getInheritStyle);
|
|
110
|
+
}
|
|
111
|
+
if (state.needsMergeStyles) {
|
|
112
|
+
helpers.push(state.mergeStyles);
|
|
113
|
+
}
|
|
114
|
+
if (helpers.length === 0) return;
|
|
115
|
+
var lastImport = path.get("body").filter(function (p) {
|
|
116
|
+
return (0, _babel.isImportOrRequire)(p);
|
|
117
|
+
}).pop();
|
|
118
|
+
helpers.reverse().forEach(function (node) {
|
|
119
|
+
return lastImport ? lastImport.insertAfter(node) : path.unshiftContainer("body", node);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
JSXElement: function JSXElement(path, state) {
|
|
124
|
+
if (!state.enabled) return;
|
|
125
|
+
if ((0, _babel.isFragmentElement)(t, path.node.openingElement.name)) return;
|
|
126
|
+
var openingElement = path.node.openingElement;
|
|
127
|
+
var isRootLevel = (0, _babel.isRootLevelJSXElement)(path);
|
|
128
|
+
|
|
129
|
+
// Inject inheritStyle for root-level elements
|
|
130
|
+
if (isRootLevel) {
|
|
131
|
+
var inheritStyle = openingElement.attributes.find(function (attr) {
|
|
132
|
+
var _attr$name;
|
|
133
|
+
return ((_attr$name = attr.name) === null || _attr$name === void 0 ? void 0 : _attr$name.name) === "inheritStyle";
|
|
134
|
+
});
|
|
135
|
+
if (!inheritStyle) {
|
|
136
|
+
var inheritStyleExpressions = (0, _babel.getRootInheritStyleExpression)(path, t);
|
|
137
|
+
openingElement.attributes.push(t.jsxAttribute(t.jsxIdentifier("inheritStyle"), t.jsxExpressionContainer(inheritStyleExpressions)));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
var shouldCombineStyles = openingElement.attributes.some(function (attr) {
|
|
141
|
+
var _attr$name2;
|
|
142
|
+
return ((_attr$name2 = attr.name) === null || _attr$name2 === void 0 ? void 0 : _attr$name2.name) === "__combinedStyles";
|
|
143
|
+
});
|
|
144
|
+
if (shouldCombineStyles) {
|
|
145
|
+
var styleExpressions;
|
|
146
|
+
|
|
147
|
+
// Try static inlining first (if stylesheet loaded and class is static)
|
|
148
|
+
if (state.stylesheetData) {
|
|
149
|
+
var staticInfo = (0, _babel.tryGetStaticStyleInfo)(path, state, t);
|
|
150
|
+
if (staticInfo) {
|
|
151
|
+
// Static class! Use lightweight mergeStyles
|
|
152
|
+
styleExpressions = (0, _babel.getStaticMergeExpression)(path, state, t, staticInfo);
|
|
153
|
+
state.needsMergeStyles = true;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (!styleExpressions) {
|
|
157
|
+
// Fall back to full runtime getStyle
|
|
158
|
+
state.needsGetStyle = true;
|
|
159
|
+
styleExpressions = (0, _babel.getStyleExpression)(path, state, t);
|
|
160
|
+
}
|
|
161
|
+
var style = openingElement.attributes.find(function (attr) {
|
|
162
|
+
var _attr$name3;
|
|
163
|
+
return ((_attr$name3 = attr.name) === null || _attr$name3 === void 0 ? void 0 : _attr$name3.name) === "style";
|
|
164
|
+
});
|
|
165
|
+
if (style) {
|
|
166
|
+
style.value = t.jsxExpressionContainer(styleExpressions);
|
|
167
|
+
} else {
|
|
168
|
+
openingElement.attributes.push(t.jsxAttribute(t.jsxIdentifier("style"), t.jsxExpressionContainer(styleExpressions)));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Inject inheritStyle for child elements
|
|
173
|
+
path.traverse({
|
|
174
|
+
JSXElement: function JSXElement(childPath) {
|
|
175
|
+
var childOpeningElement = childPath.node.openingElement;
|
|
176
|
+
if ((0, _babel.isFragmentElement)(t, childOpeningElement.name)) {
|
|
177
|
+
childPath.skip();
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Child elements need getInheritStyle for propagation
|
|
182
|
+
state.needsGetInheritStyle = true;
|
|
183
|
+
// getInheritStyle depends on getStyle
|
|
184
|
+
state.needsGetStyle = true;
|
|
185
|
+
var inheritStyleExpressions = (0, _babel.getInheritStyleExpression)(path, state, t);
|
|
186
|
+
var inheritStyle = childOpeningElement.attributes.find(function (attr) {
|
|
187
|
+
var _attr$name4;
|
|
188
|
+
return ((_attr$name4 = attr.name) === null || _attr$name4 === void 0 ? void 0 : _attr$name4.name) === "inheritStyle";
|
|
189
|
+
});
|
|
190
|
+
if (inheritStyle) {
|
|
191
|
+
inheritStyle.value = t.jsxExpressionContainer(inheritStyleExpressions);
|
|
192
|
+
} else {
|
|
193
|
+
childOpeningElement.attributes.push(t.jsxAttribute(t.jsxIdentifier("inheritStyle"), t.jsxExpressionContainer(inheritStyleExpressions)));
|
|
194
|
+
}
|
|
195
|
+
childPath.skip();
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
}
|