Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-01-22 19:07:23 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-01-23 01:43:44 +0300
commit32b86076ee1c0833b538b84e1cc9e6d79babecf2 (patch)
tree7332c7ba35372e520fe67fa011747773a1b226d5 /resources
parent241b7483ea954653512d4895ad6bacf79ee26ddc (diff)
js: Add Inject config option
Fixes #8164
Diffstat (limited to 'resources')
-rw-r--r--resources/resource_transformers/js/build.go23
-rw-r--r--resources/resource_transformers/js/options.go72
2 files changed, 65 insertions, 30 deletions
diff --git a/resources/resource_transformers/js/build.go b/resources/resource_transformers/js/build.go
index ee60aa502..bd126efda 100644
--- a/resources/resource_transformers/js/build.go
+++ b/resources/resource_transformers/js/build.go
@@ -18,6 +18,7 @@ import (
"io/ioutil"
"os"
"path"
+ "path/filepath"
"regexp"
"strings"
@@ -103,6 +104,28 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
defer os.Remove(buildOptions.Outdir)
}
+ if opts.Inject != nil {
+ // Resolve the absolute filenames.
+ for i, ext := range opts.Inject {
+ impPath := filepath.FromSlash(ext)
+ if filepath.IsAbs(impPath) {
+ return errors.Errorf("inject: absolute paths not supported, must be relative to /assets")
+ }
+
+ m := resolveComponentInAssets(t.c.rs.Assets.Fs, impPath)
+
+ if m == nil {
+ return errors.Errorf("inject: file %q not found", ext)
+ }
+
+ opts.Inject[i] = m.Filename()
+
+ }
+
+ buildOptions.Inject = opts.Inject
+
+ }
+
result := api.Build(buildOptions)
if len(result.Errors) > 0 {
diff --git a/resources/resource_transformers/js/options.go b/resources/resource_transformers/js/options.go
index a65e67ac0..921e944d4 100644
--- a/resources/resource_transformers/js/options.go
+++ b/resources/resource_transformers/js/options.go
@@ -20,6 +20,8 @@ import (
"path/filepath"
"strings"
+ "github.com/spf13/afero"
+
"github.com/pkg/errors"
"github.com/evanw/esbuild/pkg/api"
@@ -64,6 +66,11 @@ type Options struct {
// External dependencies, e.g. "react".
Externals []string
+ // This option allows you to automatically replace a global variable with an import from another file.
+ // The filenames must be relative to /assets.
+ // See https://esbuild.github.io/api/#inject
+ Inject []string
+
// User defined symbols.
Defines map[string]interface{}
@@ -137,6 +144,40 @@ func loaderFromFilename(filename string) api.Loader {
return api.LoaderJS
}
+func resolveComponentInAssets(fs afero.Fs, impPath string) hugofs.FileMeta {
+ findFirst := func(base string) hugofs.FileMeta {
+ // This is the most common sub-set of ESBuild's default extensions.
+ // We assume that imports of JSON, CSS etc. will be using their full
+ // name with extension.
+ for _, ext := range []string{".js", ".ts", ".tsx", ".jsx"} {
+ if fi, err := fs.Stat(base + ext); err == nil {
+ return fi.(hugofs.FileMetaInfo).Meta()
+ }
+ }
+
+ // Not found.
+ return nil
+ }
+
+ var m hugofs.FileMeta
+
+ // First the path as is.
+ fi, err := fs.Stat(impPath)
+
+ if err == nil {
+ if fi.IsDir() {
+ m = findFirst(filepath.Join(impPath, "index"))
+ } else {
+ m = fi.(hugofs.FileMetaInfo).Meta()
+ }
+ } else {
+ // It may be a regular file imported without an extension.
+ m = findFirst(impPath)
+ }
+
+ return m
+}
+
func createBuildPlugins(c *Client, opts Options) ([]api.Plugin, error) {
fs := c.rs.Assets
@@ -169,36 +210,7 @@ func createBuildPlugins(c *Client, opts Options) ([]api.Plugin, error) {
impPath = filepath.Join(relDir, impPath)
}
- findFirst := func(base string) hugofs.FileMeta {
- // This is the most common sub-set of ESBuild's default extensions.
- // We assume that imports of JSON, CSS etc. will be using their full
- // name with extension.
- for _, ext := range []string{".js", ".ts", ".tsx", ".jsx"} {
- if fi, err := fs.Fs.Stat(base + ext); err == nil {
- return fi.(hugofs.FileMetaInfo).Meta()
- }
- }
-
- // Not found.
- return nil
- }
-
- var m hugofs.FileMeta
-
- // First the path as is.
- fi, err := fs.Fs.Stat(impPath)
-
- if err == nil {
- if fi.IsDir() {
- m = findFirst(filepath.Join(impPath, "index"))
- } else {
- m = fi.(hugofs.FileMetaInfo).Meta()
- }
- } else {
- // It may be a regular file imported without an extension.
- m = findFirst(impPath)
- }
- //
+ m := resolveComponentInAssets(fs.Fs, impPath)
if m != nil {
// Store the source root so we can create a jsconfig.json