Skip to content

Conversation

@layershifter
Copy link
Member

@layershifter layershifter commented Oct 3, 2022

Fixes #227.

This PR adds new tests and modifies @griffel/babel-preset or @griffel/webpack-loader to handle makeResetStyles transforms.

resetImportName added to modules options of Babel preset and will be documented in a separate PR.

@github-actions
Copy link

github-actions bot commented Oct 3, 2022

📊 Bundle size report

🤖 This report was generated against 48239dc8719e4ac723d517291e5d9a1f866e42c6

@layershifter layershifter force-pushed the feat/make-reset-styles branch 2 times, most recently from 5fa5ade to d02e321 Compare October 4, 2022 17:55
@layershifter layershifter force-pushed the feat/make-reset-styles branch 4 times, most recently from a132e3a to 791a7e1 Compare October 5, 2022 16:10
"jestConfig": "packages/webpack-loader/jest.config.js",
"passWithNoTests": true
},
"dependsOn": [{ "target": "build", "projects": "dependencies" }]
Copy link
Member Author

Choose a reason for hiding this comment

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

I changed @griffel/react to use externals, so we need to build less code during each test and we don't need to build packages before

Comment on lines +178 to +181
it('makeStyles', () => {
expect(shouldTransformSourceCode(`import { makeStyles } from "@griffel/react"`, undefined)).toBe(true);
expect(shouldTransformSourceCode(`import { Button } from "@fluentui/react"`, undefined)).toBe(false);
});
Copy link
Member Author

Choose a reason for hiding this comment

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

The same test, just moved into describe block

Comment on lines +190 to +207
it('makeStyles', () => {
expect(
shouldTransformSourceCode(`import { makeStyles } from "@griffel/react"`, [
{ moduleSource: '@griffel/react', importName: 'makeStyles' },
]),
).toBe(true);
expect(
shouldTransformSourceCode(`import { createStyles } from "make-styles"`, [
{ moduleSource: 'make-styles', importName: 'createStyles' },
]),
).toBe(true);

expect(
shouldTransformSourceCode(`import { Button } from "@fluentui/react"`, [
{ moduleSource: '@griffel/react', importName: 'makeStyles' },
]),
).toBe(false);
});
Copy link
Member Author

Choose a reason for hiding this comment

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

The same test, just moved into describe block

Comment on lines 21 to 24
enum FunctionKinds {
makeStyles = 'makeStyles',
makeResetStyles = 'makeResetStyles',
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Leaves the space for transforms of makeStaticStyles

@layershifter layershifter force-pushed the feat/make-reset-styles branch 2 times, most recently from ba2d10a to 5aa1472 Compare October 5, 2022 16:25
@layershifter layershifter force-pushed the feat/make-reset-styles branch from 5aa1472 to 755cccf Compare October 5, 2022 18:40
@layershifter layershifter marked this pull request as ready for review October 5, 2022 18:49
@layershifter layershifter requested a review from a team as a code owner October 5, 2022 18:49
throw definitionsPath.buildCodeFrameError('makeStyles() function accepts only an object as a param');
if (functionKind === 'makeStyles') {
if (!definitionsPath.isObjectExpression()) {
throw definitionsPath.buildCodeFrameError('makeStyles() function accepts only an object as a param');
Copy link
Contributor

Choose a reason for hiding this comment

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

both makeStyles and makeResetStyles accept an object only as param right ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct, will check it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Restored to the previous condition, don't remember why it looks like that

Comment on lines +8 to +9
importName: string;
resetImportName?: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
importName: string;
resetImportName?: string;
makeStylesimportName: string;
makeResetStylesImportName?: string;

it's a bit longer, but it might make things clearer in the plugin code

Copy link
Member Author

Choose a reason for hiding this comment

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

It's the part of public API so a change there will be BC, that's why it's like that 🥲

// Fallback to "makeStyles" if options were not provided
const imports = modules ? modules.map(module => module.importName).join('|') : 'makeStyles';
const imports = modules
? modules.flatMap(module => [module.importName, module.resetImportName || 'makeResetStyles']).join('|')
Copy link
Contributor

Choose a reason for hiding this comment

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

why does module.resetImportName need to default to makeResetStyles when importName doesn't need to default to makeStyles ?

Copy link
Member Author

Choose a reason for hiding this comment

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

  • importName is a required param
  • resetImportName is optional to avoid BC 😕

Copy link
Contributor

@ling1726 ling1726 left a comment

Choose a reason for hiding this comment

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

Changes look more or less OK to me, it's simply just extending the plugin state to differentiate between the two functions and then handling the replacement appropriately.

Evaluation hasn't changed at all from what I can see

@layershifter
Copy link
Member Author

Evaluation hasn't changed at all from what I can see

Yup, that's correct.

@layershifter layershifter force-pushed the feat/make-reset-styles branch from bc0f31f to d04b624 Compare October 6, 2022 09:28
@layershifter layershifter merged commit b360b56 into microsoft:main Oct 6, 2022
@layershifter layershifter deleted the feat/make-reset-styles branch October 6, 2022 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

babel-preset/webpack-loader: implement makeResetStyles support

2 participants