return nil, err
}
- sm.Sources = fixSourceMapSources(sm.Sources, resolve)
+ hasSourcesContent := len(sm.SourcesContent) == len(sm.Sources)
+
+ var sources []string
+ var sourcesContent []string
+ for i, src := range sm.Sources {
+ if resolved := resolve(src); resolved != "" {
+ // Absolute filenames works fine on U*ix (tested in Chrome on MacOs), but works very poorly on Windows (again Chrome).
+ // So, convert it to a URL.
+ if u, err := paths.UrlFromFilename(resolved); err == nil {
+ sources = append(sources, u.String())
+ if hasSourcesContent {
+ sourcesContent = append(sourcesContent, sm.SourcesContent[i])
+ }
+ }
+ }
+ }
+
+ sm.Sources = sources
+ if hasSourcesContent {
+ sm.SourcesContent = sourcesContent
+ }
b, err := json.Marshal(sm)
if err != nil {
return b, nil
}
-func fixSourceMapSources(s []string, resolve func(string) string) []string {
- var result []string
- for _, src := range s {
- if s := resolve(src); s != "" {
- // Absolute filenames works fine on U*ix (tested in Chrome on MacOs), but works very poorly on Windows (again Chrome).
- // So, convert it to a URL.
- if u, err := paths.UrlFromFilename(s); err == nil {
- result = append(result, u.String())
- }
- }
- }
- return result
-}
-
// Used in tests.
func SourcesFromSourceMap(s string) []string {
var sm sourceMap
"strings"
"testing"
+ qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/htesting"
"github.com/gohugoio/hugo/hugolib"
+ "github.com/gohugoio/hugo/internal/js/esbuild"
)
func TestCSSBuild(t *testing.T) {
article { background: yellow; }
-- layouts/home.html --
{{ with resources.Get "css/main.css" }}
-{{ $opts := (dict "minify" false "sourceMap" "SOURCE_MAP" "sourcesContent" SOURCES_CONTENT "loaders" (dict ".png" "dataurl"))}}
+{{ $opts := (dict "minify" MINIFY "sourceMap" "SOURCE_MAP" "sourcesContent" SOURCES_CONTENT "loaders" (dict ".png" "dataurl"))}}
{{ with . | css.Build $opts }}
<link rel="stylesheet" href="{{ .RelPermalink }}" />
{{ end }}
{{ end }}
`
- r := strings.NewReplacer(
- "SOURCE_MAP", "linked",
- "SOURCES_CONTENT", "true",
+ var (
+ r *strings.Replacer
+ files string
+ b *hugolib.IntegrationTestBuilder
)
- files := r.Replace(filesTemplate)
+ for _, minify := range []string{"true", "false"} {
- b := hugolib.Test(t, files, hugolib.TestOptOsFs())
- b.AssertFileContent("public/css/main.css", "/*# sourceMappingURL=main.css.map */")
- b.AssertFileContent("public/css/main.css.map",
- `"sourcesContent":["p { background: red; }"`,
- "AAAA;AAAI,cAAY;AAAK",
- )
+ r = strings.NewReplacer(
+ "SOURCE_MAP", "linked",
+ "SOURCES_CONTENT", "true",
+ "MINIFY", minify,
+ )
+
+ files = r.Replace(filesTemplate)
+
+ b = hugolib.Test(t, files, hugolib.TestOptOsFs())
+ b.AssertFileContent("public/css/main.css", "/*# sourceMappingURL=main.css.map */")
+ b.AssertFileContent("public/css/main.css.map",
+ `"sourcesContent":["`,
+ `"mappings":"`,
+ )
+
+ sources := esbuild.SourcesFromSourceMap(b.FileContent("public/css/main.css.map"))
+ // main.css + foo.css + bar.css + baz.css = 4 sources.
+ b.Assert(len(sources), qt.Equals, 4)
+ }
r = strings.NewReplacer(
"SOURCE_MAP", "external",
"SOURCES_CONTENT", "true",
+ "MINIFY", "false",
)
files = r.Replace(filesTemplate)
r = strings.NewReplacer(
"SOURCE_MAP", "external",
"SOURCES_CONTENT", "false",
+ "MINIFY", "false",
)
files = r.Replace(filesTemplate)
r = strings.NewReplacer(
"SOURCE_MAP", "inline",
"SOURCES_CONTENT", "false",
+ "MINIFY", "false",
)
files = r.Replace(filesTemplate)