internal/static: bundle and minifiy css files during static build
The last hurdle to minifying out CSS was the legacy stylesheets in
static/legacy. Now that they are removed we can bundle and minify
stylesheets for each page in a <page>.min.css file.
Change-Id: I0f26458cc85f9381f8bd8a0e225df79268594d0f
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/344399
Trust: Jamal Carvalho <jamal@golang.org>
Run-TryBot: Jamal Carvalho <jamal@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
diff --git a/internal/static/static.go b/internal/static/static.go
index 0a0c6f0..9068561 100644
--- a/internal/static/static.go
+++ b/internal/static/static.go
@@ -37,15 +37,22 @@
// devtools/cmd/static/main.go with Watch=false for building
// productionized assets.
func Build(config Config) (*api.BuildResult, error) {
- files, err := getEntry(config.EntryPoint)
+ files, err := getEntry(config.EntryPoint, config.Bundle)
if err != nil {
return nil, err
}
options := api.BuildOptions{
- EntryPoints: files,
- Bundle: config.Bundle,
- Outdir: config.EntryPoint,
- Write: true,
+ EntryPoints: files,
+ Bundle: config.Bundle,
+ Outdir: config.EntryPoint,
+ Write: true,
+ OutExtensions: map[string]string{".css": ".min.css"},
+ External: []string{"*.svg"},
+ Banner: map[string]string{"css": "/*!\n" +
+ " * Copyright 2021 The Go Authors. All rights reserved.\n" +
+ " * Use of this source code is governed by a BSD-style\n" +
+ " * license that can be found in the LICENSE file.\n" +
+ " */"},
}
if config.Watch {
options.Sourcemap = api.SourceMapInline
@@ -70,7 +77,7 @@
// for esbuild. It ignores test files and files prefixed with an underscore.
// Underscore prefixed files are assumed to be imported by and bundled together
// with the output of an entry file.
-func getEntry(dir string) ([]string, error) {
+func getEntry(dir string, bundle bool) ([]string, error) {
var matches []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
@@ -82,11 +89,9 @@
basePath := filepath.Base(path)
notPartial := !strings.HasPrefix(basePath, "_")
notTest := !strings.HasSuffix(basePath, ".test.ts")
- matched, err := filepath.Match("*.ts", basePath)
- if err != nil {
- return err
- }
- if notPartial && notTest && matched {
+ isTS := strings.HasSuffix(basePath, ".ts")
+ isCSS := strings.HasSuffix(basePath, ".css") && !strings.HasSuffix(basePath, ".min.css")
+ if notPartial && notTest && (isTS || (bundle && isCSS)) {
matches = append(matches, path)
}
return nil