From 7d630ebbf251917cc02b1812481744f4f21f2036 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Fri, 16 Jul 2021 22:08:09 -0700 Subject: [PATCH] fix #1426: remove warning about bad CSS "@" rules --- CHANGELOG.md | 8 +++++ internal/css_parser/css_parser.go | 47 ++++++++++---------------- internal/css_parser/css_parser_test.go | 3 +- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8384ca89b20..9ac46713be4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/internal/css_parser/css_parser.go b/internal/css_parser/css_parser.go index 0a35e906993..8ce92e140e4 100644 --- a/internal/css_parser/css_parser.go +++ b/internal/css_parser/css_parser.go @@ -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") } } diff --git a/internal/css_parser/css_parser_test.go b/internal/css_parser/css_parser_test.go index e9edab80583..ac7828a07c4 100644 --- a/internal/css_parser/css_parser_test.go +++ b/internal/css_parser/css_parser_test.go @@ -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", ": warning: \"@unknown\" is not a known rule name\n: warning: Expected \"{\" but found end of file\n") + expectParseError(t, "@unknown", ": warning: Expected \"{\" but found end of file\n") expectParseError(t, "@", ": warning: Unexpected \"@\"\n") expectParseError(t, "@;", ": warning: Unexpected \"@\"\n") expectParseError(t, "@{}", ": warning: Unexpected \"@\"\n")