Repository: microsoft/griffel
Pull request #441 (2023-09-18 15:32:39)

fix: metadata entries should be defined by default
If the user configures the preset to generate metadata, the objects should be defined by default to align with typings.

This fixes the edge case where the preset runs on a file without Griffel - in a backwards compatible way.

github-actions[bot]: ## 📊 Bundle size report

<sub>🤖 This report was generated against <a href='https://github.com/microsoft/griffel/commit/4d2e93ea559d80483e02a397b4c6785e8c713ea7'>4d2e93ea559d80483e02a397b4c6785e8c713ea7</a></sub>

<!-- Sticky Pull Request Comment -->

Patch:
diff --git a/change/@griffel-babel-preset-96f2826d-0876-42d6-8ef0-f3dbd6c45004.json b/change/@griffel-babel-preset-96f2826d-0876-42d6-8ef0-f3dbd6c45004.json
new file mode 100644
index 0000000..74ee231
--- /dev/null
+++ b/change/@griffel-babel-preset-96f2826d-0876-42d6-8ef0-f3dbd6c45004.json
@@ -0,0 +1,7 @@
+{
+  "type": "patch",
+  "comment": "fix: metadata entries should be defined by default",
+  "packageName": "@griffel/babel-preset",
+  "email": "lingfan.gao@microsoft.com",
+  "dependentChangeType": "patch"
+}
diff --git a/packages/babel-preset/src/transformPlugin.ts b/packages/babel-preset/src/transformPlugin.ts
index dd4cbe9..c0dde9f 100644
--- a/packages/babel-preset/src/transformPlugin.ts
+++ b/packages/babel-preset/src/transformPlugin.ts
@@ -228,6 +228,13 @@ export const transformPlugin = declare<Partial<BabelPluginOptions>, PluginObj<Ba
       this.calleePaths = [];
       this.cssEntries = {};
       this.cssResetEntries = {};
+
+      if (pluginOptions.generateMetadata) {
+        Object.assign(this.file.metadata, {
+          cssResetEntries: {},
+          cssEntries: {},
+        } as BabelPluginMetadata);
+      }
     },
 
     visitor: {


Test patch:
diff --git a/packages/babel-preset/src/transformPlugin.test.ts b/packages/babel-preset/src/transformPlugin.test.ts
index 80e2625..49ee88c 100644
--- a/packages/babel-preset/src/transformPlugin.test.ts
+++ b/packages/babel-preset/src/transformPlugin.test.ts
@@ -4,6 +4,7 @@ import * as fs from 'fs';
 import * as path from 'path';
 
 import { transformPlugin } from './transformPlugin';
+import { BabelPluginMetadata } from './types';
 
 const prettierConfig = JSON.parse(
   fs.readFileSync(path.resolve(__dirname, '../../../.prettierrc'), { encoding: 'utf-8' }),
@@ -286,6 +287,20 @@ describe('babel preset', () => {
     expect(babelFileResult?.metadata).toMatchInlineSnapshot(`Object {}`);
   });
 
+  it('should return empty metadata when file contains no griffel code', () => {
+    const code = 'export {}';
+    const babelFileResult = Babel.transformSync(code, {
+      babelrc: false,
+      configFile: false,
+      plugins: [[transformPlugin, { generateMetadata: true }]],
+      filename: 'test.js',
+      presets: ['@babel/typescript'],
+    });
+
+    expect((babelFileResult?.metadata as BabelPluginMetadata | undefined)?.cssEntries).toEqual({});
+    expect((babelFileResult?.metadata as BabelPluginMetadata | undefined)?.cssResetEntries).toEqual({});
+  });
+
   it('should generate metadata for makeStyles when configured', () => {
     const fixture = path.resolve(fixturesDir, 'object', 'code.ts');
     const code = fs.readFileSync(fixture).toString();