@griffel/transform 2.0.1 → 3.0.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/CHANGELOG.md CHANGED
@@ -1,16 +1,32 @@
1
1
  # Change Log - @griffel/transform
2
2
 
3
- This log was last generated on Thu, 12 Mar 2026 17:19:24 GMT and should not be manually modified.
3
+ This log was last generated on Mon, 23 Mar 2026 16:49:14 GMT and should not be manually modified.
4
4
 
5
5
  <!-- Start content -->
6
6
 
7
- ## 2.0.1
7
+ ## 3.0.0
8
8
 
9
- Thu, 12 Mar 2026 17:19:24 GMT
9
+ Mon, 23 Mar 2026 16:49:14 GMT
10
+
11
+ ### Major changes
12
+
13
+ - BREAKING: rename modules to importsToTransform, add functionsToTransform option (olfedias@microsoft.com)
14
+
15
+ ### Patches
16
+
17
+ - fix: reverse styleCalls iteration to match CSS rule ordering of old plugin (olfedias@microsoft.com)
18
+ - fix: add zIndex fallback values in fluentTokensPlugin (olfedias@microsoft.com)
19
+
20
+ ## 2.0.2
21
+
22
+ Fri, 13 Mar 2026 17:00:44 GMT
10
23
 
11
24
  ### Patches
12
25
 
13
- - Bump @griffel/transform-shaker to v1.0.1
26
+ - fix: wrap VM errors as host Error with filename context (olfedias@microsoft.com)
27
+ - perf: skip eval cache for __mkPreval entry-point evaluations (olfedias@microsoft.com)
28
+ - fix: wrap VM errors as host Error with filename context, defer CJS export assignments for IIFE patterns (olfedias@microsoft.com)
29
+ - Bump @griffel/transform-shaker to v1.0.2
14
30
 
15
31
  ## 2.0.0
16
32
 
@@ -25,7 +25,7 @@ export declare class Module {
25
25
  cache: Record<string, Module>;
26
26
  resolve: (id: string) => string;
27
27
  };
28
- evaluate(text: string, only?: string[] | null): void;
28
+ evaluate(text: string, only?: string[] | null, useEvalCache?: boolean): void;
29
29
  static invalidate: () => void;
30
30
  static invalidateEvalCache: () => void;
31
31
  static _nodeModulePaths: (filename: string) => string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@griffel/transform",
3
- "version": "2.0.1",
3
+ "version": "3.0.0",
4
4
  "description": "A package that performs build time transforms for Griffel",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "@griffel/core": "^1.20.1",
20
- "@griffel/transform-shaker": "^1.0.1",
20
+ "@griffel/transform-shaker": "^1.0.2",
21
21
  "debug": "^4.3.0",
22
22
  "magic-string": "^0.30.19",
23
23
  "oxc-parser": "^0.116.0",
package/transform.js CHANGED
@@ -51,6 +51,7 @@ function convertESMtoCJS(code, filename) {
51
51
  return code;
52
52
  }
53
53
  const ms = new MagicString(code);
54
+ const deferredExports = [];
54
55
  for (const node of program.body) {
55
56
  switch (node.type) {
56
57
  case "ImportDeclaration": {
@@ -105,14 +106,11 @@ function convertESMtoCJS(code, filename) {
105
106
  const names = prop(decl, "declarations").flatMap(
106
107
  (d) => extractDeclaredNames(prop(d, "id"))
107
108
  );
108
- const exportsCode = names.map((name) => `exports.${name} = ${name};`).join(" ");
109
- ms.appendLeft(node.end, "\n" + exportsCode);
109
+ deferredExports.push(...names);
110
110
  } else if (decl.type === "FunctionDeclaration" || decl.type === "ClassDeclaration") {
111
111
  const id = prop(decl, "id");
112
112
  if (id) {
113
- const name = prop(id, "name");
114
- ms.appendLeft(node.end, `
115
- exports.${name} = ${name};`);
113
+ deferredExports.push(prop(id, "name"));
116
114
  }
117
115
  }
118
116
  } else if (prop(node, "source")) {
@@ -173,6 +171,9 @@ exports.default = ${prop(id, "name")};`);
173
171
  }
174
172
  }
175
173
  }
174
+ if (deferredExports.length > 0) {
175
+ ms.append("\n" + deferredExports.map((name) => `exports.${name} = ${name};`).join("\n"));
176
+ }
176
177
  ms.prepend('Object.defineProperty(exports, "__esModule", { value: true });\n');
177
178
  return ms.toString();
178
179
  }
@@ -293,6 +294,9 @@ const debug = createDebug("griffel:module");
293
294
  let cache = {};
294
295
  const NOOP = () => {
295
296
  };
297
+ function isError$1(e) {
298
+ return e != null && typeof e === "object" && "message" in e && "stack" in e;
299
+ }
296
300
  const createCustomDebug = (depth) => (namespaces, arg1, ...args) => {
297
301
  const modulePrefix = depth === 0 ? "module" : `sub-module-${depth}`;
298
302
  debug(`${modulePrefix}:${namespaces}`, arg1, ...args);
@@ -383,7 +387,7 @@ class Module {
383
387
  resolve: (id) => this.resolveFilename(id, this).path
384
388
  }
385
389
  );
386
- evaluate(text, only = null) {
390
+ evaluate(text, only = null, useEvalCache = true) {
387
391
  const { filename } = this;
388
392
  let action = "ignore";
389
393
  for (let i = this.rules.length - 1; i >= 0; i--) {
@@ -394,7 +398,7 @@ class Module {
394
398
  }
395
399
  }
396
400
  const cacheKey = [this.filename, ...only ?? []];
397
- if (has(cacheKey, text)) {
401
+ if (useEvalCache && has(cacheKey, text)) {
398
402
  this.exports = get(cacheKey, text);
399
403
  return;
400
404
  }
@@ -417,25 +421,36 @@ ${code}`);
417
421
  })(exports);`, {
418
422
  filename: this.filename
419
423
  });
420
- script.runInContext(
421
- vm.createContext({
422
- clearImmediate: NOOP,
423
- clearInterval: NOOP,
424
- clearTimeout: NOOP,
425
- setImmediate: NOOP,
426
- setInterval: NOOP,
427
- setTimeout: NOOP,
428
- fetch: NOOP,
429
- global,
430
- process: mockProcess,
431
- module: this,
432
- exports: this.exports,
433
- require: this.require,
434
- __filename: this.filename,
435
- __dirname: path.dirname(this.filename)
436
- })
437
- );
438
- set(cacheKey, text, this.exports);
424
+ try {
425
+ script.runInContext(
426
+ vm.createContext({
427
+ clearImmediate: NOOP,
428
+ clearInterval: NOOP,
429
+ clearTimeout: NOOP,
430
+ setImmediate: NOOP,
431
+ setInterval: NOOP,
432
+ setTimeout: NOOP,
433
+ fetch: NOOP,
434
+ global,
435
+ process: mockProcess,
436
+ module: this,
437
+ exports: this.exports,
438
+ require: this.require,
439
+ __filename: this.filename,
440
+ __dirname: path.dirname(this.filename)
441
+ })
442
+ );
443
+ } catch (vmError) {
444
+ const message = isError$1(vmError) ? vmError.message : String(vmError);
445
+ const hostError = new Error(message);
446
+ if (isError$1(vmError)) {
447
+ hostError.stack = vmError.stack;
448
+ }
449
+ throw hostError;
450
+ }
451
+ if (useEvalCache) {
452
+ set(cacheKey, text, this.exports);
453
+ }
439
454
  }
440
455
  static invalidate = () => {
441
456
  cache = {};
@@ -529,7 +544,12 @@ export const __mkPreval = (() => {
529
544
  `;
530
545
  try {
531
546
  const mod = new Module(filename, evaluationRules, resolveFilename);
532
- mod.evaluate(codeForEvaluation, ["__mkPreval"]);
547
+ mod.evaluate(
548
+ codeForEvaluation,
549
+ ["__mkPreval"],
550
+ /* useEvalCache */
551
+ false
552
+ );
533
553
  const result = mod.exports.__mkPreval;
534
554
  if (isError(result)) {
535
555
  return { confident: false, error: result };
@@ -579,6 +599,16 @@ function batchEvaluator(sourceCode, filename, styleCalls, evaluationRules, resol
579
599
  evaluationResults
580
600
  };
581
601
  }
602
+ const Z_INDEX_DEFAULTS = {
603
+ zIndexBackground: 0,
604
+ zIndexContent: 1,
605
+ zIndexOverlay: 1e3,
606
+ zIndexPopup: 2e3,
607
+ zIndexMessages: 3e3,
608
+ zIndexFloating: 4e3,
609
+ zIndexPriority: 5e3,
610
+ zIndexDebug: 6e3
611
+ };
582
612
  function evaluateTemplateLiteralWithTokens(node) {
583
613
  let result = "";
584
614
  for (let i = 0; i < node.quasis.length; i++) {
@@ -586,7 +616,9 @@ function evaluateTemplateLiteralWithTokens(node) {
586
616
  if (i < node.expressions.length) {
587
617
  const expression = node.expressions[i];
588
618
  if (expression.type === "MemberExpression" && expression.object.type === "Identifier" && expression.object.name === "tokens" && expression.property.type === "Identifier" && !expression.computed) {
589
- result += `var(--${expression.property.name})`;
619
+ const name = expression.property.name;
620
+ const fallback = Z_INDEX_DEFAULTS[name];
621
+ result += fallback !== void 0 ? `var(--${name}, ${fallback})` : `var(--${name})`;
590
622
  } else {
591
623
  return DEOPT;
592
624
  }
@@ -596,7 +628,9 @@ function evaluateTemplateLiteralWithTokens(node) {
596
628
  }
597
629
  function evaluateTokensMemberExpression(node) {
598
630
  if (node.object.type === "Identifier" && node.object.name === "tokens" && node.property.type === "Identifier" && !node.computed) {
599
- return `var(--${node.property.name})`;
631
+ const name = node.property.name;
632
+ const fallback = Z_INDEX_DEFAULTS[name];
633
+ return fallback !== void 0 ? `var(--${name}, ${fallback})` : `var(--${name})`;
600
634
  } else {
601
635
  return DEOPT;
602
636
  }
@@ -713,10 +747,13 @@ function transformSync(sourceCode, options) {
713
747
  resolveModule,
714
748
  classNameHashSalt = "",
715
749
  generateMetadata = false,
716
- modules = ["@griffel/core", "@griffel/react", "@fluentui/react-components"],
750
+ importsToTransform = ["@griffel/core", "@griffel/react", "@fluentui/react-components"],
751
+ functionsToTransform = ["makeStyles", "makeResetStyles", "makeStaticStyles"],
717
752
  evaluationRules = [{ action: perfIssues ? wrapWithPerfIssues(shakerEvaluator, perfIssues) : shakerEvaluator }],
718
753
  astEvaluationPlugins = [fluentTokensPlugin]
719
754
  } = options;
755
+ const importsToTransformSet = new Set(importsToTransform);
756
+ const functionsToTransformSet = new Set(functionsToTransform);
720
757
  if (!filename) {
721
758
  throw new Error('Transform error: "filename" option is required');
722
759
  }
@@ -732,7 +769,7 @@ function transformSync(sourceCode, options) {
732
769
  const magicString = new MagicString(sourceCode);
733
770
  const programAst = parseResult.program;
734
771
  const hasGriffelImports = parseResult.module.staticImports.some(
735
- (si) => modules.includes(si.moduleRequest.value) && si.entries.some((e) => e.importName.kind === "Name" && RUNTIME_IDENTIFIERS.has(e.importName.name))
772
+ (si) => importsToTransformSet.has(si.moduleRequest.value) && si.entries.some((e) => e.importName.kind === "Name" && functionsToTransformSet.has(e.importName.name))
736
773
  );
737
774
  if (!hasGriffelImports) {
738
775
  return { code: sourceCode, usedProcessing: false, usedVMForEvaluation: false };
@@ -752,7 +789,7 @@ function transformSync(sourceCode, options) {
752
789
  return;
753
790
  }
754
791
  const importSource = declaration.importNode.source.value;
755
- if (!modules.includes(importSource)) {
792
+ if (!importsToTransformSet.has(importSource)) {
756
793
  return;
757
794
  }
758
795
  const imported = declaration.node.imported;
@@ -760,12 +797,14 @@ function transformSync(sourceCode, options) {
760
797
  return;
761
798
  }
762
799
  const importedName = imported.name;
763
- if (!RUNTIME_IDENTIFIERS.has(importedName)) {
800
+ if (!functionsToTransformSet.has(importedName)) {
764
801
  return;
765
802
  }
766
803
  const functionKind = importedName;
767
804
  if (node.arguments.length !== 1) {
768
- throw new Error(`${functionKind}() function accepts only a single param`);
805
+ throw new Error(
806
+ `${functionKind}() function accepts only a single param, got ${node.arguments.length} in ${filename}`
807
+ );
769
808
  }
770
809
  matchedSpecifiers.set(declaration.node.start, {
771
810
  start: declaration.node.start,
@@ -819,7 +858,7 @@ function transformSync(sourceCode, options) {
819
858
  programAst,
820
859
  astEvaluationPlugins
821
860
  );
822
- for (let i = 0; i < styleCalls.length; i++) {
861
+ for (let i = styleCalls.length - 1; i >= 0; i--) {
823
862
  const styleCall = styleCalls[i];
824
863
  const evaluationResult = evaluationResults[i];
825
864
  switch (styleCall.functionKind) {
@@ -12,7 +12,9 @@ export type TransformOptions = {
12
12
  */
13
13
  generateMetadata?: boolean;
14
14
  /** Defines set of modules and imports handled by a transformPlugin. */
15
- modules?: string[];
15
+ importsToTransform?: string[];
16
+ /** Defines the set of function names that should be treated as Griffel style calls. */
17
+ functionsToTransform?: FunctionKinds[];
16
18
  /** The set of rules that defines how the matched files will be transformed during the evaluation. */
17
19
  evaluationRules?: EvalRule[];
18
20
  /** Plugins for extending AST evaluation with custom node handling. */
@@ -30,7 +32,9 @@ export type TransformResult = {
30
32
  usedVMForEvaluation: boolean;
31
33
  perfIssues?: TransformPerfIssue[];
32
34
  };
35
+ type FunctionKinds = 'makeStyles' | 'makeResetStyles' | 'makeStaticStyles';
33
36
  /**
34
37
  * Transforms passed source code with oxc-parser and oxc-walker instead of Babel.
35
38
  */
36
39
  export declare function transformSync(sourceCode: string, options: TransformOptions): TransformResult;
40
+ export {};