@teleporthq/teleport-plugin-css 0.43.0-alpha.0 → 0.43.3

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/esm/index.js CHANGED
@@ -45,20 +45,39 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
45
45
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
46
  }
47
47
  };
48
+ import { join, relative } from 'path';
48
49
  import { StringUtils, UIDLUtils } from '@teleporthq/teleport-shared';
49
- import { StyleUtils, StyleBuilders, HASTUtils, ASTUtils } from '@teleporthq/teleport-plugin-common';
50
+ import { StyleUtils, StyleBuilders, HASTUtils, HASTBuilders, ASTUtils, createBinaryExpression, } from '@teleporthq/teleport-plugin-common';
50
51
  import * as types from '@babel/types';
51
52
  import { ChunkType, FileType, PluginCSS, } from '@teleporthq/teleport-types';
52
53
  import { createStyleSheetPlugin } from './style-sheet';
54
+ import { createConditionalStatement } from './utils';
55
+ var prefixUrlPathsInCss = function (css, folderPath) {
56
+ var relativePrefix = relative(join.apply(void 0, folderPath), './');
57
+ if (!relativePrefix) {
58
+ return css;
59
+ }
60
+ return css.replace(/url\(["']?(.*?)["']?\)/g, function (match, url) {
61
+ if (url.startsWith('http') ||
62
+ url.startsWith('data:') ||
63
+ url.startsWith('../') ||
64
+ url.startsWith('#')) {
65
+ return match;
66
+ }
67
+ return "url(\"".concat(join(relativePrefix, url), "\")");
68
+ });
69
+ };
53
70
  var createCSSPlugin = function (config) {
54
- var _a = config || {}, _b = _a.chunkName, chunkName = _b === void 0 ? 'style-chunk' : _b, _c = _a.templateChunkName, templateChunkName = _c === void 0 ? 'template-chunk' : _c, _d = _a.componentDecoratorChunkName, componentDecoratorChunkName = _d === void 0 ? 'component-decorator' : _d, _e = _a.inlineStyleAttributeKey, inlineStyleAttributeKey = _e === void 0 ? 'style' : _e, _f = _a.classAttributeName, classAttributeName = _f === void 0 ? 'class' : _f, _g = _a.templateStyle, templateStyle = _g === void 0 ? 'html' : _g, _h = _a.declareDependency, declareDependency = _h === void 0 ? 'none' : _h, _j = _a.forceScoping, forceScoping = _j === void 0 ? false : _j, dynamicVariantPrefix = _a.dynamicVariantPrefix, _k = _a.staticPropReferences, staticPropReferences = _k === void 0 ? false : _k;
71
+ var _a = config || {}, _b = _a.chunkName, chunkName = _b === void 0 ? 'style-chunk' : _b, _c = _a.templateChunkName, templateChunkName = _c === void 0 ? 'template-chunk' : _c, _d = _a.componentDecoratorChunkName, componentDecoratorChunkName = _d === void 0 ? 'component-decorator' : _d, _e = _a.inlineStyleAttributeKey, inlineStyleAttributeKey = _e === void 0 ? 'style' : _e, _f = _a.classAttributeName, classAttributeName = _f === void 0 ? 'class' : _f, _g = _a.templateStyle, templateStyle = _g === void 0 ? 'html' : _g, _h = _a.declareDependency, declareDependency = _h === void 0 ? 'none' : _h, dynamicVariantPrefix = _a.dynamicVariantPrefix, _j = _a.staticPropReferences, staticPropReferences = _j === void 0 ? false : _j, _k = _a.standaloneHtmlComponents, standaloneHtmlComponents = _k === void 0 ? false : _k;
55
72
  var cssPlugin = function (structure) { return __awaiter(void 0, void 0, void 0, function () {
56
- var uidl, chunks, dependencies, options, node, _a, componentStyleSet, _b, propDefinitions, _c, projectStyleSet, _d, _e, _f, tokens, isRootComponent, _g, _h, styleSetDefinitions, projectStyleSheetName, projectStyleSheetPath, templateChunk, componentDecoratorChunk, templateLookup, propsPrefix, cssMap, mediaStylesMap, cssFileName, decoratorAST, decoratorParam;
57
- return __generator(this, function (_j) {
73
+ var uidl, chunks, dependencies, options, node, _a, componentStyleSet, _b, propDefinitions, _c, stateDefinitions, _d, projectStyleSet, _e, _f, _g, tokens, isRootComponent, _h, globalStateDefinitions, prefixInlineClasses, _j, _k, styleSetDefinitions, projectStyleSheetName, projectStyleSheetPath, templateChunk, componentDecoratorChunk, jsxNodesLookup, propsPrefix, cssMap, mediaStylesMap, usedProjectStyleIds, generateStylesForElementNode, _i, _l, prop, inlineCssMap, hasTokens, hasUsedProjectStyles, globalAssets, scopedRootClass_1, scopeCssSelectors_1, inlineStyleAssets, _m, inlineStyleAssets_1, styleAsset, css, tokenSelector, usedProjectStyles_1, filteredDefinitions, projectStylesCssMap, projectMediaStylesMap, scopedProjectStyles, mediaStyles, scopedMediaStyles, scopedCssMap, fontAssets, fontLinkNodes, _o, fontAssets_1, fontAsset, linkNode, _p, _q, _r, attrKey, attrValue, value, nodesToPrepend, styleNode, cssContent, cssFileName, decoratorAST, decoratorParam, folderPath_1, cssContent;
74
+ var _s;
75
+ var _t, _u;
76
+ return __generator(this, function (_v) {
58
77
  uidl = structure.uidl, chunks = structure.chunks, dependencies = structure.dependencies, options = structure.options;
59
- node = uidl.node, _a = uidl.styleSetDefinitions, componentStyleSet = _a === void 0 ? {} : _a, _b = uidl.propDefinitions, propDefinitions = _b === void 0 ? {} : _b;
60
- _c = options || {}, projectStyleSet = _c.projectStyleSet, _d = _c.designLanguage, _e = _d === void 0 ? {} : _d, _f = _e.tokens, tokens = _f === void 0 ? {} : _f, isRootComponent = _c.isRootComponent;
61
- _g = projectStyleSet || {}, _h = _g.styleSetDefinitions, styleSetDefinitions = _h === void 0 ? {} : _h, projectStyleSheetName = _g.fileName, projectStyleSheetPath = _g.path;
78
+ node = uidl.node, _a = uidl.styleSetDefinitions, componentStyleSet = _a === void 0 ? {} : _a, _b = uidl.propDefinitions, propDefinitions = _b === void 0 ? {} : _b, _c = uidl.stateDefinitions, stateDefinitions = _c === void 0 ? {} : _c;
79
+ _d = options || {}, projectStyleSet = _d.projectStyleSet, _e = _d.designLanguage, _f = _e === void 0 ? {} : _e, _g = _f.tokens, tokens = _g === void 0 ? {} : _g, isRootComponent = _d.isRootComponent, _h = _d.globalStateDefinitions, globalStateDefinitions = _h === void 0 ? {} : _h, prefixInlineClasses = _d.prefixInlineClasses;
80
+ _j = projectStyleSet || {}, _k = _j.styleSetDefinitions, styleSetDefinitions = _k === void 0 ? {} : _k, projectStyleSheetName = _j.fileName, projectStyleSheetPath = _j.path;
62
81
  if (isRootComponent) {
63
82
  if (Object.keys(tokens).length > 0 || Object.keys(styleSetDefinitions).length > 0) {
64
83
  dependencies[projectStyleSheetName] = {
@@ -73,62 +92,104 @@ var createCSSPlugin = function (config) {
73
92
  }
74
93
  templateChunk = chunks.find(function (chunk) { return chunk.name === templateChunkName; });
75
94
  componentDecoratorChunk = chunks.find(function (chunk) { return chunk.name === componentDecoratorChunkName; });
76
- templateLookup = templateChunk.meta.nodesLookup;
95
+ jsxNodesLookup = templateChunk.meta.nodesLookup;
77
96
  propsPrefix = templateChunk.meta.dynamicRefPrefix
78
97
  ? templateChunk.meta.dynamicRefPrefix.prop
79
98
  : '';
80
99
  cssMap = [];
81
100
  mediaStylesMap = {};
82
- UIDLUtils.traverseElements(node, function (element) {
101
+ usedProjectStyleIds = new Set();
102
+ generateStylesForElementNode = function (element) {
83
103
  var _a, _b;
84
104
  var classNamesToAppend = new Set();
85
105
  var dynamicVariantsToAppend = new Set();
86
- var _c = element.style, style = _c === void 0 ? {} : _c, key = element.key, _d = element.referencedStyles, referencedStyles = _d === void 0 ? {} : _d, dependency = element.dependency, _e = element.attrs, attrs = _e === void 0 ? {} : _e, elementType = element.elementType;
87
- var elementClassName = StringUtils.camelCaseToDashCase(key);
88
- if (forceScoping && (dependency === null || dependency === void 0 ? void 0 : dependency.type) === 'local') {
106
+ var _c = element.style, style = _c === void 0 ? {} : _c, key = element.key, _d = element.referencedStyles, referencedStyles = _d === void 0 ? {} : _d, _e = element.attrs, attrs = _e === void 0 ? {} : _e, elementType = element.elementType, dependency = element.dependency;
107
+ var dynamicStyleBindings = element.dynamicStyleBindings;
108
+ var hasDynamicBindings = dynamicStyleBindings && Object.keys(dynamicStyleBindings).length > 0;
109
+ var root = jsxNodesLookup[key];
110
+ if (!root) {
111
+ return;
112
+ }
113
+ // Refer to line 323 all component scoped styles are appended with component name by default
114
+ if ((dependency === null || dependency === void 0 ? void 0 : dependency.type) === 'local') {
89
115
  StyleBuilders.setPropValueForCompStyle({
90
116
  attrs: attrs,
91
- key: key,
92
- jsxNodesLookup: templateLookup,
117
+ root: root,
93
118
  templateStyle: templateStyle,
119
+ // elementType is used here in-order to target the component name that the class is actually defined.
120
+ // Here we are appendigng to the node where the component is being called.
94
121
  getClassName: function (styleName) {
95
- return getClassName(forceScoping, StringUtils.camelCaseToDashCase(elementType), styleName);
122
+ return StringUtils.camelCaseToDashCase(elementType + styleName);
96
123
  },
97
124
  });
98
125
  }
99
126
  if (Object.keys(style).length === 0 &&
100
127
  Object.keys(referencedStyles).length === 0 &&
101
- Object.keys(componentStyleSet).length === 0) {
128
+ Object.keys(componentStyleSet).length === 0 &&
129
+ !hasDynamicBindings) {
102
130
  return;
103
131
  }
104
- var root = templateLookup[key];
105
- if (!root) {
106
- throw new PluginCSS("Node - ".concat(key, " is missing from the template chunk"));
107
- }
108
- var className = getClassName(forceScoping, uidl.name, elementClassName);
132
+ // Inline (node-key-derived) classes can share a name with a real project
133
+ // class. When `prefixInlineClasses` is set (AI editor codegen only), tag
134
+ // every inline class so the consumer can tell fake from real by name.
135
+ var className = prefixInlineClasses
136
+ ? "".concat(prefixInlineClasses).concat(StringUtils.camelCaseToDashCase(key))
137
+ : StringUtils.camelCaseToDashCase(key);
109
138
  var _f = UIDLUtils.splitDynamicAndStaticStyles(style), staticStyles = _f.staticStyles, dynamicStyles = _f.dynamicStyles, tokenStyles = _f.tokenStyles;
110
139
  if (Object.keys(staticStyles).length > 0 || Object.keys(tokenStyles).length > 0) {
111
140
  var collectedStyles = __assign(__assign({}, StyleUtils.getContentOfStyleObject(staticStyles)), StyleUtils.getCSSVariablesContentFromTokenStyles(tokenStyles));
112
141
  cssMap.push(StyleBuilders.createCSSClass(className, collectedStyles));
113
142
  classNamesToAppend.add(className);
114
143
  }
144
+ var jsxInlineStyles = null;
115
145
  if (Object.keys(dynamicStyles).length > 0) {
146
+ for (var _i = 0, _g = Object.values(dynamicStyles); _i < _g.length; _i++) {
147
+ var styleValue = _g[_i];
148
+ if (styleValue.type === 'dynamic' &&
149
+ styleValue.content.referenceType === 'global' &&
150
+ templateChunk.meta.globalReferences) {
151
+ ;
152
+ templateChunk.meta.globalReferences.push(styleValue.content.id);
153
+ }
154
+ }
116
155
  /* If dynamic styles are on nested-styles they are unfortunately lost,
117
156
  since inline style does not support that */
118
157
  if (templateStyle === 'html') {
119
158
  var inlineStyles = createDynamicInlineStyle(dynamicStyles);
120
- HASTUtils.addAttributeToNode(root, inlineStyleAttributeKey, "{".concat(inlineStyles, "}"));
159
+ var bindingParts = hasDynamicBindings
160
+ ? createDynamicBindingInlineStyle(dynamicStyleBindings)
161
+ : '';
162
+ var combined = bindingParts ? "".concat(inlineStyles, ", ").concat(bindingParts) : inlineStyles;
163
+ HASTUtils.addAttributeToNode(root, inlineStyleAttributeKey, "{".concat(combined, "}"));
121
164
  }
122
165
  else {
123
- var inlineStyles = UIDLUtils.transformDynamicStyles(dynamicStyles, function (styleValue) {
166
+ jsxInlineStyles = UIDLUtils.transformDynamicStyles(dynamicStyles, function (styleValue) {
124
167
  return StyleBuilders.createDynamicStyleExpression(styleValue, propsPrefix);
125
168
  });
126
- ASTUtils.addAttributeToJSXTag(root, inlineStyleAttributeKey, inlineStyles);
127
169
  }
128
170
  }
171
+ else if (hasDynamicBindings && templateStyle === 'html') {
172
+ var bindingParts = createDynamicBindingInlineStyle(dynamicStyleBindings);
173
+ HASTUtils.addAttributeToNode(root, inlineStyleAttributeKey, "{".concat(bindingParts, "}"));
174
+ }
175
+ if (hasDynamicBindings && templateStyle !== 'html') {
176
+ if (!jsxInlineStyles) {
177
+ jsxInlineStyles = {};
178
+ }
179
+ for (var _h = 0, _j = Object.entries(dynamicStyleBindings); _h < _j.length; _h++) {
180
+ var _k = _j[_h], cssProperty = _k[0], binding = _k[1];
181
+ var camelCaseProperty = cssProperty.replace(/-([a-z])/g, function (_, letter) {
182
+ return letter.toUpperCase();
183
+ });
184
+ jsxInlineStyles[camelCaseProperty] = StyleBuilders.createDynamicBindingExpression(binding);
185
+ }
186
+ }
187
+ if (jsxInlineStyles) {
188
+ ASTUtils.addAttributeToJSXTag(root, inlineStyleAttributeKey, jsxInlineStyles);
189
+ }
129
190
  Object.values(referencedStyles).forEach(function (styleRef) {
130
191
  var _a;
131
- var _b;
192
+ var _b, _c;
132
193
  switch (styleRef.content.mapType) {
133
194
  case 'inlined': {
134
195
  var filtredStyles = UIDLUtils.splitDynamicAndStaticStyles(styleRef.content.styles);
@@ -156,17 +217,18 @@ var createCSSPlugin = function (config) {
156
217
  styleRef.content.content.content.referenceType === 'prop') {
157
218
  var defaultPropValue = (_b = propDefinitions[styleRef.content.content.content.id]) === null || _b === void 0 ? void 0 : _b.defaultValue;
158
219
  if (defaultPropValue) {
159
- /* Changing the default value of the prop.
160
- When forceScoping is enabled the classnames change. So, we need to change the default prop too. */
161
- propDefinitions[styleRef.content.content.content.id].defaultValue = getClassName(forceScoping, uidl.name, String(defaultPropValue));
220
+ propDefinitions[styleRef.content.content.content.id].defaultValue =
221
+ StringUtils.camelCaseToDashCase(String(defaultPropValue));
162
222
  }
223
+ // staticPropReferences flag is only used for just html-code generation.
224
+ // This is used to append the class name to the node where the component is being called.
225
+ // Instead of how frameworks handle them using props at runtime. This makes the props
226
+ // to behave statically during the generation time instead by appending them directly.
163
227
  if (staticPropReferences) {
164
- if (!defaultPropValue) {
228
+ if (defaultPropValue === undefined || typeof defaultPropValue !== 'string') {
165
229
  return;
166
230
  }
167
- if (staticPropReferences) {
168
- classNamesToAppend.add(getClassName(forceScoping, uidl.name, String(defaultPropValue)));
169
- }
231
+ classNamesToAppend.add(StringUtils.camelCaseToDashCase(uidl.name + defaultPropValue));
170
232
  }
171
233
  else {
172
234
  dynamicVariantsToAppend.add(styleRef.content.content.content.id);
@@ -177,7 +239,7 @@ var createCSSPlugin = function (config) {
177
239
  if (!componentStyleSet[styleRef.content.content.content.id]) {
178
240
  throw new PluginCSS("Node ".concat(elementType, " is referring to a comp style instance ").concat(styleRef.content.content.content.id, " which is missing."));
179
241
  }
180
- classNamesToAppend.add(getClassName(forceScoping, uidl.name, String(styleRef.content.content.content.id)));
242
+ classNamesToAppend.add(StringUtils.camelCaseToDashCase(uidl.name + styleRef.content.content.content.id));
181
243
  }
182
244
  return;
183
245
  }
@@ -185,9 +247,195 @@ var createCSSPlugin = function (config) {
185
247
  var content = styleRef.content;
186
248
  var referedStyle = styleSetDefinitions[content.referenceId];
187
249
  if (!referedStyle) {
188
- throw new PluginCSS("Style used from global stylesheet is missing - ".concat(content.referenceId));
250
+ return;
251
+ }
252
+ usedProjectStyleIds.add(content.referenceId);
253
+ if (styleRef.content.condition) {
254
+ if (templateStyle === 'html') {
255
+ var _d = styleRef.content.condition, staticValue = _d.value, reference = _d.reference, _e = _d.expression, conditions = _e.conditions, matchingCriteria = _e.matchingCriteria;
256
+ var _f = reference.content, referenceType = _f.referenceType, id = _f.id, _g = _f.refPath, refPath = _g === void 0 ? [] : _g;
257
+ switch (referenceType) {
258
+ case 'prop': {
259
+ var usedProp = propDefinitions[id];
260
+ if (usedProp === undefined || usedProp.defaultValue === undefined) {
261
+ throw new PluginCSS("Prop with ".concat(id, " is missing in the propDefinitions."));
262
+ }
263
+ var defaultValue = usedProp.defaultValue;
264
+ var operandDefaultValue = void 0;
265
+ for (var _i = 0, refPath_1 = refPath; _i < refPath_1.length; _i++) {
266
+ var path = refPath_1[_i];
267
+ defaultValue = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue[path];
268
+ }
269
+ // If defaultValue is undefined or null after path traversal, use original default
270
+ defaultValue = defaultValue !== null && defaultValue !== void 0 ? defaultValue : usedProp.defaultValue;
271
+ // our conditions can be of type 'expr' or they can be a dynamic reference
272
+ var rightSideCondition = conditions[0].operand;
273
+ var rightSideConditionType = typeof rightSideCondition;
274
+ if (rightSideConditionType === 'object') {
275
+ var type = rightSideCondition.type;
276
+ if (type === 'dynamic') {
277
+ var dynamicRef = rightSideCondition;
278
+ if (dynamicRef.content.referenceType === 'prop') {
279
+ var prop = propDefinitions[dynamicRef.content.id];
280
+ if (prop && prop.defaultValue) {
281
+ operandDefaultValue = prop.defaultValue;
282
+ }
283
+ }
284
+ if (dynamicRef.content.referenceType === 'state') {
285
+ var state = stateDefinitions[dynamicRef.content.id];
286
+ if (state && state.defaultValue) {
287
+ defaultValue = state.defaultValue;
288
+ }
289
+ }
290
+ }
291
+ if (type === 'expr') {
292
+ operandDefaultValue = rightSideCondition.content;
293
+ }
294
+ }
295
+ else {
296
+ operandDefaultValue = rightSideCondition;
297
+ }
298
+ // Since we know the operand and the default value from the prop.
299
+ // We can try building the condition and check if the condition is true or false.
300
+ // @todo: You can only use a 'value' in UIDL or 'conditions' but not both.
301
+ // UIDL validations need to be improved on this aspect.
302
+ var resolvedConditions = [
303
+ {
304
+ operation: conditions[0].operation,
305
+ operand: operandDefaultValue,
306
+ },
307
+ ];
308
+ var dynamicConditions = createConditionalStatement(staticValue !== undefined
309
+ ? [{ operand: staticValue, operation: '===' }]
310
+ : resolvedConditions, defaultValue);
311
+ var matchCondition = matchingCriteria && matchingCriteria === 'all' ? '&&' : '||';
312
+ var conditionString = dynamicConditions.join(" ".concat(matchCondition, " "));
313
+ // tslint:disable-next-line function-constructor
314
+ var isConditionPassing = new Function("return ".concat(conditionString))();
315
+ if (isConditionPassing) {
316
+ classNamesToAppend.add(styleRef.content.referenceId);
317
+ return;
318
+ }
319
+ return;
320
+ }
321
+ case 'state': {
322
+ var usedState = stateDefinitions[id];
323
+ if (usedState === undefined || usedState.defaultValue === undefined) {
324
+ throw new PluginCSS("State with ".concat(id, " is missing in the stateDefinitions."));
325
+ }
326
+ var defaultValue = usedState.defaultValue;
327
+ for (var _h = 0, refPath_2 = refPath; _h < refPath_2.length; _h++) {
328
+ var path = refPath_2[_h];
329
+ defaultValue = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue[path];
330
+ }
331
+ if (typeof defaultValue === 'object' &&
332
+ defaultValue !== null &&
333
+ 'type' in defaultValue &&
334
+ 'content' in defaultValue) {
335
+ var entry = defaultValue;
336
+ if (entry.type === 'static') {
337
+ defaultValue = entry.content;
338
+ }
339
+ }
340
+ defaultValue = defaultValue !== null && defaultValue !== void 0 ? defaultValue : usedState.defaultValue;
341
+ var dynamicConditions = createConditionalStatement(staticValue !== undefined
342
+ ? [{ operand: staticValue, operation: '===' }]
343
+ : conditions, defaultValue);
344
+ var matchCondition = matchingCriteria && matchingCriteria === 'all' ? '&&' : '||';
345
+ var conditionString = dynamicConditions.join(" ".concat(matchCondition, " "));
346
+ // tslint:disable-next-line function-constructor
347
+ var isConditionPassing = new Function("return ".concat(conditionString))();
348
+ if (isConditionPassing) {
349
+ classNamesToAppend.add(styleRef.content.referenceId);
350
+ return;
351
+ }
352
+ return;
353
+ }
354
+ case 'globalState': {
355
+ var gsDef = findGlobalStateDefinitionById(id, globalStateDefinitions);
356
+ if (!gsDef) {
357
+ classNamesToAppend.add(styleRef.content.referenceId);
358
+ return;
359
+ }
360
+ var defaultValue = gsDef.defaultValue;
361
+ for (var _j = 0, refPath_3 = refPath; _j < refPath_3.length; _j++) {
362
+ var path = refPath_3[_j];
363
+ defaultValue = defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue[path];
364
+ }
365
+ defaultValue = defaultValue !== null && defaultValue !== void 0 ? defaultValue : gsDef.defaultValue;
366
+ var dynamicConditions = createConditionalStatement(staticValue !== undefined
367
+ ? [{ operand: staticValue, operation: '===' }]
368
+ : conditions, defaultValue);
369
+ var matchCondition = matchingCriteria && matchingCriteria === 'all' ? '&&' : '||';
370
+ var conditionString = dynamicConditions.join(" ".concat(matchCondition, " "));
371
+ // tslint:disable-next-line function-constructor
372
+ var isConditionPassing = new Function("return ".concat(conditionString))();
373
+ if (isConditionPassing) {
374
+ classNamesToAppend.add(styleRef.content.referenceId);
375
+ return;
376
+ }
377
+ return;
378
+ }
379
+ default: {
380
+ classNamesToAppend.add(styleRef.content.referenceId);
381
+ return;
382
+ }
383
+ }
384
+ }
385
+ else {
386
+ var referenceContent = styleRef.content.condition.reference.content;
387
+ var referenceType = referenceContent.referenceType;
388
+ var nameToAppend = referenceType === 'local' && ((_c = referenceContent.refPath) === null || _c === void 0 ? void 0 : _c.length)
389
+ ? referenceContent.refPath.join('.')
390
+ : referenceContent.id;
391
+ var conditions = styleRef.content.condition.expression.conditions;
392
+ var operator = conditions[0].operation;
393
+ var right = conditions[0].operand;
394
+ var binaryExpressionType = '';
395
+ switch (referenceType) {
396
+ case 'prop': {
397
+ binaryExpressionType = propDefinitions[nameToAppend].type;
398
+ break;
399
+ }
400
+ case 'state': {
401
+ binaryExpressionType = stateDefinitions[nameToAppend].type;
402
+ break;
403
+ }
404
+ case 'local': {
405
+ binaryExpressionType = typeof right;
406
+ break;
407
+ }
408
+ default: {
409
+ if (referenceType === 'globalState') {
410
+ var gsDef = findGlobalStateDefinitionById(nameToAppend, globalStateDefinitions);
411
+ if (gsDef) {
412
+ binaryExpressionType = gsDef.type;
413
+ }
414
+ break;
415
+ }
416
+ throw new PluginCSS("Un-supported reference type ".concat(referenceType, " for ").concat(nameToAppend));
417
+ }
418
+ }
419
+ var binaryKey = nameToAppend;
420
+ if (referenceType === 'prop') {
421
+ binaryKey = 'props?.' + nameToAppend;
422
+ }
423
+ else if (referenceType === 'globalState') {
424
+ var gsDef = findGlobalStateDefinitionById(nameToAppend, globalStateDefinitions);
425
+ binaryKey = gsDef ? gsDef.name : nameToAppend;
426
+ }
427
+ var binaryExpression = createBinaryExpression({ operation: operator, operand: right }, {
428
+ key: binaryKey,
429
+ type: binaryExpressionType,
430
+ });
431
+ var conditionalExpression = types.conditionalExpression(binaryExpression, types.stringLiteral(content.referenceId), types.stringLiteral(''));
432
+ dynamicVariantsToAppend.add(conditionalExpression);
433
+ return;
434
+ }
435
+ }
436
+ else {
437
+ classNamesToAppend.add(content.referenceId);
189
438
  }
190
- classNamesToAppend.add(content.referenceId);
191
439
  return;
192
440
  }
193
441
  default: {
@@ -211,17 +459,200 @@ var createCSSPlugin = function (config) {
211
459
  var dynamicAttrValueIdentifier = dynamicVariantPrefix
212
460
  ? types.identifier(dynamicVariantPrefix)
213
461
  : types.identifier(propsPrefix);
214
- return types.memberExpression(dynamicAttrValueIdentifier, types.identifier(variant));
462
+ if (typeof variant === 'string') {
463
+ return types.memberExpression(dynamicAttrValueIdentifier, types.identifier(variant));
464
+ }
465
+ else {
466
+ // variant is a ConditionalExpression, so return it directly
467
+ return variant;
468
+ }
215
469
  }));
216
470
  }
217
- });
471
+ };
472
+ UIDLUtils.traverseElements(node, generateStylesForElementNode);
473
+ for (_i = 0, _l = Object.values(propDefinitions); _i < _l.length; _i++) {
474
+ prop = _l[_i];
475
+ if (prop.type === 'element' && prop.defaultValue) {
476
+ UIDLUtils.traverseElements(prop.defaultValue, generateStylesForElementNode);
477
+ }
478
+ }
218
479
  if (Object.keys(componentStyleSet).length > 0) {
219
- StyleBuilders.generateStylesFromStyleSetDefinitions(componentStyleSet, cssMap, mediaStylesMap, function (styleName) { return getClassName(forceScoping, uidl.name, styleName); });
480
+ StyleBuilders.generateStylesFromStyleSetDefinitions(componentStyleSet, cssMap, mediaStylesMap, function (styleName) {
481
+ return StringUtils.camelCaseToDashCase(uidl.name + styleName);
482
+ });
220
483
  }
221
484
  if (Object.keys(mediaStylesMap).length > 0) {
222
485
  cssMap.push.apply(cssMap, StyleBuilders.generateMediaStyle(mediaStylesMap));
223
486
  }
224
- if (cssMap.length > 0) {
487
+ // Handle inline style tag for HTML templates (self-contained fragments)
488
+ if (standaloneHtmlComponents && templateStyle === 'html' && templateChunk) {
489
+ inlineCssMap = [];
490
+ hasTokens = Object.keys(tokens).length > 0;
491
+ hasUsedProjectStyles = usedProjectStyleIds.size > 0;
492
+ globalAssets = options.globalAssets || [];
493
+ scopedRootClass_1 = (_t = templateChunk.meta) === null || _t === void 0 ? void 0 : _t.scopedRootClass;
494
+ scopeCssSelectors_1 = function (css, rootSelector) {
495
+ // Replace :root with scoped root class
496
+ var scopedCss = css.replace(/:root\s*\{/g, "".concat(rootSelector, " {"));
497
+ // Scope regular selectors (but not inside @media or @keyframes)
498
+ // This regex matches CSS rules outside of @ blocks
499
+ scopedCss = scopedCss.replace(/(?:^|\})\s*([^@{}]+?)\s*\{/gm, function (match, selectors) {
500
+ // Skip if it's just closing brace or empty
501
+ if (!selectors || selectors.trim() === '') {
502
+ return match;
503
+ }
504
+ // Skip if selector is already the root class
505
+ if (selectors.trim() === rootSelector) {
506
+ return match;
507
+ }
508
+ // Handle multiple selectors (comma-separated)
509
+ var scopedSelectors = selectors
510
+ .split(',')
511
+ .map(function (sel) {
512
+ var trimmedSel = sel.trim();
513
+ // Skip empty selectors
514
+ if (!trimmedSel) {
515
+ return '';
516
+ }
517
+ // Skip if already scoped
518
+ if (trimmedSel.startsWith(rootSelector)) {
519
+ return trimmedSel;
520
+ }
521
+ // Replace html/body with root selector
522
+ if (trimmedSel === 'html' || trimmedSel === 'body') {
523
+ return rootSelector;
524
+ }
525
+ // For universal selector, scope it
526
+ if (trimmedSel === '*') {
527
+ return "".concat(rootSelector, " *");
528
+ }
529
+ // Prefix other selectors with root
530
+ return "".concat(rootSelector, " ").concat(trimmedSel);
531
+ })
532
+ .filter(Boolean)
533
+ .join(', ');
534
+ return match.replace(selectors, scopedSelectors);
535
+ });
536
+ return scopedCss;
537
+ };
538
+ inlineStyleAssets = globalAssets.filter(function (asset) { return asset.type === 'style' && 'content' in asset; });
539
+ for (_m = 0, inlineStyleAssets_1 = inlineStyleAssets; _m < inlineStyleAssets_1.length; _m++) {
540
+ styleAsset = inlineStyleAssets_1[_m];
541
+ if (styleAsset.content) {
542
+ css = scopedRootClass_1
543
+ ? scopeCssSelectors_1(styleAsset.content, ".".concat(scopedRootClass_1))
544
+ : styleAsset.content;
545
+ inlineCssMap.push(css);
546
+ }
547
+ }
548
+ // 1. Add CSS variables (tokens) - scoped to root class instead of :root
549
+ if (hasTokens) {
550
+ tokenSelector = scopedRootClass_1 ? ".".concat(scopedRootClass_1) : ':root';
551
+ inlineCssMap.push(StyleBuilders.createCSSClassWithSelector('@global', tokenSelector, StyleUtils.getTokensContentFromTokensObject(tokens)));
552
+ }
553
+ // 2. Add project-referenced styles that this component uses (scoped)
554
+ if (hasUsedProjectStyles) {
555
+ usedProjectStyles_1 = Array.from(usedProjectStyleIds);
556
+ filteredDefinitions = Object.fromEntries(Object.entries(styleSetDefinitions).filter(function (_a) {
557
+ var key = _a[0], definition = _a[1];
558
+ // Include if exact match
559
+ if (usedProjectStyles_1.includes(key)) {
560
+ return true;
561
+ }
562
+ // Include compound selectors where className matches a used project style
563
+ // e.g., "navigation-mobile-overlay.navigation-mobile-overlay-active" has className "navigation-mobile-overlay"
564
+ var className = definition.className;
565
+ if (className && usedProjectStyles_1.includes(className)) {
566
+ return true;
567
+ }
568
+ // Include compound selectors where the base class (before the dot or space) is a used style
569
+ // e.g., "dashboard-sidebar.collapsed" should be included if "dashboard-sidebar" is used
570
+ // e.g., "dashboard-sidebar.collapsed .sidebar-brand-text" should also be included
571
+ var dotIdx = key.indexOf('.');
572
+ var spaceIdx = key.indexOf(' ');
573
+ if (dotIdx > 0) {
574
+ var baseClass = key.substring(0, dotIdx);
575
+ if (usedProjectStyles_1.includes(baseClass)) {
576
+ return true;
577
+ }
578
+ }
579
+ if (spaceIdx > 0 && dotIdx < 0) {
580
+ var baseClass = key.substring(0, spaceIdx);
581
+ if (usedProjectStyles_1.includes(baseClass)) {
582
+ return true;
583
+ }
584
+ }
585
+ return false;
586
+ }));
587
+ projectStylesCssMap = [];
588
+ projectMediaStylesMap = {};
589
+ StyleBuilders.generateStylesFromStyleSetDefinitions(filteredDefinitions, projectStylesCssMap, projectMediaStylesMap, function (styleName) { return styleName; });
590
+ // Scope the project styles if scoped root class is available
591
+ if (scopedRootClass_1) {
592
+ scopedProjectStyles = projectStylesCssMap.map(function (css) {
593
+ return scopeCssSelectors_1(css, ".".concat(scopedRootClass_1));
594
+ });
595
+ inlineCssMap.push.apply(inlineCssMap, scopedProjectStyles);
596
+ }
597
+ else {
598
+ inlineCssMap.push.apply(inlineCssMap, projectStylesCssMap);
599
+ }
600
+ if (Object.keys(projectMediaStylesMap).length > 0) {
601
+ mediaStyles = StyleBuilders.generateMediaStyle(projectMediaStylesMap);
602
+ if (scopedRootClass_1) {
603
+ scopedMediaStyles = mediaStyles.map(function (css) {
604
+ return scopeCssSelectors_1(css, ".".concat(scopedRootClass_1));
605
+ });
606
+ inlineCssMap.push.apply(inlineCssMap, scopedMediaStyles);
607
+ }
608
+ else {
609
+ inlineCssMap.push.apply(inlineCssMap, mediaStyles);
610
+ }
611
+ }
612
+ }
613
+ // 3. Add component-specific styles (scoped)
614
+ if (scopedRootClass_1) {
615
+ scopedCssMap = cssMap.map(function (css) { return scopeCssSelectors_1(css, ".".concat(scopedRootClass_1)); });
616
+ inlineCssMap.push.apply(inlineCssMap, scopedCssMap);
617
+ }
618
+ else {
619
+ inlineCssMap.push.apply(inlineCssMap, cssMap);
620
+ }
621
+ fontAssets = globalAssets.filter(function (asset) { return asset.type === 'font' && 'path' in asset; });
622
+ fontLinkNodes = [];
623
+ for (_o = 0, fontAssets_1 = fontAssets; _o < fontAssets_1.length; _o++) {
624
+ fontAsset = fontAssets_1[_o];
625
+ linkNode = HASTBuilders.createHTMLNode('link');
626
+ HASTUtils.addAttributeToNode(linkNode, 'rel', 'stylesheet');
627
+ HASTUtils.addAttributeToNode(linkNode, 'href', fontAsset.path);
628
+ if (fontAsset.attrs) {
629
+ for (_p = 0, _q = Object.entries(fontAsset.attrs); _p < _q.length; _p++) {
630
+ _r = _q[_p], attrKey = _r[0], attrValue = _r[1];
631
+ value = typeof attrValue === 'object' && 'content' in attrValue
632
+ ? String(attrValue.content)
633
+ : String(attrValue);
634
+ HASTUtils.addAttributeToNode(linkNode, attrKey, value);
635
+ }
636
+ }
637
+ fontLinkNodes.push(linkNode);
638
+ }
639
+ // 5. Create <style> node and prepend to HTML (fonts first, then styles)
640
+ if (inlineCssMap.length > 0 || fontLinkNodes.length > 0) {
641
+ nodesToPrepend = [];
642
+ // Add font links first
643
+ nodesToPrepend.push.apply(nodesToPrepend, fontLinkNodes);
644
+ // Add style tag with all CSS
645
+ if (inlineCssMap.length > 0) {
646
+ styleNode = HASTBuilders.createHTMLNode('style');
647
+ cssContent = HASTBuilders.createTextNode(inlineCssMap.join('\n\n'));
648
+ styleNode.children.push(cssContent);
649
+ nodesToPrepend.push(styleNode);
650
+ }
651
+ ;
652
+ (_s = templateChunk.content.children).unshift.apply(_s, nodesToPrepend);
653
+ }
654
+ }
655
+ else if (cssMap.length > 0) {
225
656
  cssFileName = UIDLUtils.getStyleFileName(uidl);
226
657
  if (declareDependency === 'decorator' && componentDecoratorChunk) {
227
658
  decoratorAST = componentDecoratorChunk.content;
@@ -241,11 +672,15 @@ var createCSSPlugin = function (config) {
241
672
  },
242
673
  };
243
674
  }
675
+ folderPath_1 = (_u = uidl.outputOptions) === null || _u === void 0 ? void 0 : _u.folderPath;
676
+ cssContent = (folderPath_1 === null || folderPath_1 === void 0 ? void 0 : folderPath_1.length) > 0
677
+ ? cssMap.map(function (css) { return prefixUrlPathsInCss(css, folderPath_1); }).join('\n \n')
678
+ : cssMap.join('\n \n');
244
679
  chunks.push({
245
680
  type: ChunkType.STRING,
246
681
  name: chunkName,
247
682
  fileType: FileType.CSS,
248
- content: cssMap.join('\n'),
683
+ content: cssContent,
249
684
  linkAfter: [],
250
685
  });
251
686
  }
@@ -256,6 +691,15 @@ var createCSSPlugin = function (config) {
256
691
  };
257
692
  export { createStyleSheetPlugin, createCSSPlugin };
258
693
  export default createCSSPlugin();
694
+ var findGlobalStateDefinitionById = function (id, definitions) {
695
+ for (var _i = 0, _a = Object.values(definitions); _i < _a.length; _i++) {
696
+ var def = _a[_i];
697
+ if (def.id === id) {
698
+ return def;
699
+ }
700
+ }
701
+ return undefined;
702
+ };
259
703
  var createDynamicInlineStyle = function (styles) {
260
704
  return Object.keys(styles)
261
705
  .map(function (styleKey) {
@@ -263,9 +707,15 @@ var createDynamicInlineStyle = function (styles) {
263
707
  })
264
708
  .join(', ');
265
709
  };
266
- var getClassName = function (scoping, uidlName, nodeStyleName) {
267
- return scoping
268
- ? StringUtils.camelCaseToDashCase("".concat(uidlName, "-").concat(nodeStyleName))
269
- : StringUtils.camelCaseToDashCase(nodeStyleName);
710
+ var createDynamicBindingInlineStyle = function (bindings) {
711
+ return Object.entries(bindings)
712
+ .map(function (_a) {
713
+ var prop = _a[0], binding = _a[1];
714
+ var value = binding.referenceType === 'ctx' && binding.contextName
715
+ ? "".concat(binding.contextName, ".").concat(binding.stateKey)
716
+ : binding.stateKey;
717
+ return "".concat(prop, ": ").concat(value, " != null ? ").concat(value, " : '").concat(binding.defaultValue || '', "'");
718
+ })
719
+ .join(', ');
270
720
  };
271
721
  //# sourceMappingURL=index.js.map