shithub: hugo

Download patch

ref: 78f227b664d86c30fbb25f7a953b7ef8f2dacf13
parent: 5e03f644a4507f51bdbcdb42b65ce4e99095374f
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Fri Nov 13 03:54:29 EST 2020

js: Let ESBuild handle all imports from node_modules

This commit fixes some issues where modules in /assets share the same name as in node_modules.

This was not intended, and with this commit the node_modules-components should be isolated. If you want to redefine something inside node_modules, use the `defines` option.

Fixes #7948

--- a/commands/static_syncer.go
+++ b/commands/static_syncer.go
@@ -79,9 +79,9 @@
 
 			fromPath := ev.Name
 
-			relPath := sourceFs.MakePathRelative(fromPath)
+			relPath, found := sourceFs.MakePathRelative(fromPath)
 
-			if relPath == "" {
+			if !found {
 				// Not member of this virtual host.
 				continue
 			}
--- a/hugolib/filesystems/basefs.go
+++ b/hugolib/filesystems/basefs.go
@@ -267,7 +267,7 @@
 // It will return an empty string if the filename is not a member of a static filesystem.
 func (s SourceFilesystems) MakeStaticPathRelative(filename string) string {
 	for _, staticFs := range s.Static {
-		rel := staticFs.MakePathRelative(filename)
+		rel, _ := staticFs.MakePathRelative(filename)
 		if rel != "" {
 			return rel
 		}
@@ -276,8 +276,7 @@
 }
 
 // MakePathRelative creates a relative path from the given filename.
-// It will return an empty string if the filename is not a member of this filesystem.
-func (d *SourceFilesystem) MakePathRelative(filename string) string {
+func (d *SourceFilesystem) MakePathRelative(filename string) (string, bool) {
 
 	for _, dir := range d.Dirs {
 		meta := dir.(hugofs.FileMetaInfo).Meta()
@@ -288,10 +287,10 @@
 			if mp := meta.Path(); mp != "" {
 				rel = filepath.Join(mp, rel)
 			}
-			return strings.TrimPrefix(rel, filePathSeparator)
+			return strings.TrimPrefix(rel, filePathSeparator), true
 		}
 	}
-	return ""
+	return "", false
 }
 
 func (d *SourceFilesystem) RealFilename(rel string) string {
--- a/hugolib/filesystems/basefs_test.go
+++ b/hugolib/filesystems/basefs_test.go
@@ -394,9 +394,14 @@
 	sfs := bfs.Static[""]
 	c.Assert(sfs, qt.Not(qt.IsNil))
 
-	c.Assert(sfs.MakePathRelative(filepath.Join(workDir, "dist", "d1", "foo.txt")), qt.Equals, filepath.FromSlash("mydist/d1/foo.txt"))
-	c.Assert(sfs.MakePathRelative(filepath.Join(workDir, "static", "d2", "foo.txt")), qt.Equals, filepath.FromSlash("d2/foo.txt"))
-	c.Assert(sfs.MakePathRelative(filepath.Join(workDir, "dust", "d3", "foo.txt")), qt.Equals, filepath.FromSlash("foo/bar/d3/foo.txt"))
+	makeRel := func(s string) string {
+		r, _ := sfs.MakePathRelative(s)
+		return r
+	}
+
+	c.Assert(makeRel(filepath.Join(workDir, "dist", "d1", "foo.txt")), qt.Equals, filepath.FromSlash("mydist/d1/foo.txt"))
+	c.Assert(makeRel(filepath.Join(workDir, "static", "d2", "foo.txt")), qt.Equals, filepath.FromSlash("d2/foo.txt"))
+	c.Assert(makeRel(filepath.Join(workDir, "dust", "d3", "foo.txt")), qt.Equals, filepath.FromSlash("foo/bar/d3/foo.txt"))
 
 }
 
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -1047,7 +1047,7 @@
 	)
 
 	for _, ev := range events {
-		if assetsFilename := s.BaseFs.Assets.MakePathRelative(ev.Name); assetsFilename != "" {
+		if assetsFilename, _ := s.BaseFs.Assets.MakePathRelative(ev.Name); assetsFilename != "" {
 			cachePartitions = append(cachePartitions, resources.ResourceKeyPartitions(assetsFilename)...)
 			if evictCSSRe == nil {
 				if cssFileRe.MatchString(assetsFilename) || cssConfigRe.MatchString(assetsFilename) {
--- a/resources/resource_transformers/js/options.go
+++ b/resources/resource_transformers/js/options.go
@@ -19,7 +19,6 @@
 	"io/ioutil"
 	"path/filepath"
 	"strings"
-	"sync"
 
 	"github.com/pkg/errors"
 
@@ -113,11 +112,6 @@
 	return opts, nil
 }
 
-type importCache struct {
-	sync.RWMutex
-	m map[string]api.OnResolveResult
-}
-
 var extensionToLoaderMap = map[string]api.Loader{
 	".js":   api.LoaderJS,
 	".mjs":  api.LoaderJS,
@@ -141,16 +135,18 @@
 func createBuildPlugins(c *Client, opts Options) ([]api.Plugin, error) {
 	fs := c.rs.Assets
 
-	cache := importCache{
-		m: make(map[string]api.OnResolveResult),
-	}
-
 	resolveImport := func(args api.OnResolveArgs) (api.OnResolveResult, error) {
-
 		isStdin := args.Importer == stdinImporter
 		var relDir string
 		if !isStdin {
-			relDir = filepath.Dir(fs.MakePathRelative(args.Importer))
+			rel, found := fs.MakePathRelative(args.Importer)
+			if !found {
+				// Not in any of the /assets folders.
+				// This is an import from a node_modules, let
+				// ESBuild resolve this.
+				return api.OnResolveResult{}, nil
+			}
+			relDir = filepath.Dir(rel)
 		} else {
 			relDir = filepath.Dir(opts.sourcefile)
 		}
@@ -204,8 +200,7 @@
 			return api.OnResolveResult{Path: m.Filename(), Namespace: nsImportHugo}, nil
 		}
 
-		// Not found in /assets. Probably in node_modules. ESBuild will handle that
-		// rather complex logic.
+		// Fall back to ESBuild's resolve.
 		return api.OnResolveResult{}, nil
 	}
 
@@ -214,26 +209,7 @@
 		Setup: func(build api.PluginBuild) {
 			build.OnResolve(api.OnResolveOptions{Filter: `.*`},
 				func(args api.OnResolveArgs) (api.OnResolveResult, error) {
-					// Try cache first.
-					cache.RLock()
-					v, found := cache.m[args.Path]
-					cache.RUnlock()
-
-					if found {
-						return v, nil
-					}
-
-					imp, err := resolveImport(args)
-					if err != nil {
-						return imp, err
-					}
-
-					cache.Lock()
-					defer cache.Unlock()
-
-					cache.m[args.Path] = imp
-
-					return imp, nil
+					return resolveImport(args)
 
 				})
 			build.OnLoad(api.OnLoadOptions{Filter: `.*`, Namespace: nsImportHugo},
--- a/resources/resource_transformers/tocss/scss/tocss.go
+++ b/resources/resource_transformers/tocss/scss/tocss.go
@@ -76,7 +76,7 @@
 		if prev == "stdin" {
 			prevDir = baseDir
 		} else {
-			prevDir = t.c.sfs.MakePathRelative(filepath.Dir(prev))
+			prevDir, _ = t.c.sfs.MakePathRelative(filepath.Dir(prev))
 
 			if prevDir == "" {
 				// Not a member of this filesystem. Let LibSASS handle it.