mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
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
This commit is contained in:
parent
5e03f644a4
commit
78f227b664
6 changed files with 26 additions and 46 deletions
|
@ -79,9 +79,9 @@ func (s *staticSyncer) syncsStaticEvents(staticEvents []fsnotify.Event) error {
|
||||||
|
|
||||||
fromPath := ev.Name
|
fromPath := ev.Name
|
||||||
|
|
||||||
relPath := sourceFs.MakePathRelative(fromPath)
|
relPath, found := sourceFs.MakePathRelative(fromPath)
|
||||||
|
|
||||||
if relPath == "" {
|
if !found {
|
||||||
// Not member of this virtual host.
|
// Not member of this virtual host.
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
// It will return an empty string if the filename is not a member of a static filesystem.
|
||||||
func (s SourceFilesystems) MakeStaticPathRelative(filename string) string {
|
func (s SourceFilesystems) MakeStaticPathRelative(filename string) string {
|
||||||
for _, staticFs := range s.Static {
|
for _, staticFs := range s.Static {
|
||||||
rel := staticFs.MakePathRelative(filename)
|
rel, _ := staticFs.MakePathRelative(filename)
|
||||||
if rel != "" {
|
if rel != "" {
|
||||||
return rel
|
return rel
|
||||||
}
|
}
|
||||||
|
@ -276,8 +276,7 @@ func (s SourceFilesystems) MakeStaticPathRelative(filename string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakePathRelative creates a relative path from the given filename.
|
// 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, bool) {
|
||||||
func (d *SourceFilesystem) MakePathRelative(filename string) string {
|
|
||||||
|
|
||||||
for _, dir := range d.Dirs {
|
for _, dir := range d.Dirs {
|
||||||
meta := dir.(hugofs.FileMetaInfo).Meta()
|
meta := dir.(hugofs.FileMetaInfo).Meta()
|
||||||
|
@ -288,10 +287,10 @@ func (d *SourceFilesystem) MakePathRelative(filename string) string {
|
||||||
if mp := meta.Path(); mp != "" {
|
if mp := meta.Path(); mp != "" {
|
||||||
rel = filepath.Join(mp, rel)
|
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 {
|
func (d *SourceFilesystem) RealFilename(rel string) string {
|
||||||
|
|
|
@ -394,9 +394,14 @@ func TestMakePathRelative(t *testing.T) {
|
||||||
sfs := bfs.Static[""]
|
sfs := bfs.Static[""]
|
||||||
c.Assert(sfs, qt.Not(qt.IsNil))
|
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"))
|
makeRel := func(s string) string {
|
||||||
c.Assert(sfs.MakePathRelative(filepath.Join(workDir, "static", "d2", "foo.txt")), qt.Equals, filepath.FromSlash("d2/foo.txt"))
|
r, _ := sfs.MakePathRelative(s)
|
||||||
c.Assert(sfs.MakePathRelative(filepath.Join(workDir, "dust", "d3", "foo.txt")), qt.Equals, filepath.FromSlash("foo/bar/d3/foo.txt"))
|
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"))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1047,7 +1047,7 @@ func (s *Site) processPartial(config *BuildCfg, init func(config *BuildCfg) erro
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, ev := range events {
|
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)...)
|
cachePartitions = append(cachePartitions, resources.ResourceKeyPartitions(assetsFilename)...)
|
||||||
if evictCSSRe == nil {
|
if evictCSSRe == nil {
|
||||||
if cssFileRe.MatchString(assetsFilename) || cssConfigRe.MatchString(assetsFilename) {
|
if cssFileRe.MatchString(assetsFilename) || cssConfigRe.MatchString(assetsFilename) {
|
||||||
|
|
|
@ -19,7 +19,6 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
@ -113,11 +112,6 @@ func decodeOptions(m map[string]interface{}) (Options, error) {
|
||||||
return opts, nil
|
return opts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type importCache struct {
|
|
||||||
sync.RWMutex
|
|
||||||
m map[string]api.OnResolveResult
|
|
||||||
}
|
|
||||||
|
|
||||||
var extensionToLoaderMap = map[string]api.Loader{
|
var extensionToLoaderMap = map[string]api.Loader{
|
||||||
".js": api.LoaderJS,
|
".js": api.LoaderJS,
|
||||||
".mjs": api.LoaderJS,
|
".mjs": api.LoaderJS,
|
||||||
|
@ -141,16 +135,18 @@ func loaderFromFilename(filename string) api.Loader {
|
||||||
func createBuildPlugins(c *Client, opts Options) ([]api.Plugin, error) {
|
func createBuildPlugins(c *Client, opts Options) ([]api.Plugin, error) {
|
||||||
fs := c.rs.Assets
|
fs := c.rs.Assets
|
||||||
|
|
||||||
cache := importCache{
|
|
||||||
m: make(map[string]api.OnResolveResult),
|
|
||||||
}
|
|
||||||
|
|
||||||
resolveImport := func(args api.OnResolveArgs) (api.OnResolveResult, error) {
|
resolveImport := func(args api.OnResolveArgs) (api.OnResolveResult, error) {
|
||||||
|
|
||||||
isStdin := args.Importer == stdinImporter
|
isStdin := args.Importer == stdinImporter
|
||||||
var relDir string
|
var relDir string
|
||||||
if !isStdin {
|
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 {
|
} else {
|
||||||
relDir = filepath.Dir(opts.sourcefile)
|
relDir = filepath.Dir(opts.sourcefile)
|
||||||
}
|
}
|
||||||
|
@ -204,8 +200,7 @@ func createBuildPlugins(c *Client, opts Options) ([]api.Plugin, error) {
|
||||||
return api.OnResolveResult{Path: m.Filename(), Namespace: nsImportHugo}, nil
|
return api.OnResolveResult{Path: m.Filename(), Namespace: nsImportHugo}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not found in /assets. Probably in node_modules. ESBuild will handle that
|
// Fall back to ESBuild's resolve.
|
||||||
// rather complex logic.
|
|
||||||
return api.OnResolveResult{}, nil
|
return api.OnResolveResult{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,26 +209,7 @@ func createBuildPlugins(c *Client, opts Options) ([]api.Plugin, error) {
|
||||||
Setup: func(build api.PluginBuild) {
|
Setup: func(build api.PluginBuild) {
|
||||||
build.OnResolve(api.OnResolveOptions{Filter: `.*`},
|
build.OnResolve(api.OnResolveOptions{Filter: `.*`},
|
||||||
func(args api.OnResolveArgs) (api.OnResolveResult, error) {
|
func(args api.OnResolveArgs) (api.OnResolveResult, error) {
|
||||||
// Try cache first.
|
return resolveImport(args)
|
||||||
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
|
|
||||||
|
|
||||||
})
|
})
|
||||||
build.OnLoad(api.OnLoadOptions{Filter: `.*`, Namespace: nsImportHugo},
|
build.OnLoad(api.OnLoadOptions{Filter: `.*`, Namespace: nsImportHugo},
|
||||||
|
|
|
@ -76,7 +76,7 @@ func (t *toCSSTransformation) Transform(ctx *resources.ResourceTransformationCtx
|
||||||
if prev == "stdin" {
|
if prev == "stdin" {
|
||||||
prevDir = baseDir
|
prevDir = baseDir
|
||||||
} else {
|
} else {
|
||||||
prevDir = t.c.sfs.MakePathRelative(filepath.Dir(prev))
|
prevDir, _ = t.c.sfs.MakePathRelative(filepath.Dir(prev))
|
||||||
|
|
||||||
if prevDir == "" {
|
if prevDir == "" {
|
||||||
// Not a member of this filesystem. Let LibSASS handle it.
|
// Not a member of this filesystem. Let LibSASS handle it.
|
||||||
|
|
Loading…
Reference in a new issue