Separate minify task
This is slower, but ensures we have accurate file size on the UI.
This commit is contained in:
parent
f77dd38704
commit
0556b2592c
|
@ -26,7 +26,7 @@ build:site:
|
|||
- cp -r src/* "$SRC_DIR"/
|
||||
script:
|
||||
- templ generate
|
||||
- go run . -gen "$SRC_DIR"
|
||||
- go run . -minify -gen "$SRC_DIR"
|
||||
artifacts:
|
||||
expire_in: 1 hour
|
||||
paths:
|
||||
|
|
|
@ -87,7 +87,7 @@
|
|||
templ generate
|
||||
'';
|
||||
buildPhase = ''
|
||||
go run . -gen src
|
||||
go run . -minify -gen src
|
||||
'';
|
||||
installPhase = ''
|
||||
mkdir -p "$out"
|
||||
|
|
82
main.go
82
main.go
|
@ -10,6 +10,7 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
@ -63,11 +64,31 @@ func minifyInPlace(mediatype, path string) error {
|
|||
return minifier.Minify(mediatype, outputFile, inputFile)
|
||||
}
|
||||
|
||||
func traverse(prefix, path string, d fs.DirEntry) error {
|
||||
trimedPath := strings.TrimPrefix(path, prefix+"/")
|
||||
func genIndexFile(path string, c templ.Component) error {
|
||||
indexFile, err := os.Create(filepath.Join(path, "index.html"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer closeFile(indexFile)
|
||||
|
||||
// Generate index.html file if we hit a directory
|
||||
buf := new(bytes.Buffer)
|
||||
if err := c.Render(ctx, buf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return minifier.Minify("text/html", indexFile, buf)
|
||||
}
|
||||
|
||||
func traverse(prefix, path string, d fs.DirEntry) error {
|
||||
// Generate home page for the root directory.
|
||||
if prefix == path {
|
||||
logger.Info("create index.html for root directory")
|
||||
return genIndexFile(path, indexPage())
|
||||
}
|
||||
|
||||
// Otherwise generate a listing page
|
||||
if d.IsDir() {
|
||||
trimedPath := strings.TrimPrefix(strings.TrimPrefix(path, prefix), "/")
|
||||
logger.Info("create index.html", "dir", trimedPath)
|
||||
|
||||
entries, err := os.ReadDir(path)
|
||||
|
@ -75,24 +96,7 @@ func traverse(prefix, path string, d fs.DirEntry) error {
|
|||
return err
|
||||
}
|
||||
|
||||
indexFile, err := os.Create(filepath.Join(path, "index.html"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer closeFile(indexFile)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if err := listingPage(trimedPath, entries).Render(ctx, buf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return minifier.Minify("text/html", indexFile, buf)
|
||||
}
|
||||
|
||||
// Otherwise minify it, if it is a CSS file
|
||||
if d.Type().IsRegular() && filepath.Ext(path) == ".css" {
|
||||
logger.Info("minify CSS", "file", trimedPath)
|
||||
return minifyInPlace("text/css", path)
|
||||
return genIndexFile(path, listingPage(trimedPath, entries))
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -128,6 +132,7 @@ func preview(root, port string) error {
|
|||
func main() {
|
||||
genFlag := flag.Bool("gen", false, "Generate index files")
|
||||
previewFlag := flag.Bool("preview", false, "Preview the built website")
|
||||
minifyFlag := flag.Bool("minify", false, "Minify existing CSS files")
|
||||
flag.Usage = func() {
|
||||
fmt.Printf(`Usage: go run . [OPTIONS] DIR
|
||||
|
||||
|
@ -185,31 +190,30 @@ Options:
|
|||
logger.Fatal("unable to get root directory", "err", err)
|
||||
}
|
||||
|
||||
if *genFlag {
|
||||
// Run minification first, to ensure file size is calculated correctly later
|
||||
if *minifyFlag {
|
||||
err = filepath.WalkDir(rootDir, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Generate the home page for the root directory.
|
||||
// NOTE: there isn't anything else in the root directory, so don't check each entry for minification
|
||||
if rootDir == path {
|
||||
logger.Info("create index.html for root directory")
|
||||
indexFile, err := os.Create(filepath.Join(path, "index.html"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer closeFile(indexFile)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
if err := indexPage().Render(ctx, buf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return minifier.Minify("text/html", indexFile, buf)
|
||||
if d.Type().IsRegular() && filepath.Ext(path) == ".css" {
|
||||
logger.Info("minify CSS", "file", strings.TrimPrefix(path, rootDir+"/"))
|
||||
return minifyInPlace("text/css", path)
|
||||
}
|
||||
|
||||
// Otherwise generate listing pages and minify CSS files
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
logger.Fatal("failed minifying CSS files", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
if *genFlag {
|
||||
err = filepath.WalkDir(rootDir, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return traverse(rootDir, path, d)
|
||||
})
|
||||
if err != nil {
|
||||
|
|
|
@ -21,7 +21,7 @@ templ pageTemplate(path string) {
|
|||
<link rel="icon" type="image/x-icon" href="/favicon.ico"/>
|
||||
<link rel="canonical" href={ "https://cdn.folliehiyuki.com/" + path }/>
|
||||
<link rel="stylesheet" href="/styles/normalize.css" integrity="sha256-Atknw9eu6T9FV6v//wFp8skKdJ5JcAqANQ1bPykR4go=" crossorigin="anonymous"/>
|
||||
<link rel="stylesheet" href="/styles/self.css" integrity="sha256-c991ejabvQnXqaa6D9clbr+4rzY5fM7DmNHXvMBN4Dw=" crossorigin="anonymous"/>
|
||||
<link rel="stylesheet" href="/styles/self.css" integrity="sha256-T3aZjYlhGVVQtRtuoRVDQryPntqVvabgIMEiW9z+Nww=" crossorigin="anonymous"/>
|
||||
<link rel="stylesheet" href="/fonts/iosevka/iosevka-aile.css" integrity="sha256-20HRMpRlRW2+dk9R7asoOl5/z8Xyc2BbjeLZsButUww=" crossorigin="anonymous"/>
|
||||
<link rel="stylesheet" href="/fonts/font-awesome/solid.css" integrity="sha256-xuw8YIIudFiLKxnOSDufxt0N2CFAJQXsK2lnTUPbofE=" crossorigin="anonymous"/>
|
||||
<link rel="preload" href="/fonts/iosevka/iosevka-aile-bold.woff2" as="font" type="font/woff2" integrity="sha256-Hu4sqJ4m0rjWqqKYxhGKw1Lqz/VpftqKe0CeHACkSto=" crossorigin="anonymous"/>
|
||||
|
@ -58,10 +58,11 @@ templ indexPage() {
|
|||
|
||||
templ listingPage(path string, entries []fs.DirEntry) {
|
||||
@pageTemplate(path) {
|
||||
<h1 style="font-size:1.25rem;">
|
||||
<h1>Directory listing</h1>
|
||||
<h2>
|
||||
<a href="/" class="fa-solid"></a>
|
||||
@splitPathIntoLinks(strings.Split(path, "/"))
|
||||
</h1>
|
||||
</h2>
|
||||
<div>
|
||||
<!-- List everything except index.html files -->
|
||||
for _, entry := range entries {
|
||||
|
|
|
@ -47,11 +47,11 @@ a:hover {
|
|||
border-bottom: 0.1rem solid var(--color-cyan);
|
||||
}
|
||||
|
||||
h1 > a {
|
||||
h2 > a {
|
||||
color: var(--color-fg);
|
||||
}
|
||||
|
||||
h1 > a:hover {
|
||||
h2 > a:hover {
|
||||
color: var(--color-fg);
|
||||
border: none;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue