Skip to content

Commit

Permalink
more error text for #2790
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 4, 2023
1 parent 930f60d commit 85fd2b1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
21 changes: 15 additions & 6 deletions internal/bundler/bundler.go
Expand Up @@ -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")
Expand All @@ -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])}})
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions internal/bundler_tests/bundler_css_test.go
Expand Up @@ -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).
`,
})
}
Expand All @@ -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).
`,
})
}
Expand Down Expand Up @@ -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).
`,
})
}
Expand Down

0 comments on commit 85fd2b1

Please sign in to comment.