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