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

feat: Using CSS data urls for bundling #513

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

feat: Using CSS data urls for bundling #513

wants to merge 1 commit into from

Conversation

nmn
Copy link
Contributor

@nmn nmn commented Apr 1, 2024

Resolving #297

Idea

The idea is that the Babel plugin should be able to generate import statements that import CSS as data URLs. After this, bundlers should be able to generate CSS bundles to include this CSS and a PostCSS/LightningCSS plugin can post-process the CSS file after the fact.

Challenge

I am unable to figure out a valid format for CSS data URL that can be put into a JS import statement.

I tried to come up with the simplest possible example of trying to inject body{background:pink} and tried the following import statements:

import 'data:text/css;charset=utf-8,body{background:pink}';
import 'data:text/css;charset=utf-8,body{background:pink}';
import 'css?data:text/css;charset=utf-8,body{background:pink}';
import '?css:data:text/css;charset=utf-8,body{background:pink}';
import 'css-loader?data:text/css;charset=utf-8,body{background:pink}';
import 'data:text/css;charset=utf-8,body{background:pink}?css';
import 'data:text/css;charset=utf-8,body{background:pink}?css-loader';

import 'data:text/css;charset=utf-8,body{background:pink}';
import 'data:text/css;charset=utf-8,body%7Bbackground%3Apink%7D';
import 'css?data:text/css;charset=utf-8,body%7Bbackground%3Apink%7D';
import '?css:data:text/css;charset=utf-8,body%7Bbackground%3Apink%7D';
import 'css-loader?data:text/css;charset=utf-8,body%7Bbackground%3Apink%7D';
import 'data:text/css;charset=utf-8,body%7Bbackground%3Apink%7D?css';
import 'data:text/css;charset=utf-8,body%7Bbackground%3Apink%7D?css-loader';

import 'data:text/css,body{background:pink}';
import 'data:text/css,body%7Bbackground%3Apink%7D';
import 'css?data:text/css,body%7Bbackground%3Apink%7D';
import '?css:data:text/css,body%7Bbackground%3Apink%7D';
import 'css-loader?data:text/css,body%7Bbackground%3Apink%7D';
import 'data:text/css,body%7Bbackground%3Apink%7D?css';
import 'data:text/css,body%7Bbackground%3Apink%7D?css-loader';

import 'style-loader?data:text/css;charset=utf-8,body{background:pink}';
import 'style-loader?data:text/css;charset=utf-8,body%7Bbackground%3Apink%7D';
import 'style-loader?data:text/css,body%7Bbackground%3Apink%7D';

I wasn't able to get any of them work in a fresh NextJS example. Webpack did not understand any of these imports.

I tried the same in Vite with similar results.

Is there a particular way to make bundlers bundle CSS provided this way?

Is this approach even feasible? I'm putting up this PR in case someone has further insight here.

We are independently working on a CLI to work around bundlers entirely in the meantime.

@nmn nmn marked this pull request as draft April 1, 2024 07:56
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 1, 2024
Copy link

github-actions bot commented Apr 1, 2024

compressed-size: runtime library

Size change: 0.00 kB
Total size: 2.52 kB

View unchanged
Filename: gzip (minify) kB size kB change % change
./packages/stylex/lib/stylex.js 1.04 (3.22) 0.00 (0.00) 0.0% (0.0%)
./packages/stylex/lib/StyleXSheet.js 1.48 (3.75) 0.00 (0.00) 0.0% (0.0%)

Copy link

github-actions bot commented Apr 1, 2024

compressed-size: e2e bundles

Size change: 0.00 kB
Total size: 1128.55 kB

View unchanged
Filename: gzip (minify) kB size kB change % change
./apps/rollup-example/.build/bundle.js 1005.20 (10185.36) 0.00 (0.00) 0.0% (0.0%)
./apps/rollup-example/.build/stylex.css 123.34 (773.82) 0.00 (0.00) 0.0% (0.0%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants