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