Skip to content

Commit

Permalink
css: add a global-css loader with global symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 18, 2023
1 parent 9ff3860 commit 7db1264
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 163 deletions.
12 changes: 7 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

* Implement local CSS names ([#20](https://github.com/evanw/esbuild/issues/20))

This release introduces a new loader called `local-css` and two new pseudo-class selectors `:local()` and `:global()`. This is a partial implementation of the popular [CSS modules](https://github.com/css-modules/css-modules) approach for avoiding unintentional name collisions in CSS. I'm not calling this feature "CSS modules" because although some people in the community call it that, other people in the community have started using "CSS modules" to refer to [something completely different](https://github.com/WICG/webcomponents/blob/60c9f682b63c622bfa0d8222ea6b1f3b659e007c/proposals/css-modules-v1-explainer.md) and now CSS modules is an overloaded term.
This release introduces two new loaders called `global-css` and `local-css` and two new pseudo-class selectors `:local()` and `:global()`. This is a partial implementation of the popular [CSS modules](https://github.com/css-modules/css-modules) approach for avoiding unintentional name collisions in CSS. I'm not calling this feature "CSS modules" because although some people in the community call it that, other people in the community have started using "CSS modules" to refer to [something completely different](https://github.com/WICG/webcomponents/blob/60c9f682b63c622bfa0d8222ea6b1f3b659e007c/proposals/css-modules-v1-explainer.md) and now CSS modules is an overloaded term.

Here's how this new local CSS name feature works with esbuild:

* Identifiers that look like `.className` and `#idName` are global with the `css` loader and local with the `local-css` loader. Global identifiers are the same across all files (the way CSS normally works) but local identifiers are different between different files. If two separate CSS files use the same local identifier `.button`, esbuild will automatically rename one of them so that they don't collide. This is analogous to how esbuild automatically renames JS local variables with the same name in separate JS files to avoid name collisions.
* Identifiers that look like `.className` and `#idName` are global with the `global-css` loader and local with the `local-css` loader. Global identifiers are the same across all files (the way CSS normally works) but local identifiers are different between different files. If two separate CSS files use the same local identifier `.button`, esbuild will automatically rename one of them so that they don't collide. This is analogous to how esbuild automatically renames JS local variables with the same name in separate JS files to avoid name collisions.

* It only makes sense to use local CSS names with esbuild when you are also using esbuild's bundler to bundle JS files that import CSS files. When you do that, esbuild will generate one export for each local name in the CSS file. The JS code can import these names and use them when constructing HTML DOM. For example:

Expand Down Expand Up @@ -53,7 +53,7 @@
This feature only makes sense to use when bundling is enabled both because your code needs to `import` the renamed local names so that it can use them, and because esbuild needs to be able to process all CSS files containing local names in a single bundling operation so that it can successfully rename conflicting local names to avoid collisions.
* If you are in a global CSS file (with the `css` loader) you can create a local name using `:local()`, and if you are in a local CSS file (with the `local-css` loader) you can create a global name with `:global()`. So the choice of the `css` loader vs. the `local-css` loader just sets the default behavior for identifiers, but you can override it on a case-by-case basis as necessary. For example:
* If you are in a global CSS file (with the `global-css` loader) you can create a local name using `:local()`, and if you are in a local CSS file (with the `local-css` loader) you can create a global name with `:global()`. So the choice of the `global-css` loader vs. the `local-css` loader just sets the default behavior for identifiers, but you can override it on a case-by-case basis as necessary. For example:
```css
:local(.button) {
Expand All @@ -64,7 +64,7 @@
}
```
Processing this CSS file with esbuild will result in something like this:
Processing this CSS file with esbuild with either the `global-css` or `local-css` loader will result in something like this:
```css
.stdin_button {
Expand All @@ -77,7 +77,9 @@
* The names that esbuild generates for local CSS names are an implementation detail and are not intended to be hard-coded anywhere. The only way you should be referencing the local CSS names in your JS or HTML is with an `import` statement in JS that is bundled with esbuild, as demonstrated above. For example, when `--minify` is enabled esbuild will use a different name generation algorithm which generates names that are as short as possible (analogous to how esbuild minifies local identifiers in JS).
* You can easily use both global CSS files and local CSS files simultaneously if you give them different file extensions. For example, you could pass `--loader:.module.css=local-css` to esbuild so that `.css` files still use global names by default but `.module.css` files use local names by default.
* You can easily use both global CSS files and local CSS files simultaneously if you give them different file extensions. For example, you could pass `--loader:.css=global-css` and `--loader:.module.css=local-css` to esbuild so that `.css` files still use global names by default but `.module.css` files use local names by default.
* Keep in mind that the `css` loader is different than the `global-css` loader. The `:local` and `:global` annotations are not enabled with the `css` loader and will be passed through unchanged. This allows you to have the option of using esbuild to process CSS containing while preserving these annotations. It also means that local CSS names are disabled by default for now (since the `css` loader is currently the default for CSS files). The `:local` and `:global` syntax may be enabled by default in a future release.
Note that esbuild's implementation does not currently have feature parity with other implementations of modular CSS in similar tools. This is only a preliminary release with a partial implementation that includes some basic behavior to get the process started. Additional behavior may be added in future releases. In particular, this release does not implement:

Expand Down
4 changes: 2 additions & 2 deletions cmd/esbuild/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ var helpText = func(colors logger.Colors) string {
is browser and cjs when platform is node)
--loader:X=L Use loader L to load file extension X, where L is
one of: base64 | binary | copy | css | dataurl |
empty | file | js | json | jsx | local-css | text |
ts | tsx
empty | file | global-css | js | json | jsx |
local-css | text | ts | tsx
--minify Minify the output (sets all --minify-* flags)
--outdir=... The output directory (for multiple entry points)
--outfile=... The output file (for one entry point)
Expand Down
2 changes: 1 addition & 1 deletion internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func parseFile(args parseArgs) {
result.file.inputFile.Repr = &graph.JSRepr{AST: ast}
result.ok = ok

case config.LoaderCSS, config.LoaderLocalCSS:
case config.LoaderCSS, config.LoaderGlobalCSS, config.LoaderLocalCSS:
ast := args.caches.CSSCache.Parse(args.log, source, css_parser.OptionsFromConfig(loader, &args.options))
result.file.inputFile.Repr = &graph.CSSRepr{AST: ast}
result.ok = true
Expand Down
124 changes: 45 additions & 79 deletions internal/bundler_tests/bundler_css_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,100 +301,65 @@ func TestImportLocalCSSFromJSMinifyIdentifiersAvoidGlobalNames(t *testing.T) {
}

func TestImportCSSFromJSLocalVsGlobal(t *testing.T) {
css_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
import "./foo.css"
import "./bar.module.css"
`,
"/foo.css": `
.GLOBAL { color: #000 }
:global(.GLOBAL) { color: #001 }
:local(.local) { color: #002 }
div:global(.GLOBAL) { color: #003 }
div:local(.local) { color: #004 }
.GLOBAL:global(div) { color: #005 }
.GLOBAL:local(div) { color: #006 }
:global(div.GLOBAL) { color: #007 }
:local(div.local) { color: #008 }
div:global(span.GLOBAL) { color: #009 }
div:local(span.local) { color: #00A }
div:global(#GLOBAL0.GLOBAL1.GLOBAL2):local(.local0.local1#local2) { color: #00B }
div:global(#GLOBAL0 .GLOBAL1 .GLOBAL2):local(.local0 .local1 #local2) { color: #00C }
.nested {
:global(&.GLOBAL) { color: #00D }
:local(&.local) { color: #00E }
css := `
.top_level { color: #000 }
&:global(.GLOBAL) { color: #00F }
&:local(.local) { color: #010 }
}
:global(.GLOBAL0, .GLOBAL1) { color: #011 }
:local(.local0, .local1) { color: #012 }
div:global(.GLOBAL0, .GLOBAL1) { color: #013 }
div:local(.local0, .local1) { color: #014 }
:global(.GLOBAL) { color: #001 }
:local(.local) { color: #002 }
div :global(.GLOBAL0, .GLOBAL1) span { color: #015 }
div :local(.local0, .local1) span { color: #016 }
div:global(.GLOBAL) { color: #003 }
div:local(.local) { color: #004 }
div :global(.GLOBAL0 .GLOBAL1) span { color: #017 }
div :local(.local0 .local1) span { color: #018 }
.top_level:global(div) { color: #005 }
.top_level:local(div) { color: #006 }
div > :global(.GLOBAL0 ~ .GLOBAL1) + span { color: #019 }
div > :local(.local0 ~ .local1) + span { color: #01A }
`,
"/bar.module.css": `
.local { color: #000 }
:global(.GLOBAL) { color: #001 }
:local(.local) { color: #002 }
:global(div.GLOBAL) { color: #007 }
:local(div.local) { color: #008 }
div:global(.GLOBAL) { color: #003 }
div:local(.local) { color: #004 }
div:global(span.GLOBAL) { color: #009 }
div:local(span.local) { color: #00A }
.local:global(div) { color: #005 }
.local:local(div) { color: #006 }
div:global(#GLOBAL_A.GLOBAL_B.GLOBAL_C):local(.local_a.local_b#local_c) { color: #00B }
div:global(#GLOBAL_A .GLOBAL_B .GLOBAL_C):local(.local_a .local_b #local_c) { color: #00C }
:global(div.GLOBAL) { color: #007 }
:local(div.local) { color: #008 }
.nested {
:global(&.GLOBAL) { color: #00D }
:local(&.local) { color: #00E }
div:global(span.GLOBAL) { color: #009 }
div:local(span.local) { color: #00A }
&:global(.GLOBAL) { color: #00F }
&:local(.local) { color: #010 }
}
div:global(#GLOBAL0.GLOBAL1.GLOBAL2):local(.local0.local1#local2) { color: #00B }
div:global(#GLOBAL0 .GLOBAL1 .GLOBAL2):local(.local0 .local1 #local2) { color: #00C }
:global(.GLOBAL_A, .GLOBAL_B) { color: #011 }
:local(.local_a, .local_b) { color: #012 }
.nested {
:global(&.GLOBAL) { color: #00D }
:local(&.local) { color: #00E }
div:global(.GLOBAL_A, .GLOBAL_B) { color: #013 }
div:local(.local_a, .local_b) { color: #014 }
&:global(.GLOBAL) { color: #00F }
&:local(.local) { color: #010 }
}
div :global(.GLOBAL_A, .GLOBAL_B) span { color: #015 }
div :local(.local_a, .local_b) span { color: #016 }
:global(.GLOBAL0, .GLOBAL1) { color: #011 }
:local(.local0, .local1) { color: #012 }
div :global(.GLOBAL_A .GLOBAL_B) span { color: #017 }
div :local(.local_a .local_b) span { color: #018 }
div:global(.GLOBAL0, .GLOBAL1) { color: #013 }
div:local(.local0, .local1) { color: #014 }
div > :global(.GLOBAL_A ~ .GLOBAL_B) + span { color: #019 }
div > :local(.local_a ~ .local_b) + span { color: #01A }
`

div :global(.GLOBAL0, .GLOBAL1) span { color: #015 }
div :local(.local0, .local1) span { color: #016 }
div :global(.GLOBAL0 .GLOBAL1) span { color: #017 }
div :local(.local0 .local1) span { color: #018 }
css_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.js": `
import normalStyles from "./normal.css"
import globalStyles from "./LOCAL.global-css"
import localStyles from "./LOCAL.local-css"
div > :global(.GLOBAL0 ~ .GLOBAL1) + span { color: #019 }
div > :local(.local0 ~ .local1) + span { color: #01A }
console.log('should be empty:', normalStyles)
console.log('fewer local names:', globalStyles)
console.log('more local names:', localStyles)
`,
"/normal.css": css,
"/LOCAL.global-css": css,
"/LOCAL.local-css": css,
},
entryPaths: []string{"/entry.js"},
options: config.Options{
Expand All @@ -403,7 +368,8 @@ func TestImportCSSFromJSLocalVsGlobal(t *testing.T) {
ExtensionToLoader: map[string]config.Loader{
".js": config.LoaderJS,
".css": config.LoaderCSS,
".module.css": config.LoaderLocalCSS,
".global-css": config.LoaderGlobalCSS,
".local-css": config.LoaderLocalCSS,
},
},
})
Expand Down
Loading

0 comments on commit 7db1264

Please sign in to comment.