@cssxio/babel-plugin 0.2.1 → 0.4.0

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/dist/index.cjs CHANGED
@@ -110,12 +110,6 @@ function readStaticString(path) {
110
110
  const initializer = binding.path.get("init");
111
111
  return initializer.isStringLiteral() ? initializer.node.value : null;
112
112
  }
113
- function propertyKey(key, t) {
114
- return t.isValidIdentifier(key) ? t.identifier(key) : t.stringLiteral(key);
115
- }
116
- function packedStyleExpression(style, t) {
117
- return t.valueToNode(style);
118
- }
119
113
  function objectPropertyName(property, t) {
120
114
  if (t.isIdentifier(property.key)) {
121
115
  return property.key.name;
@@ -177,6 +171,7 @@ function cssxBabelPlugin(api, options = {}) {
177
171
  finalizeFoldedProps(path, t);
178
172
  path.scope.crawl();
179
173
  markReferencedStyleCandidates(path);
174
+ materializeLiveStyleMaps(path, t);
180
175
  removeDeadStyleMaps(path);
181
176
  compactLiveStyleRecords(path);
182
177
  for (const statement of path.get("body")) {
@@ -253,17 +248,24 @@ function cssxBabelPlugin(api, options = {}) {
253
248
  for (const [className, atomicClasses] of Object.entries(result.composites)) {
254
249
  state.composites.set(className, atomicClasses);
255
250
  }
256
- const replacement = types.objectExpression(
257
- Object.entries(result.styles).map(
258
- ([key, style]) => types.objectProperty(propertyKey(key, types), packedStyleExpression(style, types))
259
- )
260
- );
261
251
  if (parent.isVariableDeclarator() && types.isIdentifier(parent.node.id)) {
262
- state.styles.set(parent.node.id.name, result.styles);
263
- state.styleCandidates.set(parent.node.id.name, result.candidates);
264
- state.styleClasses.set(parent.node.id.name, result.classNames);
252
+ const styleName = parent.node.id.name;
253
+ state.styles.set(styleName, result.styles);
254
+ state.styleCandidates.set(styleName, result.candidates);
255
+ state.styleClasses.set(styleName, result.classNames);
256
+ path.replaceWith(types.objectExpression([]));
257
+ return;
258
+ }
259
+ path.replaceWith(styleMapExpression(result.styles, types));
260
+ }
261
+ function materializeLiveStyleMaps(program, types) {
262
+ for (const [styleName, styles] of state.styles) {
263
+ const binding = program.scope.getBinding(styleName);
264
+ if (!binding?.path.isVariableDeclarator() || binding.referencePaths.length === 0) {
265
+ continue;
266
+ }
267
+ binding.path.node.init = styleMapExpression(styles, types);
265
268
  }
266
- path.replaceWith(replacement);
267
269
  }
268
270
  function transformStaticProps(path, types) {
269
271
  const styles = [];
@@ -604,6 +606,9 @@ function cssxBabelPlugin(api, options = {}) {
604
606
  return values.filter(Boolean).join(" ");
605
607
  }
606
608
  }
609
+ function styleMapExpression(styles, types) {
610
+ return types.valueToNode(styles);
611
+ }
607
612
  function withStableCompositeNames(result, fileName, anchor) {
608
613
  const styles = /* @__PURE__ */ Object.create(null);
609
614
  const classNames = /* @__PURE__ */ Object.create(null);
package/dist/index.js CHANGED
@@ -84,12 +84,6 @@ function readStaticString(path) {
84
84
  const initializer = binding.path.get("init");
85
85
  return initializer.isStringLiteral() ? initializer.node.value : null;
86
86
  }
87
- function propertyKey(key, t) {
88
- return t.isValidIdentifier(key) ? t.identifier(key) : t.stringLiteral(key);
89
- }
90
- function packedStyleExpression(style, t) {
91
- return t.valueToNode(style);
92
- }
93
87
  function objectPropertyName(property, t) {
94
88
  if (t.isIdentifier(property.key)) {
95
89
  return property.key.name;
@@ -151,6 +145,7 @@ function cssxBabelPlugin(api, options = {}) {
151
145
  finalizeFoldedProps(path, t);
152
146
  path.scope.crawl();
153
147
  markReferencedStyleCandidates(path);
148
+ materializeLiveStyleMaps(path, t);
154
149
  removeDeadStyleMaps(path);
155
150
  compactLiveStyleRecords(path);
156
151
  for (const statement of path.get("body")) {
@@ -227,17 +222,24 @@ function cssxBabelPlugin(api, options = {}) {
227
222
  for (const [className, atomicClasses] of Object.entries(result.composites)) {
228
223
  state.composites.set(className, atomicClasses);
229
224
  }
230
- const replacement = types.objectExpression(
231
- Object.entries(result.styles).map(
232
- ([key, style]) => types.objectProperty(propertyKey(key, types), packedStyleExpression(style, types))
233
- )
234
- );
235
225
  if (parent.isVariableDeclarator() && types.isIdentifier(parent.node.id)) {
236
- state.styles.set(parent.node.id.name, result.styles);
237
- state.styleCandidates.set(parent.node.id.name, result.candidates);
238
- state.styleClasses.set(parent.node.id.name, result.classNames);
226
+ const styleName = parent.node.id.name;
227
+ state.styles.set(styleName, result.styles);
228
+ state.styleCandidates.set(styleName, result.candidates);
229
+ state.styleClasses.set(styleName, result.classNames);
230
+ path.replaceWith(types.objectExpression([]));
231
+ return;
232
+ }
233
+ path.replaceWith(styleMapExpression(result.styles, types));
234
+ }
235
+ function materializeLiveStyleMaps(program, types) {
236
+ for (const [styleName, styles] of state.styles) {
237
+ const binding = program.scope.getBinding(styleName);
238
+ if (!binding?.path.isVariableDeclarator() || binding.referencePaths.length === 0) {
239
+ continue;
240
+ }
241
+ binding.path.node.init = styleMapExpression(styles, types);
239
242
  }
240
- path.replaceWith(replacement);
241
243
  }
242
244
  function transformStaticProps(path, types) {
243
245
  const styles = [];
@@ -578,6 +580,9 @@ function cssxBabelPlugin(api, options = {}) {
578
580
  return values.filter(Boolean).join(" ");
579
581
  }
580
582
  }
583
+ function styleMapExpression(styles, types) {
584
+ return types.valueToNode(styles);
585
+ }
581
586
  function withStableCompositeNames(result, fileName, anchor) {
582
587
  const styles = /* @__PURE__ */ Object.create(null);
583
588
  const classNames = /* @__PURE__ */ Object.create(null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cssxio/babel-plugin",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "Babel compiler transform for CSSX.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -27,7 +27,7 @@
27
27
  "@babel/core": "^7.28.0"
28
28
  },
29
29
  "dependencies": {
30
- "@cssxio/compiler": "0.2.0"
30
+ "@cssxio/compiler": "^0.2.0"
31
31
  },
32
32
  "scripts": {
33
33
  "build": "tsup src/index.ts --format esm,cjs --dts --external @babel/core --clean"