fix: minify textContent#10024
Conversation
Shaves off 38 bytes from every bundle
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR reduces bundle size by replacing multi-line CSS template literals (with .trim()) used for injected <style> elements with minified one-line CSS strings.
Changes:
- Minifies injected CSS in
preventScrollMobileSafariby switching to a compact string literal. - Minifies injected CSS for
usePress’stouch-actionrule by switching to a compact template literal.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/react-aria/src/overlays/usePreventScroll.ts | Replaces multi-line injected CSS with a minified one-line CSS string. |
| packages/react-aria/src/interactions/usePress.ts | Replaces multi-line injected CSS with a minified one-line CSS template literal. |
|
Thanks, looks like lint is failing |
|
Isn't that |
|
Trim did what trim does - trimmed whitespace from either ends. Vite's minifier was smart enough to run trim at build time, so these strings were semi-optimized. No function could, however, just minify CSS as strings: indentation, useless semicolons. |
| } | ||
| } | ||
| `.trim(); | ||
| style.textContent=`@layer{[${PRESSABLE_ATTRIBUTE}]{touch-action:pan-x pan-y pinch-zoom}}` |
There was a problem hiding this comment.
Instead, I think we could handle this with a macro. Then we don't need to sacrifice readability and we get any smarts from css minifiers. It might be overkill given that we only do this twice and they are pretty small. @devongovett would appreciate your thoughts
import type {MacroContext} from '@parcel/macros';
import {transform} from 'lightningcss';
export function cssString(this: MacroContext | void, css: string): string {
if ((this == null || this === globalThis) && process.env.NODE_ENV !== 'test') {
throw new Error('The cssString macro must be imported with {type: "macro"}.');
}
try {
let {code} = transform({
filename: 'cssString.css',
code: Buffer.from(css),
minify: true,
sourceMap: false
});
return code.toString();
} catch (err) {
throw new Error(`cssString macro: failed to parse or minify CSS: ${err}`);
}
}
then call it like so:
style.textContent = cssString(`
@layer {
[${PRESSABLE_ATTRIBUTE}] {
touch-action: pan-x pan-y pinch-zoom;
}
}
`);
Shaves off 38 bytes from every bundle.
.trim()isn't enough to actually minify CSS, so you end up with lots of redundant whitespace even after minification.✅ Pull Request Checklist:
📝 Test Instructions:
🧢 Your Project: