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

[Storybook] Configure Aphrodite to Not Append !important to Styles #2107

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/forty-months-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/perseus-dev-ui": patch
---

[Storybook] Configure Aphrodite to Not Append !important to Styles
20 changes: 20 additions & 0 deletions .storybook/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import viteConfig from "../dev/vite.config";

Check warning on line 1 in .storybook/main.ts

View workflow job for this annotation

GitHub Actions / Lint, Typecheck, Format, and Test (ubuntu-latest, 20.x)

File ignored by default.
import {mergeConfig} from "vite";

import type {StorybookConfig} from "@storybook/react-vite";

// This is a temporary plugin option to mimic what is in PROD in regard to cascade layers.
// Perseus LESS files are wrapped in the 'shared' layer in Webapp.
// To get the same ordering of precedence in Storybook, the imported LESS files need to be wrapped accordingly.
// Once the LESS files have cascade layers included (a more involved task for a later time),
// then the following plugin option should be removed.
Comment on lines +9 to +10
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for documenting the exit path. Do you think it's worth creating a ticket and noting it here?

const lessWrapper = {
name: "wrap-less-in-layer",
transform: (code: string, pathname: string) => {
if (pathname.endsWith(".less")) {
const layerStatements =
"@layer reset, shared, legacy;\n@layer shared";
return {
code: `${layerStatements} { ${code} }`,
map: null,
};
}
},
};

const config: StorybookConfig = {
framework: "@storybook/react-vite",

Expand Down Expand Up @@ -53,6 +72,7 @@
},
// Fix from: https://github.com/storybookjs/storybook/issues/25256#issuecomment-1866441206
assetsInclude: ["/sb-preview/runtime.js"],
plugins: [...viteConfig.plugins, lessWrapper],
});
},
staticDirs: ["../static"],
Expand Down
4 changes: 4 additions & 0 deletions dev/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export default defineConfig({
hubble: resolve(__dirname, "../vendor/hubble/hubble.js"),
raphael: resolve(__dirname, "../vendor/raphael/raphael.js"),
jsdiff: resolve(__dirname, "../vendor/jsdiff/jsdiff.js"),
aphrodite: resolve(
__dirname,
"../node_modules/aphrodite/no-important",
),
...packageAliases,
},
},
Expand Down
Loading