Skip to content

Commit

Permalink
fix(url): use content type when fetching without a file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
joelmoss committed Mar 19, 2024
1 parent 3598ea7 commit 9d2f9dd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
6 changes: 3 additions & 3 deletions internal/plugin/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ var Svg = api.Plugin{
func(args api.OnLoadArgs) (api.OnLoadResult, error) {
// pp.Println("[svg] namespace(svgFromJsx)", args)

contents, err := func() (string, error) {
contents, _, err := func() (string, string, error) {
if utils.IsUrl(args.Path) {
return DownloadURL(args.Path, true)
} else {
bytes, err := os.ReadFile(args.Path)
if err != nil {
return "", err
return "", "", err
}

return string(bytes), nil
return string(bytes), "", nil
}
}()

Expand Down
33 changes: 23 additions & 10 deletions internal/plugin/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"joelmoss/proscenium/internal/utils"
"mime"
"net/http"
"os"

Expand All @@ -28,18 +29,18 @@ var Url = esbuild.Plugin{
root := build.InitialOptions.AbsWorkingDir

// When a URL is loaded, we want to actually download the content from the internet.
// FIXME: Note that CSS is not parsed with our custom parser (ie. no CSS module, mixin support).
// Note that CSS is not parsed with our custom parser (ie. no CSS module, mixin support).
build.OnLoad(esbuild.OnLoadOptions{Filter: ".*", Namespace: "url"},
func(args esbuild.OnLoadArgs) (esbuild.OnLoadResult, error) {
// pp.Println("[5] namespace(url)", args)

contents, err := DownloadURL(args.Path, true)
contents, contentType, err := DownloadURL(args.Path, true)
if err != nil {
return esbuild.OnLoadResult{}, err
}

loader := esbuild.LoaderJS
if utils.PathIsCss(args.Path) {
if utils.PathIsCss(args.Path) || contentType == "text/css" {
loader = esbuild.LoaderCSS
} else if utils.PathIsSvg(args.Path) {
loader = esbuild.LoaderText
Expand All @@ -54,17 +55,23 @@ var Url = esbuild.Plugin{
},
}

func DownloadURL(url string, shouldCache bool) (string, error) {
func DownloadURL(url string, shouldCache bool) (string, string, error) {
if shouldCache {
cached, ok := cache.Get(url)
cachedContent, ok := cache.Get(url)
if ok {
return string(cached), nil
cachedMediaType, ok := cache.Get(fmt.Sprint("contentType|", url))
if ok {
return string(cachedContent), string(cachedMediaType), nil
} else {
return string(cachedContent), "", nil
}
}
}

result, err := http.Get(url)
if err != nil {
return "", err
errMsg := fmt.Sprintf("Fetch of %v failed: %v", url, err.Error())
return "", "", errors.New(errMsg)
}

defer result.Body.Close()
Expand All @@ -73,18 +80,24 @@ func DownloadURL(url string, shouldCache bool) (string, error) {

if result.StatusCode > 299 {
err := fmt.Sprintf("Fetch of %v failed with status code: %d", url, result.StatusCode)
return "", errors.New(err)
return "", "", errors.New(err)
}

bytes, err := io.ReadAll(r)
if err != nil {
errMsg := fmt.Sprintf("Fetch of %v failed: %v", url, err.Error())
return "", errors.New(errMsg)
return "", "", errors.New(errMsg)
}

contentType := result.Header.Get("Content-Type")
mediaType, _, err := mime.ParseMediaType(contentType)
if err == nil && shouldCache {
cache.Set(fmt.Sprint("contentType|", url), []byte(mediaType))
}

if shouldCache {
cache.Set(url, bytes)
}

return string(bytes), nil
return string(bytes), mediaType, nil
}
7 changes: 7 additions & 0 deletions test/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ var _ = Describe("Build(url)", func() {
Expect(result).To(ContainCode(`body { color: red; }`))
})

It("bundles css without file extension", func() {
gock.New("https://proscenium.test").Get("/foo").Reply(200).SetHeader("Content-Type", "text/css").BodyString("body { color: red; }")

result := Build("https%3A%2F%2Fproscenium.test%2Ffoo")
Expect(result).To(ContainCode(`body { color: red; }`))
})

It("should cache", func() {
MockURL("/foo.css", "body { color: red; }")
Expect(Build("https%3A%2F%2Fproscenium.test%2Ffoo.css")).To(ContainCode(`body { color: red; }`))
Expand Down

0 comments on commit 9d2f9dd

Please sign in to comment.