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

docs: add performance documentation with purgecss and csso #3648

Merged
merged 6 commits into from
Jan 8, 2025
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
67 changes: 67 additions & 0 deletions packages/foundations/docs/Performance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Performance

If you need to improve the performance of your application, you can use the following tips:

## Minify with PurgeCSS and CSSO

When you use the full bundled `.css` file we provide, you could easily reduce the file size by removing unused CSS classes. This can be done with [PurgeCSS](https://purgecss.com/) and [CSSO](https://github.com/css/csso).

Install both with:

```shell
npm i purgecss csso --save-dev
```

Next you should create a file, e.g. `purgecss.js` in your project root with the following content:

```javascript
import { writeFileSync } from "node:fs";

import { PurgeCSS } from "purgecss";
import { minify } from "csso";

const distFolder = "dist"; // TODO: Change me if you need another folder

new PurgeCSS()
.purge({
content: [`${distFolder}/**/*.html`, `${distFolder}/**/*.js`],
css: [`${distFolder}/**/*.css`],
defaultExtractor: (content) => content.match(/[\w-/:]+(?<!:)/g) || [],
variables: true,
rejectedCss: true,
safelist: {
variables: [
/* TODO: Keep only the densities you need */
/-functional-/,
/-regular-/,
/-expressive-/,
/* Keep density & all color properties/variables */
/-default$/,
/-hovered$/,
/-pressed$/
],
/* Some components require a safelist */
greedy: [
/db-tabs/ // TODO: Add more components if necessary
]
}
})
.then((purgeCSSResult) => {
for (const result of purgeCSSResult) {
writeFileSync(result.file, minify(result.css).css);

/* Optional: for debugging */
// writeFileSync(`rejected.css`, result.rejectedCss || "");
}
});
```

You can run this script with `node purgecss.js` and it will minify your CSS files. You can also add this script to your `package.json` to run after your regular build process:

```json
{
"scripts": {
"postbuild": "node purgecss.js"
}
}
```
3 changes: 2 additions & 1 deletion showcases/patternhub/data/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ export const ROUTES: NavigationItem[] = [
label: 'Testing Overview Table',
path: '/foundations/test-table'
},
{ label: 'IDE Support', path: '/foundations/ide' }
{ label: 'IDE Support', path: '/foundations/ide' },
{ label: 'Performance', path: '/foundations/performance' }
]
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import DefaultPage from "../../../components/default-page";

import Readme from "../../../../../packages/foundations/docs/Performance.md";

<Readme />

export default ({ children }) => <DefaultPage>{children}</DefaultPage>;
2 changes: 1 addition & 1 deletion showcases/patternhub/styles/highlight.scss
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pre:has(.hljs):not(:has(.language-shell)) {
}
.hljs-subst {
/* prettylights-syntax-storage-modifier-import */
color: colors.$db-neutral-bg-basic-level-1-default;
color: colors.$db-warning-on-bg-basic-emphasis-90-default;
}
.hljs-section {
/* prettylights-syntax-markup-heading */
Expand Down
Loading