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