Skip to content

Commit

Permalink
js: Let ESBuild handle all impports from node_modules
Browse files Browse the repository at this point in the history
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 gohugoio#7948
  • Loading branch information
bep committed Nov 12, 2020
1 parent 5e03f64 commit 04e532a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 42 deletions.
4 changes: 2 additions & 2 deletions commands/static_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ func (s *staticSyncer) syncsStaticEvents(staticEvents []fsnotify.Event) error {

fromPath := ev.Name

relPath := sourceFs.MakePathRelative(fromPath)
relPath, found := sourceFs.MakePathRelative(fromPath)

if relPath == "" {
if !found {
// Not member of this virtual host.
continue
}
Expand Down
9 changes: 4 additions & 5 deletions hugolib/filesystems/basefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (s SourceFilesystems) IsI18n(filename string) bool {
// 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
}
Expand All @@ -276,8 +276,7 @@ func (s SourceFilesystems) MakeStaticPathRelative(filename string) string {
}

// 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()
Expand All @@ -288,10 +287,10 @@ func (d *SourceFilesystem) MakePathRelative(filename string) string {
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 {
Expand Down
2 changes: 1 addition & 1 deletion hugolib/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ func (s *Site) processPartial(config *BuildCfg, init func(config *BuildCfg) erro
)

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) {
Expand Down
44 changes: 10 additions & 34 deletions resources/resource_transformers/js/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"io/ioutil"
"path/filepath"
"strings"
"sync"

"github.com/pkg/errors"

Expand Down Expand Up @@ -113,11 +112,6 @@ func decodeOptions(m map[string]interface{}) (Options, error) {
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,
Expand All @@ -141,16 +135,18 @@ func loaderFromFilename(filename string) api.Loader {
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)
}
Expand Down Expand Up @@ -204,8 +200,7 @@ func createBuildPlugins(c *Client, opts Options) ([]api.Plugin, error) {
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
}

Expand All @@ -214,26 +209,7 @@ func createBuildPlugins(c *Client, opts Options) ([]api.Plugin, error) {
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},
Expand Down

0 comments on commit 04e532a

Please sign in to comment.