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