@ikas/component-cli 1.4.0-beta.9 → 1.4.0-beta.90

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.
Files changed (136) hide show
  1. package/dist/commands/add-sections-to-page.d.ts +3 -0
  2. package/dist/commands/add-sections-to-page.d.ts.map +1 -0
  3. package/dist/commands/add-sections-to-page.js +39 -0
  4. package/dist/commands/add-sections-to-page.js.map +1 -0
  5. package/dist/commands/add-to-page.d.ts +3 -0
  6. package/dist/commands/add-to-page.d.ts.map +1 -0
  7. package/dist/commands/add-to-page.js +41 -0
  8. package/dist/commands/add-to-page.js.map +1 -0
  9. package/dist/commands/build.d.ts.map +1 -1
  10. package/dist/commands/build.js +5 -165
  11. package/dist/commands/build.js.map +1 -1
  12. package/dist/commands/config.d.ts.map +1 -1
  13. package/dist/commands/config.js +112 -4
  14. package/dist/commands/config.js.map +1 -1
  15. package/dist/commands/create-design-tokens.d.ts +24 -0
  16. package/dist/commands/create-design-tokens.d.ts.map +1 -0
  17. package/dist/commands/create-design-tokens.js +297 -0
  18. package/dist/commands/create-design-tokens.js.map +1 -0
  19. package/dist/commands/create-global-variable.d.ts +3 -0
  20. package/dist/commands/create-global-variable.d.ts.map +1 -0
  21. package/dist/commands/create-global-variable.js +53 -0
  22. package/dist/commands/create-global-variable.js.map +1 -0
  23. package/dist/commands/create-page.d.ts +3 -0
  24. package/dist/commands/create-page.d.ts.map +1 -0
  25. package/dist/commands/create-page.js +31 -0
  26. package/dist/commands/create-page.js.map +1 -0
  27. package/dist/commands/delete-theme-globals.d.ts +4 -0
  28. package/dist/commands/delete-theme-globals.d.ts.map +1 -0
  29. package/dist/commands/delete-theme-globals.js +48 -0
  30. package/dist/commands/delete-theme-globals.js.map +1 -0
  31. package/dist/commands/dev.d.ts.map +1 -1
  32. package/dist/commands/dev.js +77 -49
  33. package/dist/commands/dev.js.map +1 -1
  34. package/dist/commands/get-component-props.d.ts +3 -0
  35. package/dist/commands/get-component-props.d.ts.map +1 -0
  36. package/dist/commands/get-component-props.js +32 -0
  37. package/dist/commands/get-component-props.js.map +1 -0
  38. package/dist/commands/get-page-by-type.d.ts +3 -0
  39. package/dist/commands/get-page-by-type.d.ts.map +1 -0
  40. package/dist/commands/get-page-by-type.js +25 -0
  41. package/dist/commands/get-page-by-type.js.map +1 -0
  42. package/dist/commands/get-section-values.d.ts +3 -0
  43. package/dist/commands/get-section-values.d.ts.map +1 -0
  44. package/dist/commands/get-section-values.js +39 -0
  45. package/dist/commands/get-section-values.js.map +1 -0
  46. package/dist/commands/import.d.ts +3 -0
  47. package/dist/commands/import.d.ts.map +1 -0
  48. package/dist/commands/import.js +25 -0
  49. package/dist/commands/import.js.map +1 -0
  50. package/dist/commands/list-entities.d.ts +3 -0
  51. package/dist/commands/list-entities.d.ts.map +1 -0
  52. package/dist/commands/list-entities.js +32 -0
  53. package/dist/commands/list-entities.js.map +1 -0
  54. package/dist/commands/list-imported.d.ts +3 -0
  55. package/dist/commands/list-imported.d.ts.map +1 -0
  56. package/dist/commands/list-imported.js +25 -0
  57. package/dist/commands/list-imported.js.map +1 -0
  58. package/dist/commands/list-page-sections.d.ts +3 -0
  59. package/dist/commands/list-page-sections.d.ts.map +1 -0
  60. package/dist/commands/list-page-sections.js +25 -0
  61. package/dist/commands/list-page-sections.js.map +1 -0
  62. package/dist/commands/list-pages.d.ts +3 -0
  63. package/dist/commands/list-pages.d.ts.map +1 -0
  64. package/dist/commands/list-pages.js +21 -0
  65. package/dist/commands/list-pages.js.map +1 -0
  66. package/dist/commands/list-theme-globals.d.ts +3 -0
  67. package/dist/commands/list-theme-globals.d.ts.map +1 -0
  68. package/dist/commands/list-theme-globals.js +22 -0
  69. package/dist/commands/list-theme-globals.js.map +1 -0
  70. package/dist/commands/publish-theme.d.ts +3 -0
  71. package/dist/commands/publish-theme.d.ts.map +1 -0
  72. package/dist/commands/publish-theme.js +29 -0
  73. package/dist/commands/publish-theme.js.map +1 -0
  74. package/dist/commands/search-products.d.ts +3 -0
  75. package/dist/commands/search-products.d.ts.map +1 -0
  76. package/dist/commands/search-products.js +40 -0
  77. package/dist/commands/search-products.js.map +1 -0
  78. package/dist/commands/update-global-variable.d.ts +3 -0
  79. package/dist/commands/update-global-variable.d.ts.map +1 -0
  80. package/dist/commands/update-global-variable.js +47 -0
  81. package/dist/commands/update-global-variable.js.map +1 -0
  82. package/dist/commands/update-page-sections.d.ts +3 -0
  83. package/dist/commands/update-page-sections.d.ts.map +1 -0
  84. package/dist/commands/update-page-sections.js +39 -0
  85. package/dist/commands/update-page-sections.js.map +1 -0
  86. package/dist/commands/update-section-prop.d.ts +3 -0
  87. package/dist/commands/update-section-prop.d.ts.map +1 -0
  88. package/dist/commands/update-section-prop.js +59 -0
  89. package/dist/commands/update-section-prop.js.map +1 -0
  90. package/dist/commands/upload-image.d.ts +3 -0
  91. package/dist/commands/upload-image.d.ts.map +1 -0
  92. package/dist/commands/upload-image.js +38 -0
  93. package/dist/commands/upload-image.js.map +1 -0
  94. package/dist/commands/upload-images.d.ts +3 -0
  95. package/dist/commands/upload-images.d.ts.map +1 -0
  96. package/dist/commands/upload-images.js +48 -0
  97. package/dist/commands/upload-images.js.map +1 -0
  98. package/dist/index.d.ts.map +1 -1
  99. package/dist/index.js +55 -0
  100. package/dist/index.js.map +1 -1
  101. package/dist/types.d.ts +11 -1
  102. package/dist/types.d.ts.map +1 -1
  103. package/dist/utils/compile.d.ts +4 -1
  104. package/dist/utils/compile.d.ts.map +1 -1
  105. package/dist/utils/compile.js +530 -62
  106. package/dist/utils/compile.js.map +1 -1
  107. package/dist/utils/component-helpers.d.ts +21 -3
  108. package/dist/utils/component-helpers.d.ts.map +1 -1
  109. package/dist/utils/component-helpers.js +111 -4
  110. package/dist/utils/component-helpers.js.map +1 -1
  111. package/dist/utils/editor-action-client.d.ts +28 -0
  112. package/dist/utils/editor-action-client.d.ts.map +1 -0
  113. package/dist/utils/editor-action-client.js +116 -0
  114. package/dist/utils/editor-action-client.js.map +1 -0
  115. package/dist/utils/forbid-deep-imports.d.ts +3 -0
  116. package/dist/utils/forbid-deep-imports.d.ts.map +1 -0
  117. package/dist/utils/forbid-deep-imports.js +54 -0
  118. package/dist/utils/forbid-deep-imports.js.map +1 -0
  119. package/dist/utils/load-image.d.ts +16 -0
  120. package/dist/utils/load-image.d.ts.map +1 -0
  121. package/dist/utils/load-image.js +50 -0
  122. package/dist/utils/load-image.js.map +1 -0
  123. package/dist/utils/websocket-server.d.ts +40 -2
  124. package/dist/utils/websocket-server.d.ts.map +1 -1
  125. package/dist/utils/websocket-server.js +59 -1
  126. package/dist/utils/websocket-server.js.map +1 -1
  127. package/package.json +1 -1
  128. package/templates/add/section.css +11 -0
  129. package/dist/commands/create.d.ts +0 -9
  130. package/dist/commands/create.d.ts.map +0 -1
  131. package/dist/commands/create.js +0 -9
  132. package/dist/commands/create.js.map +0 -1
  133. package/dist/commands/proxy.d.ts +0 -39
  134. package/dist/commands/proxy.d.ts.map +0 -1
  135. package/dist/commands/proxy.js +0 -212
  136. package/dist/commands/proxy.js.map +0 -1
@@ -4,33 +4,41 @@ import * as os from "os";
4
4
  import * as path from "path";
5
5
  import { resolveCssImports } from "./css-import-resolver.js";
6
6
  import { ikasComponentUtilsPlugin } from "./observer-runtime.js";
7
+ import { forbidDeepStorefrontImportsPlugin } from "./forbid-deep-imports.js";
7
8
  /**
8
- * Shim file that tells esbuild `h` and `Fragment` are reserved identifiers.
9
- * Unlike `banner` (which is appended after minification), `inject` is processed
10
- * during compilation so the minifier will never reuse these names.
9
+ * Shim that provides the JSX factory/fragment under collision-proof names.
10
+ *
11
+ * The classic JSX pragma must be a bare identifier in scope. Using `h`/`Fragment`
12
+ * directly means any user-authored local named `h` or `Fragment` (e.g. a `.map`
13
+ * callback param) shadows the factory, so JSX in that scope compiles to a call on
14
+ * the local value → runtime "x is not a function". Aliasing to `__ikas_h` /
15
+ * `__ikas_Fragment` (names users won't write) eliminates that shadowing footgun.
16
+ *
17
+ * Unlike `banner` (appended after minification), `inject` is processed during
18
+ * compilation so the minifier will never reuse these names. The bundled import
19
+ * still references preact's `h`/`Fragment`, so the canvas esm-transform keying off
20
+ * those source export names is unaffected.
11
21
  */
12
22
  const PREACT_JSX_SHIM = path.join(os.tmpdir(), "ikas-preact-jsx-shim.mjs");
13
- fs.writeFileSync(PREACT_JSX_SHIM, 'export{h,Fragment}from"preact";\n');
23
+ fs.writeFileSync(PREACT_JSX_SHIM, 'export{h as __ikas_h,Fragment as __ikas_Fragment}from"preact";\n');
14
24
  /**
15
- * External dependencies - these will be provided by the generated storefront
25
+ * External dependencies - these will be provided by the generated storefront.
26
+ *
27
+ * The `@ikas/bp-storefront*` packages are intentionally NOT listed here: they are externalized
28
+ * by `forbidDeepStorefrontImportsPlugin()` instead, which externalizes the allowed ROOT import
29
+ * and rejects private deep subpaths. (esbuild applies this `external` array before plugin
30
+ * onResolve, so keeping them here would let `/dist/...` deep imports slip through unchecked.)
16
31
  */
17
32
  const SERVER_EXTERNALS = [
18
33
  "preact",
19
34
  "preact/hooks",
20
35
  "preact-render-to-string",
21
- "mobx",
22
- "@ikas/bp-storefront",
23
- "@ikas/bp-storefront-models",
24
- "@ikas/bp-storefront-config",
25
- "@ikas/bp-storefront-api"
36
+ "mobx"
26
37
  ];
27
38
  const CLIENT_EXTERNALS = [
28
39
  "preact",
29
40
  "preact/hooks",
30
- "mobx",
31
- "@ikas/bp-storefront",
32
- "@ikas/bp-storefront-models",
33
- "@ikas/bp-storefront-config"
41
+ "mobx"
34
42
  ];
35
43
  /**
36
44
  * Compile a single component's TSX and CSS
@@ -46,7 +54,7 @@ export async function compileComponent(entryPath, stylesPath, componentId) {
46
54
  target: "node18",
47
55
  write: false,
48
56
  external: SERVER_EXTERNALS,
49
- plugins: [ikasComponentUtilsPlugin()],
57
+ plugins: [forbidDeepStorefrontImportsPlugin(), ikasComponentUtilsPlugin()],
50
58
  });
51
59
  // Build client bundle (for hydration)
52
60
  const clientResult = await esbuild.build({
@@ -57,7 +65,7 @@ export async function compileComponent(entryPath, stylesPath, componentId) {
57
65
  target: "es2020",
58
66
  write: false,
59
67
  external: CLIENT_EXTERNALS,
60
- plugins: [ikasComponentUtilsPlugin()],
68
+ plugins: [forbidDeepStorefrontImportsPlugin(), ikasComponentUtilsPlugin()],
61
69
  });
62
70
  // Read CSS (with import resolution)
63
71
  const cssPath = path.resolve(process.cwd(), stylesPath);
@@ -86,11 +94,15 @@ export async function compileComponent(entryPath, stylesPath, componentId) {
86
94
  }
87
95
  }
88
96
  /**
89
- * Scope CSS selectors with a component-specific class prefix
97
+ * Scope CSS selectors with a component-specific class prefix, then namespace any
98
+ * document-global CSS identifiers (`@keyframes`, `@font-face` font-family,
99
+ * `@counter-style`) DEFINED in this component so two components — or the host theme —
100
+ * declaring the same name can't collide in the page's global namespace.
90
101
  */
91
102
  export function scopeCSS(css, componentId) {
92
103
  const scopeClass = `cc_${componentId.replace(/[^a-zA-Z0-9]/g, "_")}`;
93
- return scopeCSSWithClasses(css, [scopeClass]);
104
+ const scoped = scopeCSSWithClasses(css, [scopeClass]);
105
+ return renameCollidingGlobals(scoped, `${scopeClass}_`);
94
106
  }
95
107
  /**
96
108
  * Scope CSS selectors with multiple class prefixes (for shared chunks).
@@ -100,63 +112,519 @@ export function scopeCSSMulti(css, componentIds) {
100
112
  const scopeClasses = componentIds.map(id => `cc_${id.replace(/[^a-zA-Z0-9]/g, "_")}`);
101
113
  return scopeCSSWithClasses(css, scopeClasses);
102
114
  }
115
+ // Conditional group at-rules whose blocks nest normal style rules and must be
116
+ // scoped recursively. This is an ALLOWLIST on purpose: at-rules NOT listed here
117
+ // (@keyframes, @font-face, @page, @property, @counter-style, ...) have non-selector
118
+ // bodies — e.g. @keyframes contains `0%`/`from` keyframe selectors that must NOT be
119
+ // prefixed — so their bodies are copied through untouched. Vendor-prefixed conditional
120
+ // rules (@-moz-document) match via the optional prefix group; @-webkit-keyframes does
121
+ // NOT match (the alternation excludes "keyframes"), which is what we want.
122
+ //
123
+ // NOTE: This scoper is duplicated in editor-models/src/helpers/code-component-css-scope.ts
124
+ // (the editor-preview / code-generator path). The CLI ships standalone to npm and cannot
125
+ // import that private package, so the two intentionally diverge. Keep parsing fixes in sync.
126
+ const NESTING_AT_RULES = /^@(?:-\w+-)?(media|supports|container|layer|document|scope|starting-style)\b/i;
103
127
  /**
104
- * Internal: scope CSS selectors with one or more scope class prefixes
128
+ * Internal: scope CSS selectors with one or more scope class prefixes.
129
+ *
130
+ * Character-based walker, NOT line-based: a rule's selector list is everything up
131
+ * to its opening `{`, so lists written across multiple lines (`.a,\n.b { ... }`)
132
+ * are scoped as a whole. The previous line-based implementation only scoped the
133
+ * selector fragment sharing a line with `{`, leaving the other selectors unscoped
134
+ * (they either lost the cascade to scoped rules or leaked into other components).
135
+ *
136
+ * Comments, string literals, and `\` escapes are treated as inert spans (via
137
+ * `skipInertSpan`) by every structural scanner, so a `{`, `}`, `;`, or `,` that
138
+ * appears inside a comment / string / escape can never be mistaken for real
139
+ * structure (which would otherwise drop the scope prefix and leak styles globally).
105
140
  */
106
141
  function scopeCSSWithClasses(css, scopeClasses) {
107
- const lines = css.split("\n");
108
- const scopedLines = [];
109
- let inMediaQuery = false;
110
- let keyframesDepth = 0;
111
- for (const line of lines) {
112
- const trimmed = line.trim();
113
- if (!trimmed) {
114
- scopedLines.push(line);
142
+ let out = "";
143
+ for (let i = 0; i < css.length;) {
144
+ const ch = css[i];
145
+ // Copy comments verbatim
146
+ if (ch === "/" && css[i + 1] === "*") {
147
+ const end = css.indexOf("*/", i + 2);
148
+ if (end === -1)
149
+ return out + css.slice(i);
150
+ out += css.slice(i, end + 2);
151
+ i = end + 2;
115
152
  continue;
116
153
  }
117
- if (trimmed.startsWith("@media")) {
118
- inMediaQuery = true;
119
- scopedLines.push(line);
154
+ // Copy whitespace and stray closing braces
155
+ if (/\s/.test(ch) || ch === "}") {
156
+ out += ch;
157
+ i++;
120
158
  continue;
121
159
  }
122
- if (trimmed.startsWith("@keyframes") || trimmed.startsWith("@-webkit-keyframes")) {
123
- keyframesDepth = 1;
124
- scopedLines.push(line);
160
+ if (ch === "@") {
161
+ const headerEnd = findAtRuleHeaderEnd(css, i);
162
+ // Block-less at-rule (@import, @charset, @layer a;, ...) — copy through
163
+ if (headerEnd >= css.length || css[headerEnd] === ";") {
164
+ out += css.slice(i, Math.min(headerEnd + 1, css.length));
165
+ i = headerEnd + 1;
166
+ continue;
167
+ }
168
+ const header = css.slice(i, headerEnd);
169
+ const blockEnd = findBlockEnd(css, headerEnd + 1);
170
+ const body = css.slice(headerEnd + 1, blockEnd);
171
+ const transformed = NESTING_AT_RULES.test(header.trim())
172
+ ? scopeCSSWithClasses(body, scopeClasses)
173
+ : body;
174
+ // Always emit the closing brace: when the source block is unterminated
175
+ // (blockEnd === css.length) this repairs it, so concatenated component CSS
176
+ // can't leak one component's block over the next.
177
+ out += `${header}{${transformed}}`;
178
+ i = blockEnd + 1;
179
+ continue;
180
+ }
181
+ // Normal style rule: selector list runs up to the rule's opening `{`
182
+ const selEnd = findSelectorListEnd(css, i);
183
+ if (selEnd >= css.length) {
184
+ // Trailing content with no block (e.g. a dangling declaration at EOF) — copy through
185
+ out += css.slice(i);
186
+ break;
187
+ }
188
+ if (css[selEnd] === ";") {
189
+ // A stray top-level `;` (or a stray declaration fragment ending in `;`) — not a rule.
190
+ // DROP it. Copying it through is not enough: per the CSS syntax spec a browser folds a
191
+ // stray `;` into the NEXT rule's prelude when consuming a qualified rule, which makes
192
+ // that selector invalid and drops the whole following rule. Dropping the stray token
193
+ // keeps the next rule valid and correctly scoped.
194
+ i = selEnd + 1;
195
+ continue;
196
+ }
197
+ if (css[selEnd] === "}") {
198
+ // Content before a stray closing brace — copy through verbatim and let the main loop
199
+ // emit the `}`.
200
+ out += css.slice(i, selEnd);
201
+ i = selEnd;
202
+ continue;
203
+ }
204
+ const selectorList = css.slice(i, selEnd);
205
+ const blockEnd = findBlockEnd(css, selEnd + 1);
206
+ const body = css.slice(selEnd + 1, blockEnd);
207
+ const scopedSelector = splitSelectorList(selectorList)
208
+ .map(s => s.trim())
209
+ .filter(Boolean)
210
+ .flatMap(s => scopeClasses.map(cls => `.${cls} ${s}`))
211
+ .join(", ");
212
+ // Always emit the closing brace (see at-rule branch above for rationale).
213
+ out += `${scopedSelector} {${body}}`;
214
+ i = blockEnd + 1;
215
+ }
216
+ return out;
217
+ }
218
+ /**
219
+ * If an inert span — a `/* ... *\/` comment, a `"..."` / `'...'` string literal, an
220
+ * unquoted `url(...)` token, or a `\x` escape — starts at `s[i]`, return the index just
221
+ * past it; otherwise return -1.
222
+ *
223
+ * Every structural scanner below funnels through this so that delimiters (`{ } ; , ( )`)
224
+ * living inside comments, strings, urls, or escapes are never treated as structure. String
225
+ * scanning honors `\` escapes and stops at a raw newline, matching CSS "bad-string" error
226
+ * recovery (an unterminated string ends at the line break) so a missing quote can't
227
+ * swallow every rule that follows it.
228
+ */
229
+ function skipInertSpan(s, i) {
230
+ const c = s[i];
231
+ if (c === "/" && s[i + 1] === "*") {
232
+ const end = s.indexOf("*/", i + 2);
233
+ return end === -1 ? s.length : end + 2;
234
+ }
235
+ if (c === '"' || c === "'") {
236
+ for (let j = i + 1; j < s.length; j++) {
237
+ const d = s[j];
238
+ if (d === "\\") {
239
+ j++; // escape: skip the next char
240
+ continue;
241
+ }
242
+ if (d === c)
243
+ return j + 1; // closing quote
244
+ if (d === "\n" || d === "\r" || d === "\f")
245
+ return j; // bad-string ends at newline
246
+ }
247
+ return s.length;
248
+ }
249
+ // Unquoted url(...) token: per the CSS tokenizer its content is opaque, so braces,
250
+ // semicolons, and parens inside it must NOT be read as structure (an unbalanced `}` in
251
+ // `url(x}y.png)` or `{` in an inline SVG data-uri would otherwise mis-terminate the
252
+ // enclosing block). A QUOTED url (`url("...")`) is a normal function + string and is left
253
+ // to the string branch above. `url` must be a standalone ident, not the tail of a longer
254
+ // identifier like `myurl(`.
255
+ if ((c === "u" || c === "U") && /^url\(/i.test(s.slice(i, i + 4))) {
256
+ const prev = i > 0 ? s[i - 1] : "";
257
+ if (!/[A-Za-z0-9_-]/.test(prev)) {
258
+ let j = i + 4;
259
+ while (j < s.length && /\s/.test(s[j]))
260
+ j++; // optional leading whitespace
261
+ if (s[j] !== '"' && s[j] !== "'") {
262
+ for (; j < s.length; j++) {
263
+ if (s[j] === "\\") {
264
+ j++; // escaped char inside the url (e.g. `url(foo\)bar)`)
265
+ continue;
266
+ }
267
+ if (s[j] === ")")
268
+ return j + 1; // end of url token
269
+ }
270
+ return s.length; // unterminated url() — consume to EOF (bad-url)
271
+ }
272
+ }
273
+ }
274
+ if (c === "\\")
275
+ return i + 2; // escaped char outside a string (e.g. `.a\,b`)
276
+ return -1;
277
+ }
278
+ /** Find the first top-level `{` or `;` that ends an at-rule's header. Respects `()` / `[]`
279
+ * and inert spans (strings/comments/escapes). */
280
+ function findAtRuleHeaderEnd(css, start) {
281
+ let pDepth = 0;
282
+ for (let j = start; j < css.length;) {
283
+ const skip = skipInertSpan(css, j);
284
+ if (skip !== -1) {
285
+ j = skip;
286
+ continue;
287
+ }
288
+ const c = css[j];
289
+ if (c === "(" || c === "[")
290
+ pDepth++;
291
+ else if (c === ")" || c === "]")
292
+ pDepth--;
293
+ else if (pDepth === 0 && (c === "{" || c === ";"))
294
+ return j;
295
+ j++;
296
+ }
297
+ return css.length;
298
+ }
299
+ /**
300
+ * Find the matching `}` for the block whose body starts at `blockStart` (char after `{`).
301
+ * Inert spans are skipped so braces inside `content: "}"`, comments, or escapes don't
302
+ * terminate the block early.
303
+ */
304
+ function findBlockEnd(css, blockStart) {
305
+ let depth = 1;
306
+ for (let j = blockStart; j < css.length;) {
307
+ const skip = skipInertSpan(css, j);
308
+ if (skip !== -1) {
309
+ j = skip;
310
+ continue;
311
+ }
312
+ const c = css[j];
313
+ if (c === "{")
314
+ depth++;
315
+ else if (c === "}") {
316
+ depth--;
317
+ if (depth === 0)
318
+ return j;
319
+ }
320
+ j++;
321
+ }
322
+ return css.length;
323
+ }
324
+ /** Find the `{`, `}`, or `;` that terminates a selector list. A `}` or `;` here means the
325
+ * run was not a real style rule. Respects `()` / `[]` and inert spans. */
326
+ function findSelectorListEnd(css, start) {
327
+ let pDepth = 0;
328
+ for (let j = start; j < css.length;) {
329
+ const skip = skipInertSpan(css, j);
330
+ if (skip !== -1) {
331
+ j = skip;
332
+ continue;
333
+ }
334
+ const c = css[j];
335
+ if (c === "(" || c === "[")
336
+ pDepth++;
337
+ else if (c === ")" || c === "]")
338
+ pDepth--;
339
+ else if (pDepth === 0 && (c === "{" || c === "}" || c === ";"))
340
+ return j;
341
+ j++;
342
+ }
343
+ return css.length;
344
+ }
345
+ /** Split a selector list on top-level commas. Respects `()` / `[]` and inert spans, so a
346
+ * comma inside a comment, string, or `\` escape (e.g. `.a\,b`) does not split the list. */
347
+ function splitSelectorList(s) {
348
+ const result = [];
349
+ let depth = 0;
350
+ let start = 0;
351
+ for (let i = 0; i < s.length;) {
352
+ const skip = skipInertSpan(s, i);
353
+ if (skip !== -1) {
354
+ i = skip;
355
+ continue;
356
+ }
357
+ const ch = s[i];
358
+ if (ch === "(" || ch === "[")
359
+ depth++;
360
+ else if (ch === ")" || ch === "]")
361
+ depth--;
362
+ else if (ch === "," && depth === 0) {
363
+ result.push(s.slice(start, i));
364
+ start = i + 1;
365
+ }
366
+ i++;
367
+ }
368
+ result.push(s.slice(start));
369
+ return result;
370
+ }
371
+ /* ============================================================================
372
+ * Global-identifier namespacing.
373
+ *
374
+ * `@keyframes`, `@font-face` font-family names, and `@counter-style` names live in a
375
+ * DOCUMENT-GLOBAL namespace — scoping selectors with `.cc_<id>` does not isolate them, so
376
+ * two components (or the theme) defining the same name would collide (last definition wins
377
+ * document-wide). `renameCollidingGlobals` prefixes every such name DEFINED in this CSS,
378
+ * plus its references within the SAME CSS, with the component's scope prefix.
379
+ *
380
+ * Only names defined in THIS css are renamed — a reference to a name defined elsewhere
381
+ * (global.css, a shared chunk, the theme) is left untouched, so cross-file references keep
382
+ * resolving. Shared-chunk CSS (scopeCSSMulti) is intentionally NOT renamed, to avoid
383
+ * breaking a chunk-defined keyframe referenced from a consuming component's own CSS.
384
+ *
385
+ * NOTE: ported from packages/editor-models/src/helpers/code-component-css-scope.ts
386
+ * (renameCollidingGlobals + helpers, the global.css path's `cc_<projectId>_` variant).
387
+ * Keep parsing/rename fixes in sync between the two.
388
+ * ============================================================================ */
389
+ // Declarations whose VALUE may reference a global identifier of the given type.
390
+ const KEYFRAME_REF_PROPS = new Set(["animation", "animation-name"]);
391
+ const FONT_FAMILY_REF_PROPS = new Set(["font", "font-family"]);
392
+ const COUNTER_STYLE_REF_PROPS = new Set(["list-style", "list-style-type"]);
393
+ function renameCollidingGlobals(css, prefix) {
394
+ // Comment/string/url-masked copy: defining-site regex scans run against this so a name
395
+ // inside a comment or `content: "..."` string can't contribute a rename or be rewritten.
396
+ const scanCss = maskInertSpans(css);
397
+ const keyframeNames = new Set();
398
+ const fontFamilyNames = new Set();
399
+ const counterStyleNames = new Set();
400
+ let out = css;
401
+ out = out.replace(/@keyframes(\s+)([\w-]+)/g, (m, ws, name, offset) => {
402
+ if (scanCss.substr(offset, 10) !== "@keyframes")
403
+ return m;
404
+ keyframeNames.add(name);
405
+ return `@keyframes${ws}${prefix}${name}`;
406
+ });
407
+ out = out.replace(/@counter-style(\s+)([\w-]+)/g, (m, ws, name, offset) => {
408
+ if (scanCss.substr(offset, 14) !== "@counter-style")
409
+ return m;
410
+ counterStyleNames.add(name);
411
+ return `@counter-style${ws}${prefix}${name}`;
412
+ });
413
+ out = rewriteFontFaceBlocks(out, fontFamilyNames, prefix);
414
+ if (keyframeNames.size === 0 && fontFamilyNames.size === 0 && counterStyleNames.size === 0) {
415
+ return out;
416
+ }
417
+ return rewriteDeclarationReferences(out, keyframeNames, fontFamilyNames, counterStyleNames, prefix);
418
+ }
419
+ /** Replace every inert span (comment / string / url() / escape) with equal-length spaces so
420
+ * position-based regex scans still align but can't match tokens hidden inside them. */
421
+ function maskInertSpans(css) {
422
+ let out = "";
423
+ for (let i = 0; i < css.length;) {
424
+ const skip = skipInertSpan(css, i);
425
+ if (skip !== -1) {
426
+ out += " ".repeat(skip - i);
427
+ i = skip;
125
428
  continue;
126
429
  }
127
- // Inside @keyframes: track brace depth, pass lines through unscoped
128
- if (keyframesDepth > 0) {
129
- for (const ch of trimmed) {
130
- if (ch === "{")
131
- keyframesDepth++;
132
- else if (ch === "}")
133
- keyframesDepth--;
430
+ out += css[i];
431
+ i++;
432
+ }
433
+ return out;
434
+ }
435
+ function rewriteFontFaceBlocks(css, collect, prefix) {
436
+ const matches = [];
437
+ css.replace(/@font-face\s*\{/g, (m, offset) => {
438
+ matches.push({ index: offset, length: m.length });
439
+ return m;
440
+ });
441
+ if (matches.length === 0)
442
+ return css;
443
+ let out = "";
444
+ let lastIdx = 0;
445
+ for (const match of matches) {
446
+ const openBraceIdx = match.index + match.length - 1;
447
+ if (openBraceIdx < lastIdx)
448
+ continue;
449
+ const endIdx = findBlockEnd(css, openBraceIdx + 1);
450
+ const before = css.slice(lastIdx, openBraceIdx + 1);
451
+ const body = css.slice(openBraceIdx + 1, endIdx);
452
+ const rewrittenBody = body.replace(/(font-family\s*:\s*)(?:"([^"]+)"|'([^']+)'|([\w-]+))/i, (_m, pre, dq, sq, bare) => {
453
+ const name = dq || sq || bare;
454
+ if (!name)
455
+ return _m;
456
+ collect.add(name);
457
+ const prefixed = `${prefix}${name}`;
458
+ if (dq)
459
+ return `${pre}"${prefixed}"`;
460
+ if (sq)
461
+ return `${pre}'${prefixed}'`;
462
+ return `${pre}${prefixed}`;
463
+ });
464
+ out += before + rewrittenBody + "}";
465
+ lastIdx = endIdx + 1;
466
+ }
467
+ out += css.slice(lastIdx);
468
+ return out;
469
+ }
470
+ function rewriteDeclarationReferences(css, keyframeNames, fontFamilyNames, counterStyleNames, prefix) {
471
+ const keyframeRe = buildBareIdentRegex(keyframeNames);
472
+ const fontFamilyRe = buildFontFamilyValueRegex(fontFamilyNames);
473
+ const counterStyleRe = buildBareIdentRegex(counterStyleNames);
474
+ // counter(<name>, <style>?) / counters(<name>, <string>, <style>?) — the counter-style
475
+ // identifier, when present, is always the LAST optional argument.
476
+ const counterFnRe = counterStyleNames.size > 0
477
+ ? new RegExp(`(\\bcounters?\\([^)]*?,\\s*)(${bareUnion(counterStyleNames)})(\\s*\\))`, "gi")
478
+ : null;
479
+ return mapRuleBlocks(css, block => {
480
+ return mapDeclarations(block, (prop, value) => {
481
+ let v = value;
482
+ const lower = prop.toLowerCase();
483
+ if (keyframeRe && KEYFRAME_REF_PROPS.has(lower)) {
484
+ v = v.replace(keyframeRe, m => `${prefix}${m}`);
134
485
  }
135
- scopedLines.push(line);
486
+ if (fontFamilyRe && FONT_FAMILY_REF_PROPS.has(lower)) {
487
+ v = v.replace(fontFamilyRe, (m, dq, sq, bare) => {
488
+ const name = dq || sq || bare;
489
+ if (!name || !fontFamilyNames.has(name))
490
+ return m;
491
+ if (dq !== undefined)
492
+ return `"${prefix}${name}"`;
493
+ if (sq !== undefined)
494
+ return `'${prefix}${name}'`;
495
+ return `${prefix}${name}`;
496
+ });
497
+ }
498
+ if (counterStyleRe && COUNTER_STYLE_REF_PROPS.has(lower)) {
499
+ v = v.replace(counterStyleRe, m => `${prefix}${m}`);
500
+ }
501
+ if (counterFnRe) {
502
+ v = v.replace(counterFnRe, (_m, pre, name, post) => `${pre}${prefix}${name}${post}`);
503
+ }
504
+ return v;
505
+ });
506
+ });
507
+ }
508
+ function bareUnion(names) {
509
+ return Array.from(names).map(escapeRegex).join("|");
510
+ }
511
+ function buildBareIdentRegex(names) {
512
+ if (names.size === 0)
513
+ return null;
514
+ return new RegExp(`(?<![\\w-])(?:${bareUnion(names)})(?![\\w-])`, "g");
515
+ }
516
+ function buildFontFamilyValueRegex(names) {
517
+ if (names.size === 0)
518
+ return null;
519
+ const u = bareUnion(names);
520
+ return new RegExp(`"(${u})"|'(${u})'|(?<![\\w-])(${u})(?![\\w-])`, "g");
521
+ }
522
+ function escapeRegex(s) {
523
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
524
+ }
525
+ /**
526
+ * Walk every top-level rule block `{ ... }` (recursing into nested at-rule contents) and
527
+ * pass the block body through `transform`. Inert spans are skipped so a `{` inside a string
528
+ * or `url(a{b)` is not mistaken for a block. At-rule preambles and inter-rule whitespace
529
+ * are left untouched.
530
+ */
531
+ function mapRuleBlocks(css, transform) {
532
+ let out = "";
533
+ for (let i = 0; i < css.length;) {
534
+ const skip = skipInertSpan(css, i);
535
+ if (skip !== -1) {
536
+ out += css.slice(i, skip);
537
+ i = skip;
538
+ continue;
539
+ }
540
+ if (css[i] === "{") {
541
+ const blockEnd = findBlockEnd(css, i + 1);
542
+ const body = css.slice(i + 1, blockEnd);
543
+ const processed = hasTopLevelBrace(body) ? mapRuleBlocks(body, transform) : transform(body);
544
+ out += "{" + processed + "}";
545
+ i = blockEnd + 1;
136
546
  continue;
137
547
  }
138
- if (trimmed === "}" && inMediaQuery) {
139
- inMediaQuery = false;
140
- scopedLines.push(line);
548
+ out += css[i];
549
+ i++;
550
+ }
551
+ return out;
552
+ }
553
+ /** Inert-span-aware: true iff a real `{` appears outside comments/strings/url()/escapes. */
554
+ function hasTopLevelBrace(s) {
555
+ for (let i = 0; i < s.length;) {
556
+ const skip = skipInertSpan(s, i);
557
+ if (skip !== -1) {
558
+ i = skip;
141
559
  continue;
142
560
  }
143
- if (!trimmed.includes("{") || trimmed.startsWith("@")) {
144
- scopedLines.push(line);
561
+ if (s[i] === "{")
562
+ return true;
563
+ i++;
564
+ }
565
+ return false;
566
+ }
567
+ /**
568
+ * Split a rule block body into `prop: value` declarations on top-level `;`, apply
569
+ * `transform(prop, value)` to each, and rejoin preserving separators. Inert spans are
570
+ * skipped so a `;` inside a string or `url(a;b)` doesn't split a declaration.
571
+ */
572
+ function mapDeclarations(body, transform) {
573
+ const parts = [];
574
+ let start = 0;
575
+ let pDepth = 0;
576
+ for (let i = 0; i < body.length;) {
577
+ const skip = skipInertSpan(body, i);
578
+ if (skip !== -1) {
579
+ i = skip;
145
580
  continue;
146
581
  }
147
- const [selector, rest] = line.split("{");
148
- if (selector && rest !== undefined) {
149
- const scopedSelector = selector
150
- .split(",")
151
- .flatMap((s) => scopeClasses.map(cls => `.${cls} ${s.trim()}`))
152
- .join(", ");
153
- scopedLines.push(`${scopedSelector} {${rest}`);
582
+ const c = body[i];
583
+ if (c === "(" || c === "[" || c === "{")
584
+ pDepth++;
585
+ else if (c === ")" || c === "]" || c === "}")
586
+ pDepth--;
587
+ else if (c === ";" && pDepth === 0) {
588
+ parts.push(body.slice(start, i));
589
+ start = i + 1;
154
590
  }
155
- else {
156
- scopedLines.push(line);
591
+ i++;
592
+ }
593
+ parts.push(body.slice(start));
594
+ return parts
595
+ .map((part, idx) => {
596
+ const isLast = idx === parts.length - 1;
597
+ const colonIdx = findTopLevelColon(part);
598
+ if (colonIdx === -1)
599
+ return part + (isLast ? "" : ";");
600
+ const valueRaw = part.slice(colonIdx + 1);
601
+ const leading = valueRaw.match(/^\s*/)?.[0] ?? "";
602
+ const trailing = valueRaw.match(/\s*$/)?.[0] ?? "";
603
+ const prop = part.slice(0, colonIdx).trim();
604
+ const value = valueRaw.slice(leading.length, valueRaw.length - trailing.length);
605
+ const transformed = transform(prop, value);
606
+ return part.slice(0, colonIdx) + ":" + leading + transformed + trailing + (isLast ? "" : ";");
607
+ })
608
+ .join("");
609
+ }
610
+ function findTopLevelColon(s) {
611
+ let pDepth = 0;
612
+ for (let i = 0; i < s.length;) {
613
+ const skip = skipInertSpan(s, i);
614
+ if (skip !== -1) {
615
+ i = skip;
616
+ continue;
157
617
  }
618
+ const c = s[i];
619
+ if (c === "(" || c === "[")
620
+ pDepth++;
621
+ else if (c === ")" || c === "]")
622
+ pDepth--;
623
+ else if (c === ":" && pDepth === 0)
624
+ return i;
625
+ i++;
158
626
  }
159
- return scopedLines.join("\n");
627
+ return -1;
160
628
  }
161
629
  /**
162
630
  * Common esbuild options shared between server and client builds
@@ -165,14 +633,14 @@ function commonBuildOptions() {
165
633
  return {
166
634
  bundle: true,
167
635
  jsx: "transform",
168
- jsxFactory: "h",
169
- jsxFragment: "Fragment",
636
+ jsxFactory: "__ikas_h",
637
+ jsxFragment: "__ikas_Fragment",
170
638
  inject: [PREACT_JSX_SHIM],
171
639
  tsconfigRaw: {
172
640
  compilerOptions: {
173
641
  jsx: "react",
174
- jsxFactory: "h",
175
- jsxFragmentFactory: "Fragment"
642
+ jsxFactory: "__ikas_h",
643
+ jsxFragmentFactory: "__ikas_Fragment"
176
644
  }
177
645
  },
178
646
  minify: true,
@@ -214,7 +682,7 @@ export async function compileAllComponents(components, globalStylesPath) {
214
682
  platform: "node",
215
683
  target: "node18",
216
684
  external: SERVER_EXTERNALS,
217
- plugins: [ikasComponentUtilsPlugin()],
685
+ plugins: [forbidDeepStorefrontImportsPlugin(), ikasComponentUtilsPlugin()],
218
686
  splitting: true,
219
687
  outdir: serverDir,
220
688
  entryNames: "[name]",
@@ -228,7 +696,7 @@ export async function compileAllComponents(components, globalStylesPath) {
228
696
  platform: "browser",
229
697
  target: "es2020",
230
698
  external: CLIENT_EXTERNALS,
231
- plugins: [ikasComponentUtilsPlugin()],
699
+ plugins: [forbidDeepStorefrontImportsPlugin(), ikasComponentUtilsPlugin()],
232
700
  splitting: true,
233
701
  outdir: clientDir,
234
702
  entryNames: "[name]",