@griffel/transform 3.0.4 → 3.0.5

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 (188) hide show
  1. package/CHANGELOG.json +404 -0
  2. package/CHANGELOG.md +130 -0
  3. package/__fixtures__/assets/blank.jpg +0 -0
  4. package/__fixtures__/assets/code.ts +12 -0
  5. package/__fixtures__/assets/empty.jpg +0 -0
  6. package/__fixtures__/assets/output.meta.json +12 -0
  7. package/__fixtures__/assets/output.ts +12 -0
  8. package/__fixtures__/assets-multiple-declarations/blank.jpg +0 -0
  9. package/__fixtures__/assets-multiple-declarations/code.ts +8 -0
  10. package/__fixtures__/assets-multiple-declarations/empty.jpg +0 -0
  11. package/__fixtures__/assets-multiple-declarations/output.meta.json +9 -0
  12. package/__fixtures__/assets-multiple-declarations/output.ts +6 -0
  13. package/__fixtures__/assets-reset-styles/blank.jpg +0 -0
  14. package/__fixtures__/assets-reset-styles/code.ts +11 -0
  15. package/__fixtures__/assets-reset-styles/empty.jpg +0 -0
  16. package/__fixtures__/assets-reset-styles/output.meta.json +11 -0
  17. package/__fixtures__/assets-reset-styles/output.ts +7 -0
  18. package/__fixtures__/assets-urls/code.ts +13 -0
  19. package/__fixtures__/assets-urls/output.meta.json +14 -0
  20. package/__fixtures__/assets-urls/output.ts +10 -0
  21. package/__fixtures__/at-rules/code.ts +14 -0
  22. package/__fixtures__/at-rules/output.meta.json +15 -0
  23. package/__fixtures__/at-rules/output.ts +10 -0
  24. package/__fixtures__/config-classname-hash-salt/code.ts +5 -0
  25. package/__fixtures__/config-classname-hash-salt/output.meta.json +7 -0
  26. package/__fixtures__/config-classname-hash-salt/output.ts +3 -0
  27. package/__fixtures__/config-evaluation-rules/code.ts +8 -0
  28. package/__fixtures__/config-evaluation-rules/consts.ts +1 -0
  29. package/__fixtures__/config-evaluation-rules/output.meta.json +7 -0
  30. package/__fixtures__/config-evaluation-rules/output.ts +4 -0
  31. package/__fixtures__/config-evaluation-rules/sampleEvaluator.cjs +12 -0
  32. package/__fixtures__/error-argument-count/fixture.js +4 -0
  33. package/__fixtures__/error-cjs/fixture.js +4 -0
  34. package/__fixtures__/error-on-undefined/fixture.ts +7 -0
  35. package/__fixtures__/error-on-undefined/tokens.ts +1 -0
  36. package/__fixtures__/export-default/code.ts +6 -0
  37. package/__fixtures__/export-default/output.meta.json +14 -0
  38. package/__fixtures__/export-default/output.ts +6 -0
  39. package/__fixtures__/function-mixin/code.ts +6 -0
  40. package/__fixtures__/function-mixin/mixins.ts +7 -0
  41. package/__fixtures__/function-mixin/output.meta.json +7 -0
  42. package/__fixtures__/function-mixin/output.ts +4 -0
  43. package/__fixtures__/function-mixin/tokens.ts +3 -0
  44. package/__fixtures__/import-alias/code.ts +5 -0
  45. package/__fixtures__/import-alias/output.meta.json +7 -0
  46. package/__fixtures__/import-alias/output.ts +3 -0
  47. package/__fixtures__/import-custom-module/code.ts +6 -0
  48. package/__fixtures__/import-custom-module/output.meta.json +7 -0
  49. package/__fixtures__/import-custom-module/output.ts +4 -0
  50. package/__fixtures__/keyframes/code.ts +22 -0
  51. package/__fixtures__/keyframes/output.meta.json +17 -0
  52. package/__fixtures__/keyframes/output.ts +6 -0
  53. package/__fixtures__/multiple-declarations/code.ts +8 -0
  54. package/__fixtures__/multiple-declarations/output.meta.json +7 -0
  55. package/__fixtures__/multiple-declarations/output.ts +4 -0
  56. package/__fixtures__/non-existing-module-call/code.ts +10 -0
  57. package/__fixtures__/non-existing-module-call/module.ts +13 -0
  58. package/__fixtures__/non-existing-module-call/output.meta.json +7 -0
  59. package/__fixtures__/non-existing-module-call/output.ts +6 -0
  60. package/__fixtures__/object/code.ts +6 -0
  61. package/__fixtures__/object/output.meta.json +14 -0
  62. package/__fixtures__/object/output.ts +6 -0
  63. package/__fixtures__/object-computed-keys/code.ts +8 -0
  64. package/__fixtures__/object-computed-keys/output.meta.json +14 -0
  65. package/__fixtures__/object-computed-keys/output.ts +8 -0
  66. package/__fixtures__/object-imported-keys/code.ts +9 -0
  67. package/__fixtures__/object-imported-keys/consts.ts +2 -0
  68. package/__fixtures__/object-imported-keys/output.meta.json +7 -0
  69. package/__fixtures__/object-imported-keys/output.ts +4 -0
  70. package/__fixtures__/object-mixins/code.ts +11 -0
  71. package/__fixtures__/object-mixins/mixins.ts +17 -0
  72. package/__fixtures__/object-mixins/output.meta.json +16 -0
  73. package/__fixtures__/object-mixins/output.ts +10 -0
  74. package/__fixtures__/object-mixins/tokens.ts +3 -0
  75. package/__fixtures__/object-nesting/code.ts +13 -0
  76. package/__fixtures__/object-nesting/consts.ts +1 -0
  77. package/__fixtures__/object-nesting/output.meta.json +9 -0
  78. package/__fixtures__/object-nesting/output.ts +6 -0
  79. package/__fixtures__/object-reset/code.ts +8 -0
  80. package/__fixtures__/object-reset/output.meta.json +7 -0
  81. package/__fixtures__/object-reset/output.ts +5 -0
  82. package/__fixtures__/object-sequence-expr/code.ts +16 -0
  83. package/__fixtures__/object-sequence-expr/output.meta.json +7 -0
  84. package/__fixtures__/object-sequence-expr/output.ts +7 -0
  85. package/__fixtures__/object-shorthands/code.ts +8 -0
  86. package/__fixtures__/object-shorthands/output.meta.json +20 -0
  87. package/__fixtures__/object-shorthands/output.ts +5 -0
  88. package/__fixtures__/object-variables/code.ts +9 -0
  89. package/__fixtures__/object-variables/output.meta.json +15 -0
  90. package/__fixtures__/object-variables/output.ts +9 -0
  91. package/__fixtures__/object-variables/vars.ts +5 -0
  92. package/__fixtures__/reset-styles/code.ts +6 -0
  93. package/__fixtures__/reset-styles/output.meta.json +7 -0
  94. package/__fixtures__/reset-styles/output.ts +3 -0
  95. package/__fixtures__/reset-styles-at-rules/code.ts +7 -0
  96. package/__fixtures__/reset-styles-at-rules/output.meta.json +8 -0
  97. package/__fixtures__/reset-styles-at-rules/output.ts +3 -0
  98. package/__fixtures__/rules-with-metadata/code.ts +9 -0
  99. package/__fixtures__/rules-with-metadata/output.meta.json +14 -0
  100. package/__fixtures__/rules-with-metadata/output.ts +3 -0
  101. package/__fixtures__/shared-mixins/code.ts +7 -0
  102. package/__fixtures__/shared-mixins/mixins.ts +6 -0
  103. package/__fixtures__/shared-mixins/output.meta.json +7 -0
  104. package/__fixtures__/shared-mixins/output.ts +8 -0
  105. package/__fixtures__/static-styles/code.ts +7 -0
  106. package/__fixtures__/static-styles/output.meta.json +7 -0
  107. package/__fixtures__/static-styles/output.ts +3 -0
  108. package/__fixtures__/static-styles-array/code.ts +10 -0
  109. package/__fixtures__/static-styles-array/output.meta.json +7 -0
  110. package/__fixtures__/static-styles-array/output.ts +3 -0
  111. package/__fixtures__/static-styles-string/code.ts +3 -0
  112. package/__fixtures__/static-styles-string/output.meta.json +7 -0
  113. package/__fixtures__/static-styles-string/output.ts +3 -0
  114. package/__fixtures__/tokens/code.ts +11 -0
  115. package/__fixtures__/tokens/output.meta.json +12 -0
  116. package/__fixtures__/tokens/output.ts +7 -0
  117. package/__fixtures__/tokens/tokens.ts +4 -0
  118. package/__fixtures__/unsupported-css-properties/fixture.ts +16 -0
  119. package/__fixtures__/unsupported-css-properties/output.meta.json +5 -0
  120. package/__fixtures__/unsupported-css-properties/output.ts +3 -0
  121. package/eslint.config.mjs +31 -0
  122. package/package.json +7 -8
  123. package/project.json +51 -0
  124. package/src/{constants.mjs → constants.mts} +0 -1
  125. package/src/evaluation/astEvaluator.mts +109 -0
  126. package/src/evaluation/astEvaluator.test.mts +126 -0
  127. package/src/evaluation/batchEvaluator.mts +79 -0
  128. package/src/evaluation/evalCache.mts +84 -0
  129. package/src/evaluation/fluentTokensPlugin.mts +82 -0
  130. package/src/evaluation/fluentTokensPlugin.test.mts +130 -0
  131. package/src/evaluation/module.mts +271 -0
  132. package/src/evaluation/module.test.mts +133 -0
  133. package/src/evaluation/{process.mjs → process.mts} +12 -7
  134. package/src/evaluation/types.mts +49 -0
  135. package/src/evaluation/vmEvaluator.mts +45 -0
  136. package/src/evaluation/vmEvaluator.test.mts +30 -0
  137. package/src/{index.d.mts → index.mts} +4 -0
  138. package/src/transformSync.mts +425 -0
  139. package/src/transformSync.test.mts +429 -0
  140. package/src/types.mts +13 -0
  141. package/src/utils/convertESMtoCJS.mts +226 -0
  142. package/src/utils/convertESMtoCJS.test.mts +159 -0
  143. package/src/utils/dedupeCSSRules.mts +25 -0
  144. package/src/utils/dedupeCSSRules.test.mts +39 -0
  145. package/tsconfig.json +19 -0
  146. package/tsconfig.lib.json +30 -0
  147. package/tsconfig.spec.json +24 -0
  148. package/vitest.config.mts +18 -0
  149. package/LICENSE.md +0 -21
  150. package/src/constants.d.mts +0 -2
  151. package/src/constants.mjs.map +0 -1
  152. package/src/evaluation/astEvaluator.d.mts +0 -20
  153. package/src/evaluation/astEvaluator.mjs +0 -90
  154. package/src/evaluation/astEvaluator.mjs.map +0 -1
  155. package/src/evaluation/batchEvaluator.d.mts +0 -13
  156. package/src/evaluation/batchEvaluator.mjs +0 -54
  157. package/src/evaluation/batchEvaluator.mjs.map +0 -1
  158. package/src/evaluation/evalCache.d.mts +0 -9
  159. package/src/evaluation/evalCache.mjs +0 -65
  160. package/src/evaluation/evalCache.mjs.map +0 -1
  161. package/src/evaluation/fluentTokensPlugin.d.mts +0 -2
  162. package/src/evaluation/fluentTokensPlugin.mjs +0 -70
  163. package/src/evaluation/fluentTokensPlugin.mjs.map +0 -1
  164. package/src/evaluation/module.d.mts +0 -44
  165. package/src/evaluation/module.mjs +0 -207
  166. package/src/evaluation/module.mjs.map +0 -1
  167. package/src/evaluation/process.d.mts +0 -24
  168. package/src/evaluation/process.mjs.map +0 -1
  169. package/src/evaluation/types.d.mts +0 -34
  170. package/src/evaluation/types.mjs +0 -2
  171. package/src/evaluation/types.mjs.map +0 -1
  172. package/src/evaluation/vmEvaluator.d.mts +0 -3
  173. package/src/evaluation/vmEvaluator.mjs +0 -33
  174. package/src/evaluation/vmEvaluator.mjs.map +0 -1
  175. package/src/index.mjs +0 -9
  176. package/src/index.mjs.map +0 -1
  177. package/src/transformSync.d.mts +0 -41
  178. package/src/transformSync.mjs +0 -252
  179. package/src/transformSync.mjs.map +0 -1
  180. package/src/types.d.mts +0 -12
  181. package/src/types.mjs +0 -2
  182. package/src/types.mjs.map +0 -1
  183. package/src/utils/convertESMtoCJS.d.mts +0 -6
  184. package/src/utils/convertESMtoCJS.mjs +0 -203
  185. package/src/utils/convertESMtoCJS.mjs.map +0 -1
  186. package/src/utils/dedupeCSSRules.d.mts +0 -6
  187. package/src/utils/dedupeCSSRules.mjs +0 -19
  188. package/src/utils/dedupeCSSRules.mjs.map +0 -1
@@ -0,0 +1,425 @@
1
+ import { parseSync, type Node } from 'oxc-parser';
2
+ import { walk, ScopeTracker, type ScopeTrackerImport } from 'oxc-walker';
3
+ import MagicString from 'magic-string';
4
+ import shakerEvaluator from '@griffel/transform-shaker';
5
+
6
+ import type { TransformResolver } from './evaluation/module.mjs';
7
+ import type { Evaluator, EvalRule, TransformPerfIssue } from './evaluation/types.mjs';
8
+ import {
9
+ resolveStyleRulesForSlots,
10
+ resolveResetStyleRules,
11
+ resolveStaticStyleRules,
12
+ normalizeCSSBucketEntry,
13
+ type GriffelStyle,
14
+ type GriffelStaticStyles,
15
+ type CSSRulesByBucket,
16
+ type CSSClassesMapBySlot,
17
+ type GriffelResetStyle,
18
+ } from '@griffel/core';
19
+
20
+ import { batchEvaluator } from './evaluation/batchEvaluator.mjs';
21
+ import { fluentTokensPlugin } from './evaluation/fluentTokensPlugin.mjs';
22
+ import type { AstEvaluatorPlugin } from './evaluation/types.mjs';
23
+ import { dedupeCSSRules } from './utils/dedupeCSSRules.mjs';
24
+ import type { StyleCall } from './types.mjs';
25
+
26
+ export type TransformOptions = {
27
+ filename: string;
28
+
29
+ /** Custom module resolver used to resolve imports inside evaluated modules. */
30
+ resolveModule: TransformResolver;
31
+
32
+ classNameHashSalt?: string;
33
+
34
+ /**
35
+ * Returns the evaluated CSS rules in the file result metadata
36
+ * @default false
37
+ */
38
+ generateMetadata?: boolean;
39
+
40
+ /** Defines set of modules and imports handled by a transformPlugin. */
41
+ importsToTransform?: string[];
42
+
43
+ /** Defines the set of function names that should be treated as Griffel style calls. */
44
+ functionsToTransform?: FunctionKinds[];
45
+
46
+ /** The set of rules that defines how the matched files will be transformed during the evaluation. */
47
+ evaluationRules?: EvalRule[];
48
+
49
+ /** Plugins for extending AST evaluation with custom node handling. */
50
+ astEvaluationPlugins?: AstEvaluatorPlugin[];
51
+
52
+ /**
53
+ * Collects performance issues (CJS modules, barrel re-exports) during evaluation.
54
+ * @default false
55
+ */
56
+ collectPerfIssues?: boolean;
57
+ };
58
+
59
+ export type TransformResult = {
60
+ code: string;
61
+ cssRulesByBucket?: CSSRulesByBucket;
62
+ usedProcessing: boolean;
63
+ usedVMForEvaluation: boolean;
64
+ perfIssues?: TransformPerfIssue[];
65
+ };
66
+
67
+ type FunctionKinds = 'makeStyles' | 'makeResetStyles' | 'makeStaticStyles';
68
+
69
+ const EXPORT_STAR_RE = /export\s+\*\s+from\s/;
70
+
71
+ function wrapWithPerfIssues(evaluator: Evaluator, perfIssues: TransformPerfIssue[]): Evaluator {
72
+ return (filename, text, only) => {
73
+ const result = evaluator(filename, text, only);
74
+
75
+ if (result.moduleKind === 'cjs') {
76
+ perfIssues.push({ type: 'cjs-module', dependencyFilename: filename });
77
+ } else if (EXPORT_STAR_RE.test(result.code)) {
78
+ perfIssues.push({ type: 'barrel-export-star', dependencyFilename: filename });
79
+ }
80
+
81
+ return result;
82
+ };
83
+ }
84
+
85
+ const RUNTIME_IDENTIFIERS = new Map<FunctionKinds, string>([
86
+ ['makeStyles', '__css'],
87
+ ['makeResetStyles', '__resetCSS'],
88
+ ['makeStaticStyles', '__staticCSS'],
89
+ ]);
90
+
91
+ /**
92
+ * Extracts CSS rules from evaluated reset styles to metadata
93
+ */
94
+ function buildCSSResetEntriesMetadata(
95
+ cssResetEntries: Record<string, string[]>,
96
+ cssRules: string[] | CSSRulesByBucket,
97
+ declaratorId: string,
98
+ ) {
99
+ const cssRulesByBucket: CSSRulesByBucket = Array.isArray(cssRules) ? { d: cssRules } : cssRules;
100
+ cssResetEntries[declaratorId] ??= [];
101
+ cssResetEntries[declaratorId] = Object.values(cssRulesByBucket).flatMap(bucketEntries => {
102
+ return bucketEntries.map(bucketEntry => {
103
+ if (Array.isArray(bucketEntry)) {
104
+ throw new Error(
105
+ `CSS rules in buckets for "makeResetStyles()" should not contain arrays, got: ${JSON.stringify(
106
+ bucketEntry,
107
+ )})}`,
108
+ );
109
+ }
110
+
111
+ return bucketEntry;
112
+ });
113
+ });
114
+ }
115
+
116
+ /**
117
+ * Extracts CSS rules from evaluated styles to metadata
118
+ */
119
+ function buildCSSEntriesMetadata(
120
+ cssEntries: Record<string, Record<string, string[]>>,
121
+ classnamesMapping: CSSClassesMapBySlot<string>,
122
+ cssRulesByBucket: CSSRulesByBucket,
123
+ declaratorId: string,
124
+ ) {
125
+ const classesBySlot: Record<string, string[]> = Object.fromEntries(
126
+ Object.entries(classnamesMapping).map(([slot, cssClassesMap]) => {
127
+ const uniqueClasses = new Set<string>();
128
+ Object.values(cssClassesMap).forEach(cssClasses => {
129
+ if (typeof cssClasses === 'string') {
130
+ uniqueClasses.add(cssClasses);
131
+ } else if (Array.isArray(cssClasses)) {
132
+ cssClasses.forEach(cssClass => uniqueClasses.add(cssClass));
133
+ }
134
+ });
135
+ return [slot, Array.from(uniqueClasses)];
136
+ }),
137
+ );
138
+
139
+ const cssRules: string[] = Object.values(cssRulesByBucket).flatMap(cssRulesByBucket => {
140
+ return cssRulesByBucket.map(bucketEntry => {
141
+ const [cssRule] = normalizeCSSBucketEntry(bucketEntry);
142
+ return cssRule;
143
+ });
144
+ });
145
+
146
+ cssEntries[declaratorId] = Object.fromEntries(
147
+ Object.entries(classesBySlot).map(([slot, cssClasses]) => {
148
+ return [
149
+ slot,
150
+ cssClasses.map(cssClass => {
151
+ return cssRules.find(rule => rule.includes(cssClass))!;
152
+ }),
153
+ ];
154
+ }),
155
+ );
156
+ }
157
+
158
+ function concatCSSRulesByBucket(bucketA: CSSRulesByBucket = {}, bucketB: CSSRulesByBucket) {
159
+ // eslint-disable-next-line guard-for-in
160
+ for (const cssBucketName in bucketB) {
161
+ const bucketName = cssBucketName as keyof CSSRulesByBucket;
162
+ const bucketBEntries = bucketB[bucketName] ?? [];
163
+
164
+ if (bucketA[bucketName]) {
165
+ bucketA[bucketName].push(...bucketBEntries);
166
+ } else {
167
+ bucketA[bucketName] = bucketBEntries;
168
+ }
169
+ }
170
+
171
+ return bucketA;
172
+ }
173
+
174
+ /**
175
+ * Transforms passed source code with oxc-parser and oxc-walker instead of Babel.
176
+ */
177
+ export function transformSync(sourceCode: string, options: TransformOptions): TransformResult {
178
+ const perfIssues = options.collectPerfIssues ? ([] as TransformPerfIssue[]) : undefined;
179
+
180
+ const {
181
+ filename,
182
+ resolveModule,
183
+ classNameHashSalt = '',
184
+ generateMetadata = false,
185
+ importsToTransform = ['@griffel/core', '@griffel/react', '@fluentui/react-components'],
186
+ functionsToTransform = ['makeStyles', 'makeResetStyles', 'makeStaticStyles'],
187
+ evaluationRules = [{ action: perfIssues ? wrapWithPerfIssues(shakerEvaluator, perfIssues) : shakerEvaluator }],
188
+ astEvaluationPlugins = [fluentTokensPlugin],
189
+ } = options;
190
+
191
+ const importsToTransformSet = new Set(importsToTransform);
192
+ const functionsToTransformSet = new Set<FunctionKinds>(functionsToTransform);
193
+
194
+ if (!filename) {
195
+ throw new Error('Transform error: "filename" option is required');
196
+ }
197
+
198
+ const parseResult = parseSync(filename, sourceCode);
199
+
200
+ if (parseResult.errors.length > 0) {
201
+ throw new Error(`Failed to parse "${filename}": ${parseResult.errors.map(e => e.message).join(', ')}`);
202
+ }
203
+
204
+ if (parseResult.program.body.length > 0 && !parseResult.module.hasModuleSyntax) {
205
+ throw new Error(
206
+ `Transform error: "${filename}" is not an ES module. ` +
207
+ `@griffel/transform only supports ES modules (files using import/export syntax).`,
208
+ );
209
+ }
210
+
211
+ const magicString = new MagicString(sourceCode);
212
+ const programAst = parseResult.program;
213
+
214
+ // Quick bail-out: if no Griffel imports exist, skip the AST walk entirely
215
+ const hasGriffelImports = parseResult.module.staticImports.some(
216
+ si =>
217
+ importsToTransformSet.has(si.moduleRequest.value) &&
218
+ si.entries.some(e => e.importName.kind === 'Name' && functionsToTransformSet.has(e.importName.name as FunctionKinds)),
219
+ );
220
+
221
+ if (!hasGriffelImports) {
222
+ return { code: sourceCode, usedProcessing: false, usedVMForEvaluation: false };
223
+ }
224
+
225
+ const styleCalls: StyleCall[] = [];
226
+ const cssEntries: Record<string, Record<string, string[]>> = {};
227
+ const cssResetEntries: Record<string, string[]> = {};
228
+
229
+ let cssRulesByBucket: CSSRulesByBucket = {};
230
+
231
+ // -----
232
+ // Walk AST to collect style function calls using ScopeTracker for scope-aware import resolution
233
+
234
+ const scopeTracker = new ScopeTracker();
235
+ const matchedSpecifiers = new Map<number, { start: number; end: number; functionKind: FunctionKinds }>();
236
+
237
+ walk(programAst, {
238
+ scopeTracker,
239
+ enter(node, parent) {
240
+ if (node.type === 'CallExpression' && node.callee.type === 'Identifier') {
241
+ const declaration = scopeTracker.getDeclaration(node.callee.name) as ScopeTrackerImport | null;
242
+
243
+ if (declaration?.type !== 'Import' || declaration.node.type !== 'ImportSpecifier') {
244
+ return;
245
+ }
246
+
247
+ const importSource = declaration.importNode.source.value;
248
+
249
+ if (!importsToTransformSet.has(importSource)) {
250
+ return;
251
+ }
252
+
253
+ const imported = declaration.node.imported;
254
+
255
+ if (imported.type !== 'Identifier') {
256
+ return;
257
+ }
258
+
259
+ const importedName = imported.name;
260
+
261
+ if (!functionsToTransformSet.has(importedName as FunctionKinds)) {
262
+ return;
263
+ }
264
+
265
+ const functionKind = importedName as FunctionKinds;
266
+
267
+ if (node.arguments.length !== 1) {
268
+ throw new Error(
269
+ `${functionKind}() function accepts only a single param, got ${node.arguments.length} in ${filename}`,
270
+ );
271
+ }
272
+
273
+ // Track the import specifier for rewriting (deduped by node start position)
274
+ matchedSpecifiers.set(declaration.node.start, {
275
+ start: declaration.node.start,
276
+ end: declaration.node.end,
277
+ functionKind,
278
+ });
279
+
280
+ // Find the variable declarator to get the hook name
281
+ let declaratorId = 'unknownHook';
282
+ let current: Node | null = parent;
283
+
284
+ while (current) {
285
+ if (!current) {
286
+ break;
287
+ }
288
+
289
+ if (current.type === 'VariableDeclarator' && current.id.type === 'Identifier') {
290
+ declaratorId = current.id.name;
291
+ break;
292
+ }
293
+
294
+ if ('parent' in current) {
295
+ current = current.parent as Node | null;
296
+ continue;
297
+ }
298
+
299
+ break;
300
+ }
301
+
302
+ const argument = node.arguments[0];
303
+
304
+ styleCalls.push({
305
+ declaratorId,
306
+ functionKind,
307
+
308
+ argumentStart: argument.start,
309
+ argumentEnd: argument.end,
310
+ argumentCode: sourceCode.slice(argument.start, argument.end),
311
+ argumentNode: argument,
312
+
313
+ callStart: node.start,
314
+ callEnd: node.end,
315
+
316
+ importId: node.callee.name,
317
+ });
318
+ }
319
+ },
320
+ });
321
+
322
+ // If no style calls found, return original code
323
+ if (styleCalls.length === 0) {
324
+ return {
325
+ code: sourceCode,
326
+ usedProcessing: false,
327
+ usedVMForEvaluation: false,
328
+ };
329
+ }
330
+
331
+ // Process style calls - evaluate and transform
332
+ const { evaluationResults, usedVMForEvaluation } = batchEvaluator(
333
+ sourceCode,
334
+ filename,
335
+ styleCalls,
336
+ evaluationRules,
337
+ resolveModule,
338
+ programAst,
339
+ astEvaluationPlugins,
340
+ );
341
+
342
+ for (let i = styleCalls.length - 1; i >= 0; i--) {
343
+ const styleCall = styleCalls[i];
344
+ const evaluationResult = evaluationResults[i];
345
+
346
+ switch (styleCall.functionKind) {
347
+ case 'makeStyles':
348
+ {
349
+ const stylesBySlots = evaluationResult as Record<string, GriffelStyle>;
350
+ const [classnamesMapping, resolvedCSSRules] = resolveStyleRulesForSlots(stylesBySlots, classNameHashSalt);
351
+ const uniqueCSSRules = dedupeCSSRules(resolvedCSSRules);
352
+
353
+ if (generateMetadata) {
354
+ buildCSSEntriesMetadata(cssEntries, classnamesMapping, uniqueCSSRules, styleCall.declaratorId);
355
+ }
356
+
357
+ // Replace the function call arguments
358
+ magicString.overwrite(styleCall.argumentStart, styleCall.argumentEnd, `${JSON.stringify(classnamesMapping)}`);
359
+ cssRulesByBucket = concatCSSRulesByBucket(cssRulesByBucket, uniqueCSSRules);
360
+ }
361
+
362
+ break;
363
+
364
+ case 'makeResetStyles':
365
+ {
366
+ const styles = evaluationResult as GriffelResetStyle;
367
+ const [ltrClassName, rtlClassName, cssRules] = resolveResetStyleRules(styles, classNameHashSalt);
368
+
369
+ if (generateMetadata) {
370
+ buildCSSResetEntriesMetadata(cssResetEntries, cssRules, styleCall.declaratorId);
371
+ }
372
+
373
+ // Replace the function call arguments
374
+ magicString.overwrite(
375
+ styleCall.argumentStart,
376
+ styleCall.argumentEnd,
377
+ `${JSON.stringify(ltrClassName)}, ${JSON.stringify(rtlClassName)}`,
378
+ );
379
+ cssRulesByBucket = concatCSSRulesByBucket(
380
+ cssRulesByBucket,
381
+ Array.isArray(cssRules) ? { r: cssRules } : cssRules,
382
+ );
383
+ }
384
+ break;
385
+
386
+ case 'makeStaticStyles':
387
+ {
388
+ const styles = evaluationResult as GriffelStaticStyles | GriffelStaticStyles[];
389
+ const stylesSet: GriffelStaticStyles[] = Array.isArray(styles) ? styles : [styles];
390
+ const cssRules = resolveStaticStyleRules(stylesSet);
391
+
392
+ // Replace the function call arguments with the resolved CSS rules bucket
393
+ magicString.overwrite(styleCall.argumentStart, styleCall.argumentEnd, JSON.stringify({ d: cssRules }));
394
+ cssRulesByBucket = concatCSSRulesByBucket(cssRulesByBucket, { d: cssRules });
395
+ }
396
+ break;
397
+ }
398
+ }
399
+
400
+ // ---
401
+ // Transform imports and function names
402
+
403
+ for (const specifier of matchedSpecifiers.values()) {
404
+ magicString.overwrite(specifier.start, specifier.end, RUNTIME_IDENTIFIERS.get(specifier.functionKind)!);
405
+ }
406
+
407
+ // ---
408
+ // Transform function call names
409
+
410
+ for (const styleCall of styleCalls) {
411
+ magicString.overwrite(
412
+ styleCall.callStart,
413
+ styleCall.callStart + styleCall.importId.length,
414
+ RUNTIME_IDENTIFIERS.get(styleCall.functionKind)!,
415
+ );
416
+ }
417
+
418
+ return {
419
+ code: magicString.toString(),
420
+ cssRulesByBucket,
421
+ usedProcessing: true,
422
+ usedVMForEvaluation,
423
+ perfIssues,
424
+ };
425
+ }