Skip to content

Replace CSS comment extraction with data URL imports for Turbopack compatibility #315

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

Open
jantimon opened this issue Apr 11, 2025 · 1 comment
Labels
enhancement New feature or request swc plugin

Comments

@jantimon
Copy link
Collaborator

jantimon commented Apr 11, 2025

Currently, next-yak extracts CSS from tagged template literals through our SWC plugin, which adds CSS as comments in the output JS file. A custom webpack loader then extracts these comments and processes them through the CSS module system. This works but relies on webpack-specific syntax (!=!) and a complex extraction mechanism.

Turbopack is planning to support CSS modules via direct data URL imports. We should investigate replacing the comment extraction approach with data URL imports to simplify our architecture and prepare for Turbopack compatibility:

Instead of generating:

/*YAK Extracted CSS: .button { color: red; } */

import styles from "./Component.yak.module.css!=!./Component?./Component.yak.module.css";

We would generate:

import styles from "data:text/css+module,.button{color:red}";

This would eliminate our custom comment serialization and parsing steps, use standard module loading, and align with Turbopack's architecture. So the SWC plugin would generate these imports directly with one single compile step

Some important considerations:

  • Our cross-file dependency resolution would need reworking
  • Data URL encoding must properly handle CSS escaping
  • We need to verify Next.js's CSS module system works with this approach

This might solve #112 and could help us in future as this format is supported by webpack turbopack rspack and vite

@Mad-Kat Mad-Kat added enhancement New feature or request swc plugin labels Apr 12, 2025
@jantimon
Copy link
Collaborator Author

jantimon commented Apr 14, 2025

It looks like webpack supports it but next.js does not (at least by now) - turbopack will probably add better support soon:
vercel/next.js#78096

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request swc plugin
Development

No branches or pull requests

2 participants