mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-17 21:01:26 +02:00
Add js.Batch
Fixes #12626 Closes #7499 Closes #9978 Closes #12879 Closes #13113 Fixes #13116
This commit is contained in:
57
tpl/js/js.go
57
tpl/js/js.go
@@ -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.
|
||||
|
Reference in New Issue
Block a user