Add js.Batch

Fixes #12626
Closes #7499
Closes #9978
Closes #12879
Closes #13113
Fixes #13116
This commit is contained in:
Bjørn Erik Pedersen
2024-12-10 16:22:08 +01:00
parent 157d86414d
commit e293e7ca6d
61 changed files with 4520 additions and 1003 deletions

View File

@@ -41,7 +41,7 @@ func New(d *deps.Deps) *Namespace {
l := d.Log.InfoCommand("timer")
d.BuildEndListeners.Add(func() {
d.BuildEndListeners.Add(func(...any) bool {
type data struct {
Name string
Count int
@@ -84,6 +84,8 @@ func New(d *deps.Deps) *Namespace {
}
ns.timers = make(map[string][]*timer)
return false
})
return ns

View File

@@ -30,8 +30,9 @@ func New(d *deps.Deps) *Namespace {
logger: d.Log,
}
d.BuildStartListeners.Add(func() {
d.BuildStartListeners.Add(func(...any) bool {
ns.logger.Reset()
return false
})
return ns

View File

@@ -24,7 +24,10 @@ const name = "js"
func init() {
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
ctx := New(d)
ctx, err := New(d)
if err != nil {
panic(err)
}
ns := &internal.TemplateFuncsNamespace{
Name: name,

View File

@@ -1,4 +1,4 @@
// Copyright 2020 The Hugo Authors. All rights reserved.
// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -17,29 +17,47 @@ package js
import (
"errors"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/internal/js/esbuild"
"github.com/gohugoio/hugo/resources"
"github.com/gohugoio/hugo/resources/resource"
"github.com/gohugoio/hugo/resources/resource_factories/create"
"github.com/gohugoio/hugo/resources/resource_transformers/babel"
"github.com/gohugoio/hugo/resources/resource_transformers/js"
jstransform "github.com/gohugoio/hugo/resources/resource_transformers/js"
"github.com/gohugoio/hugo/tpl/internal/resourcehelpers"
)
// New returns a new instance of the js-namespaced template functions.
func New(deps *deps.Deps) *Namespace {
if deps.ResourceSpec == nil {
return &Namespace{}
func New(d *deps.Deps) (*Namespace, error) {
if d.ResourceSpec == nil {
return &Namespace{}, nil
}
batcherClient, err := esbuild.NewBatcherClient(d)
if err != nil {
return nil, err
}
return &Namespace{
client: js.New(deps.BaseFs.Assets, deps.ResourceSpec),
babelClient: babel.New(deps.ResourceSpec),
}
d: d,
jsTransformClient: jstransform.New(d.BaseFs.Assets, d.ResourceSpec),
jsBatcherClient: batcherClient,
jsBatcherStore: maps.NewCache[string, esbuild.Batcher](),
createClient: create.New(d.ResourceSpec),
babelClient: babel.New(d.ResourceSpec),
}, nil
}
// Namespace provides template functions for the "js" namespace.
type Namespace struct {
client *js.Client
babelClient *babel.Client
d *deps.Deps
jsTransformClient *jstransform.Client
createClient *create.Client
babelClient *babel.Client
jsBatcherClient *esbuild.BatcherClient
jsBatcherStore *maps.Cache[string, esbuild.Batcher]
}
// Build processes the given Resource with ESBuild.
@@ -65,7 +83,24 @@ func (ns *Namespace) Build(args ...any) (resource.Resource, error) {
m = map[string]any{"targetPath": targetPath}
}
return ns.client.Process(r, m)
return ns.jsTransformClient.Process(r, m)
}
// Batch creates a new Batcher with the given ID.
// Repeated calls with the same ID will return the same Batcher.
// The ID will be used to name the root directory of the batch.
// Forward slashes in the ID is allowed.
func (ns *Namespace) Batch(id string) (esbuild.Batcher, error) {
if err := esbuild.ValidateBatchID(id, true); err != nil {
return nil, err
}
b, err := ns.jsBatcherStore.GetOrCreate(id, func() (esbuild.Batcher, error) {
return ns.jsBatcherClient.New(id)
})
if err != nil {
return nil, err
}
return b, nil
}
// Babel processes the given Resource with Babel.

View File

@@ -81,8 +81,9 @@ func New(deps *deps.Deps) *Namespace {
cache := &partialCache{cache: lru}
deps.BuildStartListeners.Add(
func() {
func(...any) bool {
cache.clear()
return false
})
return &Namespace{

View File

@@ -0,0 +1,16 @@
{{ range $i, $e := .Scripts -}}
{{ printf "import { %s as Script%d } from %q;" .Export $i .Import }}
{{ end -}}
{{ range $i, $e := .Runners }}
{{ printf "import { %s as Run%d } from %q;" .Export $i .Import }}
{{ end }}
{{/* */}}
let scripts = [];
{{ range $i, $e := .Scripts -}}
scripts.push({{ .RunnerJSON $i }});
{{ end -}}
{{/* */}}
{{ range $i, $e := .Runners }}
{{ $id := printf "Run%d" $i }}
{{ $id }}(scripts);
{{ end }}

View File

@@ -695,13 +695,13 @@ func (t *templateHandler) applyBaseTemplate(overlay, base templateInfo) (tpl.Tem
if !base.IsZero() {
templ, err = templ.Parse(base.template)
if err != nil {
return nil, base.errWithFileContext("parse failed", err)
return nil, base.errWithFileContext("text: base: parse failed", err)
}
}
templ, err = texttemplate.Must(templ.Clone()).Parse(overlay.template)
if err != nil {
return nil, overlay.errWithFileContext("parse failed", err)
return nil, overlay.errWithFileContext("text: overlay: parse failed", err)
}
// The extra lookup is a workaround, see
@@ -720,13 +720,13 @@ func (t *templateHandler) applyBaseTemplate(overlay, base templateInfo) (tpl.Tem
if !base.IsZero() {
templ, err = templ.Parse(base.template)
if err != nil {
return nil, base.errWithFileContext("parse failed", err)
return nil, base.errWithFileContext("html: base: parse failed", err)
}
}
templ, err = htmltemplate.Must(templ.Clone()).Parse(overlay.template)
if err != nil {
return nil, overlay.errWithFileContext("parse failed", err)
return nil, overlay.errWithFileContext("html: overlay: parse failed", err)
}
// The extra lookup is a workaround, see

View File

@@ -251,6 +251,9 @@ func newTemplateExecuter(d *deps.Deps) (texttemplate.Executer, map[string]reflec
}
func createFuncMap(d *deps.Deps) map[string]any {
if d.TmplFuncMap != nil {
return d.TmplFuncMap
}
funcMap := template.FuncMap{}
nsMap := make(map[string]any)
@@ -292,5 +295,7 @@ func createFuncMap(d *deps.Deps) map[string]any {
}
}
return funcMap
d.TmplFuncMap = funcMap
return d.TmplFuncMap
}