From 85fd2b185eaff3b9cd47a5dbcb49c8e661586de7 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Tue, 3 Jan 2023 20:48:54 -0500 Subject: [PATCH] more error text for #2790 --- internal/bundler/bundler.go | 21 +++++++++++++++------ internal/bundler_tests/bundler_css_test.go | 8 ++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/internal/bundler/bundler.go b/internal/bundler/bundler.go index 15979afb47e..6146a6d789d 100644 --- a/internal/bundler/bundler.go +++ b/internal/bundler/bundler.go @@ -1938,8 +1938,11 @@ func (s *scanner) processScannedFiles(entryPointMeta []graph.EntryPoint) []scann case ast.ImportAt, ast.ImportAtConditional: // Using a JavaScript file with CSS "@import" is not allowed if _, ok := otherFile.inputFile.Repr.(*graph.JSRepr); ok && otherFile.inputFile.Loader != config.LoaderEmpty { - s.log.AddError(&tracker, record.Range, - fmt.Sprintf("Cannot import %q into a CSS file", otherFile.inputFile.Source.PrettyPath)) + s.log.AddErrorWithNotes(&tracker, record.Range, + fmt.Sprintf("Cannot import %q into a CSS file", otherFile.inputFile.Source.PrettyPath), + []logger.MsgData{{Text: fmt.Sprintf( + "An \"@import\" rule can only be used to import another CSS file, and %q is not a CSS file (it was loaded with the %q loader).", + otherFile.inputFile.Source.PrettyPath, config.LoaderToString[otherFile.inputFile.Loader])}}) } else if record.Kind == ast.ImportAtConditional { s.log.AddError(&tracker, record.Range, "Bundling with conditional \"@import\" rules is not currently supported") @@ -1949,13 +1952,19 @@ func (s *scanner) processScannedFiles(entryPointMeta []graph.EntryPoint) []scann // Using a JavaScript or CSS file with CSS "url()" is not allowed switch otherRepr := otherFile.inputFile.Repr.(type) { case *graph.CSSRepr: - s.log.AddError(&tracker, record.Range, - fmt.Sprintf("Cannot use %q as a URL", otherFile.inputFile.Source.PrettyPath)) + s.log.AddErrorWithNotes(&tracker, record.Range, + fmt.Sprintf("Cannot use %q as a URL", otherFile.inputFile.Source.PrettyPath), + []logger.MsgData{{Text: fmt.Sprintf( + "You can't use a \"url()\" token to reference a CSS file, and %q is a CSS file (it was loaded with the %q loader).", + otherFile.inputFile.Source.PrettyPath, config.LoaderToString[otherFile.inputFile.Loader])}}) case *graph.JSRepr: if otherRepr.AST.URLForCSS == "" && otherFile.inputFile.Loader != config.LoaderEmpty { - s.log.AddError(&tracker, record.Range, - fmt.Sprintf("Cannot use %q as a URL", otherFile.inputFile.Source.PrettyPath)) + s.log.AddErrorWithNotes(&tracker, record.Range, + fmt.Sprintf("Cannot use %q as a URL", otherFile.inputFile.Source.PrettyPath), + []logger.MsgData{{Text: fmt.Sprintf( + "You can't use a \"url()\" token to reference the file %q because it was loaded with the %q loader, which doesn't provide a URL to embed in the resulting CSS.", + otherFile.inputFile.Source.PrettyPath, config.LoaderToString[otherFile.inputFile.Loader])}}) } } } diff --git a/internal/bundler_tests/bundler_css_test.go b/internal/bundler_tests/bundler_css_test.go index c7f41bc4e3e..2080ebb0331 100644 --- a/internal/bundler_tests/bundler_css_test.go +++ b/internal/bundler_tests/bundler_css_test.go @@ -233,6 +233,7 @@ func TestImportJSFromCSS(t *testing.T) { AbsOutputDir: "/out", }, expectedScanLog: `entry.css: ERROR: Cannot import "entry.js" into a CSS file +NOTE: An "@import" rule can only be used to import another CSS file, and "entry.js" is not a CSS file (it was loaded with the "js" loader). `, }) } @@ -253,6 +254,7 @@ func TestImportJSONFromCSS(t *testing.T) { AbsOutputDir: "/out", }, expectedScanLog: `entry.css: ERROR: Cannot import "entry.json" into a CSS file +NOTE: An "@import" rule can only be used to import another CSS file, and "entry.json" is not a CSS file (it was loaded with the "json" loader). `, }) } @@ -332,11 +334,17 @@ func TestInvalidImportURLInCSS(t *testing.T) { AbsOutputDir: "/out", }, expectedScanLog: `entry.css: ERROR: Cannot use "js.js" as a URL +NOTE: You can't use a "url()" token to reference the file "js.js" because it was loaded with the "js" loader, which doesn't provide a URL to embed in the resulting CSS. entry.css: ERROR: Cannot use "jsx.jsx" as a URL +NOTE: You can't use a "url()" token to reference the file "jsx.jsx" because it was loaded with the "jsx" loader, which doesn't provide a URL to embed in the resulting CSS. entry.css: ERROR: Cannot use "ts.ts" as a URL +NOTE: You can't use a "url()" token to reference the file "ts.ts" because it was loaded with the "ts" loader, which doesn't provide a URL to embed in the resulting CSS. entry.css: ERROR: Cannot use "tsx.tsx" as a URL +NOTE: You can't use a "url()" token to reference the file "tsx.tsx" because it was loaded with the "tsx" loader, which doesn't provide a URL to embed in the resulting CSS. entry.css: ERROR: Cannot use "json.json" as a URL +NOTE: You can't use a "url()" token to reference the file "json.json" because it was loaded with the "json" loader, which doesn't provide a URL to embed in the resulting CSS. entry.css: ERROR: Cannot use "css.css" as a URL +NOTE: You can't use a "url()" token to reference a CSS file, and "css.css" is a CSS file (it was loaded with the "css" loader). `, }) }