diff --git a/change/@griffel-webpack-extraction-plugin-46602e0c-3b1b-4553-a919-32df4dda9fd9.json b/change/@griffel-webpack-extraction-plugin-46602e0c-3b1b-4553-a919-32df4dda9fd9.json new file mode 100644 index 000000000..9756a23b4 --- /dev/null +++ b/change/@griffel-webpack-extraction-plugin-46602e0c-3b1b-4553-a919-32df4dda9fd9.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "fix: avoid empty CSS imports", + "packageName": "@griffel/webpack-extraction-plugin", + "email": "olfedias@microsoft.com", + "dependentChangeType": "patch" +} diff --git a/packages/webpack-extraction-plugin/__fixtures__/webpack/missing-calls/code.ts b/packages/webpack-extraction-plugin/__fixtures__/webpack/missing-calls/code.ts new file mode 100644 index 000000000..bb5adbd5b --- /dev/null +++ b/packages/webpack-extraction-plugin/__fixtures__/webpack/missing-calls/code.ts @@ -0,0 +1 @@ +export const foo = '__styles'; diff --git a/packages/webpack-extraction-plugin/__fixtures__/webpack/missing-calls/output.ts b/packages/webpack-extraction-plugin/__fixtures__/webpack/missing-calls/output.ts new file mode 100644 index 000000000..bb5adbd5b --- /dev/null +++ b/packages/webpack-extraction-plugin/__fixtures__/webpack/missing-calls/output.ts @@ -0,0 +1 @@ +export const foo = '__styles'; diff --git a/packages/webpack-extraction-plugin/src/babelPluginStripGriffelRuntime.ts b/packages/webpack-extraction-plugin/src/babelPluginStripGriffelRuntime.ts index a2a824081..f25014e43 100644 --- a/packages/webpack-extraction-plugin/src/babelPluginStripGriffelRuntime.ts +++ b/packages/webpack-extraction-plugin/src/babelPluginStripGriffelRuntime.ts @@ -1,6 +1,6 @@ import { NodePath, PluginObj, PluginPass, types as t } from '@babel/core'; import { declare } from '@babel/helper-plugin-utils'; -import { CSSRulesByBucket } from '@griffel/core'; +import type { CSSRulesByBucket } from '@griffel/core'; import * as path from 'path'; type StripRuntimeBabelPluginOptions = { @@ -15,7 +15,7 @@ type StripRuntimeBabelPluginState = PluginPass & { }; export type StripRuntimeBabelPluginMetadata = { - cssRulesByBucket: CSSRulesByBucket; + cssRulesByBucket?: CSSRulesByBucket; }; export function transformUrl(filename: string, resourceDirectory: string, assetPath: string) { @@ -30,6 +30,16 @@ export function transformUrl(filename: string, resourceDirectory: string, assetP return relativeAssetPath.split(path.sep).join(path.posix.sep); } +function concatCSSRulesByBucket(bucketA: CSSRulesByBucket = {}, bucketB: CSSRulesByBucket) { + Object.entries(bucketB).forEach(([cssBucketName, cssBucketEntries]) => { + bucketA[cssBucketName as keyof CSSRulesByBucket] = cssBucketEntries.concat( + bucketA[cssBucketName as keyof CSSRulesByBucket] || [], + ); + }); + + return bucketA; +} + export const babelPluginStripGriffelRuntime = declare< Partial, PluginObj @@ -38,11 +48,8 @@ export const babelPluginStripGriffelRuntime = declare< return { name: '@griffel/webpack-extraction-plugin/babel', - pre() { - this.cssRulesByBucket = {}; - }, post() { - (this.file.metadata as unknown as StripRuntimeBabelPluginMetadata).cssRulesByBucket = this.cssRulesByBucket!; + (this.file.metadata as unknown as StripRuntimeBabelPluginMetadata).cssRulesByBucket = this.cssRulesByBucket; }, visitor: { @@ -215,15 +222,11 @@ export const babelPluginStripGriffelRuntime = declare< if (functionKind === '__styles') { const cssRulesByBucket = evaluationResult.value as CSSRulesByBucket; - Object.entries(cssRulesByBucket).forEach(([cssBucketName, cssBucketEntries]) => { - state.cssRulesByBucket![cssBucketName as keyof CSSRulesByBucket] = cssBucketEntries.concat( - state.cssRulesByBucket![cssBucketName as keyof CSSRulesByBucket] || [], - ); - }); + state.cssRulesByBucket = concatCSSRulesByBucket(state.cssRulesByBucket, cssRulesByBucket); } else if (functionKind === '__resetStyles') { const cssRules = evaluationResult.value as NonNullable; - state.cssRulesByBucket!.r = cssRules.concat(state.cssRulesByBucket!.r || []); + state.cssRulesByBucket = concatCSSRulesByBucket(state.cssRulesByBucket, { r: cssRules }); } argumentPath.remove(); diff --git a/packages/webpack-extraction-plugin/src/webpackLoader.test.ts b/packages/webpack-extraction-plugin/src/webpackLoader.test.ts index 8744ce795..8201d1ab4 100644 --- a/packages/webpack-extraction-plugin/src/webpackLoader.test.ts +++ b/packages/webpack-extraction-plugin/src/webpackLoader.test.ts @@ -190,4 +190,7 @@ describe('webpackLoader', () => { testFixture('assets'); testFixture('assets-multiple'); testFixture('reset-assets'); + + // Ensures that a file without __styles calls remains unprocessed + testFixture('missing-calls'); });