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

[MVP] Bundle CSS modules #20

Closed
evanw opened this issue Feb 18, 2020 · 86 comments
Closed

[MVP] Bundle CSS modules #20

evanw opened this issue Feb 18, 2020 · 86 comments

Comments

@evanw
Copy link
Owner

evanw commented Feb 18, 2020

I want esbuild to demonstrate that it's possible for a normal web development workflow to have high-performance tools. I consider bundling CSS modules a part of my initial MVP feature set for esbuild since some form of CSS modularity is essential for a large-scale web app and I find CSS modules an elegant solution. This issue tracks the CSS module bundling part of my MVP.

At the bare minimum, importing a CSS file should ensure it's included in the bundle. I plan to also implement CSS modules, which makes CSS selector names local to the file and allows JavaScript to import the selector names as strings.

There are interesting extensions to think about once I get everything working. Tree shaking of unused CSS is an optimization that seems worth exploring, for example. That should be possible with CSS modules since unused imports correspond to unused CSS.

@eberkund
Copy link

Would this include font files, or is that a separate task?

@evanw
Copy link
Owner Author

evanw commented Jul 24, 2020

Binary resources that don't need to be parsed such as PNG, JPEG, and font files are all the same to a bundler. Fonts don't need to be special cased as far as I'm aware. I think the existing loaders such as dataurl and file should cover fonts.

@evanw
Copy link
Owner Author

evanw commented Sep 13, 2020

I'm starting to work on CSS support. I just landed a basic parser and AST, and will be working on integrating it into the build system next.

Note that I'm not planning to support the full CSS ecosystem with this feature. Today's CSS is a very diverse ecosystem with many non-standard language variants. This is possible in part due to the fault-tolerant nature of CSS. I'm imagining this feature as mainly a "CSS linker" that is responsible for the critical parts of joining CSS files together and removing unused CSS code. The CSS parser will be expecting real CSS for use with browsers. Non-standard extensions may still be parsed without syntax errors but may be passed through unmodified (e.g. not properly minified or renamed) since they aren't fully understood by the parser. It should be possible to use syntax extensions (e.g. SASS) with a plugin that transforms the source code before esbuild reads it.

@LarsDenBakker
Copy link

@evanw that's great to hear! There are different kinds of CSS modules out there, and there is also ongoing for a web standard for CSS modules (see whatwg/html#4898 and https://github.com/tc39/proposal-import-assertions). WIll esbuild allow specifying which variant to use?

@nitsky
Copy link
Contributor

nitsky commented Sep 13, 2020

@evanw will the CSS feature inline the minified CSS in the javascript bundle or emit it as a separate file? While inlining is a good fit for client rendered applications, it is not ideal for server or static rendered applications. Ideally, esbuild would emit a number of CSS chunks, and the metafile would detail which CSS chunks correspond to each javascript entrypoint. A server or build tool would then be able to add references to the CSS for each page using <link> tags in the HTML it generates.

@evanw
Copy link
Owner Author

evanw commented Sep 13, 2020

https://github.com/tc39/proposal-import-assertions

Import assertions seem unrelated to CSS to me. They just look like a way to tell the runtime what the file type is. Bundlers already have a way of doing this: they just use the file extension without needing an inline annotation, so this information is not necessary for bundlers.

whatwg/html#4898

This seems like an API to inject normal CSS inside a shadow DOM element? It looks like it would just be normal CSS as far as the bundler is concerned.

The CSS module implementation I'm thinking of adopting is the original one: https://github.com/css-modules/css-modules. Specifically, the use of local-by-default names, the :local and :global selectors, the composes declarations, and being able to import local names as strings in JavaScript.

will the CSS feature inline the minified CSS in the javascript bundle or emit it as a separate file?

It will be in a separate file. I believe this is the current expected behavior. It's how Parcel works, for example. I was not considering inlining it in the JavaScript code. I am also planning on having code splitting work the same way across JavaScript and CSS files.

@LarsDenBakker
Copy link

I listed import assertions just to indicate that there will be a standard way to import non-JS files.

The idea for the standard CSS modules is that they will return an instance of CSSStyleSheet (https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet and https://developers.google.com/web/updates/2019/02/constructable-stylesheets). So a "polyfill" would instantiate it with the css text. Shadow roots have a nice API for using those stylesheets, but they're not necessarily coupled to it. Hope fully it will be adopted more broadly.

My main concern is about what's enabled by default in esbuild, so far most (all?) things require an explicit opt-in which I think is important.

@evanw
Copy link
Owner Author

evanw commented Sep 13, 2020

The idea for the standard CSS modules is that they will return an instance of CSSStyleSheet (https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet and https://developers.google.com/web/updates/2019/02/constructable-stylesheets). So a "polyfill" would instantiate it with the css text. Shadow roots have a nice API for using those stylesheets, but they're not necessarily coupled to it. Hope fully it will be adopted more broadly.

Interesting, thanks for the clarification. FWIW I think this should be trivial to do with a plugin. That will let you experiment with this proposal.

My main concern is about what's enabled by default in esbuild, so far most (all?) things require an explicit opt-in which I think is important.

My current plan:

  • By default, esbuild will use the css loader for .css files which will only interpret @import statements and use those for bundling CSS together. This should also naturally work with code splitting without any additional configuration. When a CSS file is used as an entry point, the resulting output file will also be a CSS file without any JavaScript file (i.e. CSS will have first-class support).

  • When a JavaScript file does import "./file.css" that will cause the generation of a parallel set of CSS files alongside the output files for the JavaScript entry points. So if you run esbuild on app.ts and that imports a JavaScript file that imports a CSS file, the output folder will contain app.js and app.css. When a JavaScript file does import * as styles from "./file.css" the styles object will be empty (no properties).

  • CSS modules are opt-in by using the css-module loader. I may have a default extension mapping built-in for .module.css to override the behavior of .css, since that's a common convention in the wild. Only then will import * as styles from "./file.css" have any properties. This loader will still have the same output file generation behavior as the normal CSS loader. All imported CSS files will be bundled together into a single CSS output file (or perhaps more than one if code splitting is active and there are multiple entry points).

@LarsDenBakker
Copy link

Thanks, I think that default makes sense 👍

@nitsky
Copy link
Contributor

nitsky commented Sep 13, 2020

That behavior sounds ideal. Can't wait for it!

evanw added a commit that referenced this issue Sep 27, 2020
evanw added a commit that referenced this issue Sep 27, 2020
@evanw
Copy link
Owner Author

evanw commented Sep 27, 2020

The newly-released version 0.7.7 now has experimental CSS support. From the release notes:

This release introduces the new css loader, enabled by default for .css files. It has the following features:

  • You can now use esbuild to process CSS files by passing a CSS file as an entry point. This means CSS is a new first-class file type and you can use it without involving any JavaScript code at all.

  • When bundling is enabled, esbuild will bundle multiple CSS files together if they are referenced using the @import "./file.css"; syntax. CSS files can be excluded from the bundle by marking them as external similar to JavaScript files.

  • There is basic support for pretty-printing CSS, and for whitespace removal when the --minify flag is present. There isn't any support for CSS syntax compression yet. Note that pretty-printing and whitespace removal both rely on the CSS syntax being recognized. Currently esbuild only recognizes certain CSS syntax and passes through unrecognized syntax unchanged.

Some things to keep in mind:

  • CSS support is a significant undertaking and this is the very first release. There are almost certainly going to be issues. This is an experimental release to land the code and get feedback.

  • There is no support for CSS modules yet. Right now all class names are in the global namespace. Importing a CSS file into a JavaScript file will not result in any import names.

  • There is currently no support for code splitting of CSS. I haven't tested multiple entry-point scenarios yet and code splitting will require additional changes to the AST format.

There's still a long way to go but this feels like a good point to publish what's there so far. It should already be useful for a limited set of use cases, and then I will expand use cases over time.

This feature will be especially useful with the addition of the plugin API (see issue #111) because then esbuild's bundler can be a "CSS linker" that runs on the output of whatever CSS post-processor you're using. The plugin API hasn't been released yet because I wanted to get basic CSS support in first so that there are at least two different core file types for the API to abstract over.

@evanw
Copy link
Owner Author

evanw commented Sep 29, 2020

FYI for people following this issue: #415 was recently implemented making it possible to bundle url(...) references to images in CSS.

@wessberg
Copy link

wessberg commented Oct 29, 2020

Hey there, good work on this.

Just wanted to share my experience of trying this out. I was hoping that import styles from "./my-css-file.css" would yield a CSSStyleSheet that can be used in conjunction with Constructable Stylesheets, as @LarsDenBakker was also pointing out in this comment. As it stands right now, I'm actually not seeing how I might be able to retrieve the StyleSheet or a string representing it such that I can work with it (without implementing it in JS/TS).

I'm aware that there is a gazillion ways of handling styles on the web. I'm also aware that the expectations for how this might work varies greatly from web developer to web developer. I'm also aware that many tools do the module-returning-class-names thing, probably the vast majority. But I would argue strongly in favor of .css files becoming modules with a default export pointing to a CSSStyleSheet. For the following reasons:

  • This is in line with current standardization efforts as seen with Import Assertions which is the foundation behind the JSON Modules proposal (which will most likely also be foundation behind CSS Modules).

  • This allows for passing styles to a root (such as the document element or any Shadow Root) via adoptedStyleSheets.

  • This simplifies the implementation. In time, developers can leverage the plugin infrastructure to "override" the default behavior for different behavior, for example old-fashioned CSS Modules that returns an object of class names, etc.

Web developers who work with Shadow DOM are relying on being able to append stylesheets to a root at any point in the DOM tree, and won't benefit from, say, the imported styles being appended to the document root.

@evanw
Copy link
Owner Author

evanw commented Nov 12, 2020

@wessberg Can you confirm if the plugin API lets you write a plugin to enable the CSSStyleSheet use case? I'm unfamiliar with the proposal but I imagine writing a plugin for this should be pretty simple if you just need to construct an object and return it.

The form of CSS modules where class names are local to the file and are exported to JavaScript seems better for building scalable web applications to me. That enables tree shaking and code splitting for CSS, which other solutions don't support AFAIK. Tree shaking of CSS isn't really possible to build as an esbuild plugin because it requires deep integration with the JavaScript layer. So I am still planning to explore this in esbuild itself.

@wessberg
Copy link

wessberg commented Nov 12, 2020

I will test it out and get back to you :-)

And again, while I realize some other bundlers are doing something similar to what you're planning for with CSS support in esbuild, I'm just seeing a pretty massive gap between what is being worked on in the standards bodies and what is being implemented or have already been implemented across some bundlers. This might lead to confusion when the native runtime does things rapidly different to bundlers.

Importantly, it leaves out everyone working with Shadow DOM where styles are applied at roots across the DOM tree rather than globally at the document root, and where styles are already local to the tree from the Shadow root they are being applied to. These users will either need to declare the styles in JS/TS and either inline them in a <style> tag appended to the root or manually generate CSSStyleSheets from them or write a custom plugin as suggested, and add it to adoptedStyleSheets for that root.

I think that the more "standards-compliant" (I put this in quotes, because standardization efforts are still ongoing here) approach should be default, and the more application-specific behavior should come from plugins.

But I of course respect your decision. I come from Rollup, which is pretty founded in being as close to native ES module semantics as practically possible, but there are obvious advantages to esbuild (ease of use, performance), and I want this tool to succeed for everyone, including those that use Shadow DOM :-)

@wessberg
Copy link

Promised I would get back to you. The plugin system works great! :-) It works as expected. Would be neat if plugins could get ahold of the esbuild options. For example, I wanted to know if esbuild was instructed to produce source maps from inside the plugin but didn't have any way of knowing, as far as I'm aware.

@iamakulov
Copy link

Hey Evan!

I was just curious if you have any roadmap for CSS, by chance. I saw mentions of features that are still lacking (#468 (comment)) plus mentions that a CSS rewrite is on the to-do list (#519 (comment)), but there’s no full picture at the moment, as far as I’m aware.

I’m writing this primary because we at Framer are migrating to ESBuild, and there’re two CSS issues we’re currently facing:

So I’m trying to figure out how much we should invest into workarounds for these on our side. (Like, what if the CSS rewrites comes out in a week and fixes both issues?)

@evanw
Copy link
Owner Author

evanw commented Dec 18, 2020

Yeah CSS support isn't fully baked yet. My first priority right now is to fix JavaScript code splitting since that's a more widely-used feature. After that, getting CSS to a better state will be my first priority.

I'm currently working on a rewrite of JavaScript code splitting to address a number of related features: #399, splitting for iife and cjs, manual chunks, top-level await, entry point hashes. The main aspect of the rewrite is changing code in shared chunks to be lazily-evaluated, which makes a lot of additional use cases possible. Getting all of those working together is pretty complicated and it's unfortunately taking longer than I'd like.

I have limited time to work on this at the moment due to the holidays, so all of this is not going to come out next week. I'm hoping for it to land in the next few months.

@jimmywarting
Copy link

so I'd expect esbuild to just minify/bundle without any side effects.

That was mostly what i wanted to get out of my comment... i would not expect either to get anything else out other than a CSSStylesheet if it now works natively in chrome. my solution is just brainstorming solutions to deliver it.

@intrnl
Copy link

intrnl commented Jul 17, 2023

^ ooo we finally have CSS modules / local class names implemented 👀

@mhsdesign
Copy link

Thanks ❤️ for the native implementation.

In case you need support for composes and local names for keyframe animations, grid lines, etc you can use parcel's Lightning CSS via my adapter: https://github.com/mhsdesign/esbuild-plugin-lightningcss-modules

@joelmoss
Copy link

Amazing! But unfortunately it's only useful for use with JS imports. And that is no good for use within server rendered HTML. Step in the right direction, but quite limited - at least for me.

@terpimost
Copy link

Amazing! But unfortunately it's only useful for use with JS imports. And that is no good for use within server rendered HTML. Step in the right direction, but quite limited - at least for me.

Wait, the renaming is happening in the build step and imports are just references to string values of css classes. Am I missing something?

@joelmoss
Copy link

Wait, the renaming is happening in the build step and imports are just references to string values of css classes. Am I missing something?

Yes, but the exported class names will get renamed if and when collisions occur.

@evanw
Copy link
Owner Author

evanw commented Jul 18, 2023

An initial implementation of the local CSS name transform has now shipped in version 0.18.14. It's off by default for now and must be manually enabled with either the local-css or global-css loaders. Documentation for this is now available here: https://esbuild.github.io/content-types/#local-css.

In addition, esbuild's support for CSS source maps was overhauled to be closer to parity with JS. There are now per-token source mappings in esbuild's CSS output, and esbuild uses the names source map field to provide the original name for renamed CSS names.

If anyone wants to try it out, it would be interesting to hear what you think. Please consider the implementation experimental and subject to change as I plan to improve it more in the future (which is why this issue is still open).

@jimmywarting
Copy link

👍
I think it was a good decision to rename it to something else entirely and having it off by default (for now) given now that it's possible to natively import css files without bundlers now.

I could expect that this css preprocessor tech becomes less relevant the more ppl start to use custom elements (web components)

It's unfortunately that the term "CSS Module" became overloaded.

@clshortfuse
Copy link

I tested my existing plugin for native CSS modules and because plugins are resolved first, it's not an issue even if default is turned on. Though right now we can't differentiate between code that loads

import { button } from './button.css';
import globalStyleSheet from './globals.css' assert { type: 'css' };

Also, files only load once meaning you can't opt into using local-css based on one import type and opt into using the plugin for native CSS Modules.

import globalStyleSheet from './globals.css' assert { type: 'css' };
import { button } from './globals.css';

Nothing major, but just edge cases. Maybe something in OnLoadOptions or OnLoadArgs can be passed to differentiate the two. Nice work!

@evanw
Copy link
Owner Author

evanw commented Jul 18, 2023

Using the assert keyword like that is problematic for a few reasons. First of all, it has been deprecated since it was introduced and will never be a part of JavaScript (the standards committee changed their minds about it). Second of all, import assertions by design can’t affect how the module is loaded (the spec says this explicitly).

Import assertions are potentially going to be replaced with something else called import attributes, which will hypothetically use the with keyword and which may fix this limitation (so attributes will be able to affect how the module is loaded. However, this feature hasn’t been added to any real JavaScript runtime yet so it remains to be seen how it will actually work when it becomes a part of JavaScript for real.

Given that the standards committee has already changed their mind and "unshipped" this proposal after it reached Stage 3, I’m planning to wait to add support for import attributes to esbuild until after there’s an implementation of it shipping in a real JavaScript runtime. This also means waiting to add support for it to any built-in CSS behavior as well as to esbuild’s plugin API.

@jimmywarting

This comment was marked as off-topic.

@joelmoss
Copy link

As I mentioned earlier, this is only useful for use with JS imports. And that is no good for use within server rendered HTML. So is quite limited.

Do you have plans to expand this to expose the renamed class names, perhaps as part of the metafile?

@evanw
Copy link
Owner Author

evanw commented Jul 18, 2023

@jimmywarting This issue is about implementing https://github.com/css-modules/css-modules, not about import attributes. We can discuss how to integrate import attributes into esbuild after they are shipped in a real browser. Please leave the discussion of import attributes out of this issue.

@joelmoss I saw your comment but I'm confused. By server-rendered HTML I'm imagining something like this:

import { button } from './button.css'
console.log(`<div class="${button}"></div>`)

This should already works with what was shipped, as you can see here. Can you elaborate about why that doesn't work in your case?

@joelmoss
Copy link

Can you elaborate about why that doesn't work in your case?

The problem is that you are assuming that I will only be using JS and importing the CSS, which is not always true. Sometimes I'm including CSS with <link> tags in HTML. So this implementation will not work in that case.

To make local-css work outside of JS, esbuild will need to expose the exported CSS classes perhaps as part of the metafile, or support a way of making the exported class names deterministic, similarl to how webpack's CSS-loader does it using localIdentName.

Right now it is not a huge issue for me, as I have already built my own naive implementation of CSS modules, which works so far. But I would much prefer an esbuild native solution.

Hope that helps explain things a little. thx

@evanw
Copy link
Owner Author

evanw commented Jul 18, 2023

I think including the CSS with a <link> tag in the HTML should be fine if the HTML is JS-generated. Something like this. If you're talking about linking to CSS from hand-written (non JS-generated) HTML and then minifying CSS names within the HTML itself then yes, esbuild's local CSS name transform won't work for that use case. But I'd argue that in that situation you probably shouldn't be processing the linked CSS using the local CSS name transform. Maybe providing an example of what you're trying to achieve would help.

To answer your deeper question: I'm open exposing the name mapping somehow at some point, but I haven't figured out how yet. Having specific example use cases for this data to think through will be helpful when adding that feature so thanks for describing what you want to achieve.

@joelmoss
Copy link

My specific use case is with Ruby on Rails, so the server generated HTML is from Ruby, not JS. But sometimes I use React, which is why I have a mix of JS and server (Ruby) generated HTML.

When the HTML is generated from the server, one or more stylesheets are also included as <link> tags, with the src of each being generated by esbuild, and my custom CSS module plugin. If the name of the CSS files ends with .module.css all classnames in that file are renamed to something like [className]-[filenameHash].

Now because these renamed class names are deterministic, and I already know the filenameHash and className, I can then use said class names anywhere in the server generated HTML.

I have a little helper that I can call on the server by simply passing it the name of the class and the path of the CSS file.

So as an example, I have a CSS file at /some/path/to/styles.module.css and a class name of myclass. I add the <link> tag with a src of /some/path/to/styles.module.css into the HTML. Then I calculate the hash of that path, giving me this:

.myclass-abcd1234 {
  color: red;
}
<div class="myclass-abcd1234" />

The above would actually be far better than reading any exposed name mapping, as I do not not need to know anything about the mapping. It would also be a lot less work to implement. There is also no chance of conflicts, as it is safe to assume that all classes within each stylesheet are unique.

Hope that helps explain my use case.

@strogonoff
Copy link

strogonoff commented Jul 31, 2023

Can’t wait for built-in implementation! The existing plugins don’t work well in some cases because they all seem to be based on lightningcss, which requires native Node modules and appears to be poorly compatible with Yarn’s zero-install mode.

@rmannibucau
Copy link

+1 for the provided impl in "last" release, fulfills my expectations and needs for now. Thanks a lot.

@asbjorn-brevio
Copy link

Note that I'm not planning to support the full CSS ecosystem with this feature. Today's CSS is a very diverse ecosystem with many non-standard language variants. This is possible in part due to the fault-tolerant nature of CSS. I'm imagining this feature as mainly a "CSS linker" that is responsible for the critical parts of joining CSS files together and removing unused CSS code. The CSS parser will be expecting real CSS for use with browsers. Non-standard extensions may still be parsed without syntax errors but may be passed through unmodified (e.g. not properly minified or renamed) since they aren't fully understood by the parser. It should be possible to use syntax extensions (e.g. SASS) with a plugin that transforms the source code before esbuild reads it.

Question about the (highlighted) last sentence above:
Is this supported in the current version (0.18.17)?
If not, are there still plans for supporting it?

I tried setting local-css for .module.scss, .module.css, .scss and .css and as far as I can tell none of the classes from the SCSS files are getting renamed (even conflicting ones). The output JS seems to have been transformed as expected though

@clshortfuse
Copy link

@asbjorn-brevio

with a plugin

I believe you need a plug-in to transform SCSS to the custom CSS syntax with :global() and :local() support (not just regular CSS). After that the plugin calls esbuild with the local-css or global-css loader on the transformed CSS and returns its result.

@asbjorn-brevio
Copy link

@clshortfuse Yeah, I'm using esbuild-sass-plugin to transform the SCSS. Is the plugin the problem?

@hyrious
Copy link

hyrious commented Aug 2, 2023

@asbjorn-brevio Yes. If a plugin wants to make use of the local-css loader, it has to set that in the onLoad() callback, or at least return {loader: 'default'} and let user set that in the build options. Here's a minimal example to write such plugin:

// build.mjs
import { build } from "esbuild";
import { compile } from "sass";

var sass_module_plugin = {
  name: "local-sass",
  setup({ onLoad }) {
    onLoad({ filter: /\.module\.scss$/ }, (args) => {
      const { css } = compile(args.path);

      return { contents: css, loader: "local-css" };
      //                              ^^^^^^^^^^^
    });
  },
};

await build({
  entryPoints: ["index.js"],
  bundle: true,
  outdir: "dist",
  plugins: [sass_module_plugin],
}).catch(() => process.exit(1));
// index.js
import { foo } from "./foo.module.scss";
console.log(foo);
/* foo.module.scss */
$name: "foo";
.#{$name} {
  color: red;
}

@asbjorn-brevio
Copy link

@hyrious Got it, thanks!

I've postponed migrating to esbuild awaiting this feature so I haven't internalized how this all works yet. Sorry for the noise.

@strogonoff
Copy link

strogonoff commented Aug 3, 2023

FWIW, local-css is working well for CSS in my simple case so far. I haven’t yet tested it very thoroughly, but in a very simple (for now) frontend migration from esbuild-css-modules-plugin to local-css loader was painless, it required no changes to TSX/CSS sources and let me shed a lot of dependencies right away.

@evanw evanw closed this as completed in a0910fd Aug 7, 2023
@evanw
Copy link
Owner Author

evanw commented Aug 7, 2023

I'm considering this to be implemented now that support for composes is in (see the latest release notes for details). Any additional improvements and/or bug fixes can be tracked using new issues.

evanw added a commit that referenced this issue Aug 8, 2023
@senro
Copy link

senro commented Jul 10, 2024

//index.module.less

.roleAuthorization {
  :global {
     .header::after,.content::after{
            content: '88888888';
      }
  }
}

after local-css loader, turn css like:

.index_module_roleAuthorization .header::after,
.roleAuthorization .content::after {
  content: "88888888";
}

the right css should be:

.index_module_roleAuthorization .header::after,
.index_module_roleAuthorization .content::after {
  content: "88888888";
}

is local-css can't support ',' in css?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests