Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid empty CSS imports #289

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: avoid empty CSS imports",
"packageName": "@griffel/webpack-extraction-plugin",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = '__styles';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = '__styles';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it make more sense to have better regex ? like __styles()? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but this loader targets node_modules and needs to process thousands files in real projects, so if will put a regex in the hot path I believe that it will be a bottleneck.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps some kind of sliding window algorithm might be able to do the same thing while still being faster than Regex, especially you know how the call starts and terminates 🤔

but I guess handling empty buckets is a better gate

Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -15,7 +15,7 @@ type StripRuntimeBabelPluginState = PluginPass & {
};

export type StripRuntimeBabelPluginMetadata = {
cssRulesByBucket: CSSRulesByBucket;
cssRulesByBucket?: CSSRulesByBucket;
};

export function transformUrl(filename: string, resourceDirectory: string, assetPath: string) {
Expand All @@ -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<StripRuntimeBabelPluginOptions>,
PluginObj<StripRuntimeBabelPluginState>
Expand All @@ -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: {
Expand Down Expand Up @@ -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<CSSRulesByBucket['r']>;

state.cssRulesByBucket!.r = cssRules.concat(state.cssRulesByBucket!.r || []);
state.cssRulesByBucket = concatCSSRulesByBucket(state.cssRulesByBucket, { r: cssRules });
}

argumentPath.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});