Skip to content

Commit

Permalink
fix #1426: remove warning about bad CSS "@" rules
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 17, 2021
1 parent 32cf570 commit 7d630eb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## Unreleased

* Remove warning about bad CSS `@`-rules ([#1426](https://github.com/evanw/esbuild/issues/1426))

The CSS bundler built in to esbuild is only designed with real CSS in mind. Running other languages that compile down to CSS through esbuild without compiling them down to CSS first can be a bad idea since esbuild applies browser-style error recovery to invalid syntax and uses browser-style import order that other languages might not be expecting. This is why esbuild previously generated warnings when it encountered unknown CSS `@`-rules.

However, some people want to run other non-CSS languages through esbuild's CSS bundler anyway. So with this release, esbuild will no longer generate any warnings if you do this. But keep in mind that doing this is still potentially unsafe. Depending on the input language, using esbuild's CSS bundler to bundle non-CSS code can still potentially alter the semantics of your code.

## 0.12.15

* Fix a bug with `var()` in CSS color lowering ([#1421](https://github.com/evanw/esbuild/issues/1421))
Expand Down
47 changes: 18 additions & 29 deletions internal/css_parser/css_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,35 +564,24 @@ func (p *parser) parseAtRule(context atRuleContext) css_ast.R {
}

default:
// Warn about unsupported at-rules since they will be passed through
// unmodified and may be part of a CSS preprocessor syntax that should
// have been compiled away but wasn't.
//
// The list of supported at-rules that esbuild draws from is here:
// https://developer.mozilla.org/en-US/docs/Web/CSS/At-rule. Deprecated
// and Firefox-only at-rules have been removed.
if kind == atRuleUnknown {
if atToken == "namespace" {
// CSS namespaces are a weird feature that appears to only really be
// useful for styling XML. And the world has moved on from XHTML to
// HTML5 so pretty much no one uses CSS namespaces anymore. They are
// also complicated to support in a bundler because CSS namespaces are
// file-scoped, which means:
//
// * Default namespaces can be different in different files, in which
// case some default namespaces would have to be converted to prefixed
// namespaces to avoid collisions.
//
// * Prefixed namespaces from different files can use the same name, in
// which case some prefixed namespaces would need to be renamed to
// avoid collisions.
//
// Instead of implementing all of that for an extremely obscure feature,
// CSS namespaces are just explicitly not supported.
p.log.AddRangeWarning(&p.tracker, atRange, "\"@namespace\" rules are not supported")
} else {
p.log.AddRangeWarning(&p.tracker, atRange, fmt.Sprintf("%q is not a known rule name", "@"+atToken))
}
if kind == atRuleUnknown && atToken == "namespace" {
// CSS namespaces are a weird feature that appears to only really be
// useful for styling XML. And the world has moved on from XHTML to
// HTML5 so pretty much no one uses CSS namespaces anymore. They are
// also complicated to support in a bundler because CSS namespaces are
// file-scoped, which means:
//
// * Default namespaces can be different in different files, in which
// case some default namespaces would have to be converted to prefixed
// namespaces to avoid collisions.
//
// * Prefixed namespaces from different files can use the same name, in
// which case some prefixed namespaces would need to be renamed to
// avoid collisions.
//
// Instead of implementing all of that for an extremely obscure feature,
// CSS namespaces are just explicitly not supported.
p.log.AddRangeWarning(&p.tracker, atRange, "\"@namespace\" rules are not supported")
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/css_parser/css_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,12 +706,13 @@ func TestBadQualifiedRules(t *testing.T) {
}

func TestAtRule(t *testing.T) {
expectPrinted(t, "@unknown", "@unknown;\n")
expectPrinted(t, "@unknown;", "@unknown;\n")
expectPrinted(t, "@unknown{}", "@unknown {}\n")
expectPrinted(t, "@unknown x;", "@unknown x;\n")
expectPrinted(t, "@unknown{\na: b;\nc: d;\n}", "@unknown { a: b; c: d; }\n")

expectParseError(t, "@unknown", "<stdin>: warning: \"@unknown\" is not a known rule name\n<stdin>: warning: Expected \"{\" but found end of file\n")
expectParseError(t, "@unknown", "<stdin>: warning: Expected \"{\" but found end of file\n")
expectParseError(t, "@", "<stdin>: warning: Unexpected \"@\"\n")
expectParseError(t, "@;", "<stdin>: warning: Unexpected \"@\"\n")
expectParseError(t, "@{}", "<stdin>: warning: Unexpected \"@\"\n")
Expand Down

0 comments on commit 7d630eb

Please sign in to comment.