js: Misc fixes

* Fix resolve of package.json deps in submodules
* Fix directory logic for writing assets/jsconfig.json

Fixes #7924
Fixes #7923
This commit is contained in:
Bjørn Erik Pedersen
2020-11-04 16:13:37 +01:00
parent cf6131dc18
commit bf2837a314
6 changed files with 144 additions and 65 deletions

View File

@@ -18,14 +18,12 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/gohugoio/hugo/hugofs"
"github.com/spf13/afero"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/hugolib/filesystems"
@@ -79,10 +77,9 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
return err
}
sdir, _ := path.Split(ctx.SourcePath)
opts.sourcefile = ctx.SourcePath
opts.resolveDir = t.c.sfs.RealFilename(sdir)
opts.workDir = t.c.rs.WorkingDir
opts.resolveDir = opts.workDir
opts.contents = string(src)
opts.mediaType = ctx.InMediaType
@@ -99,39 +96,54 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
result := api.Build(buildOptions)
if len(result.Errors) > 0 {
first := result.Errors[0]
loc := first.Location
path := loc.File
var err error
var f afero.File
var filename string
createErr := func(msg api.Message) error {
loc := msg.Location
path := loc.File
var (
f afero.File
err error
)
if strings.HasPrefix(path, nsImportHugo) {
path = strings.TrimPrefix(path, nsImportHugo+":")
f, err = hugofs.Os.Open(path)
} else {
var fi os.FileInfo
fi, err = t.c.sfs.Fs.Stat(path)
if err == nil {
m := fi.(hugofs.FileMetaInfo).Meta()
path = m.Filename()
f, err = m.Open()
}
}
if !strings.HasPrefix(path, "..") {
// Try first in the assets fs
var fi os.FileInfo
fi, err = t.c.rs.BaseFs.Assets.Fs.Stat(path)
if err == nil {
m := fi.(hugofs.FileMetaInfo).Meta()
filename = m.Filename()
f, err = m.Open()
fe := herrors.NewFileError("js", 0, loc.Line, loc.Column, errors.New(msg.Text))
err, _ := herrors.WithFileContext(fe, path, f, herrors.SimpleLineMatcher)
f.Close()
return err
}
return fmt.Errorf("%s", msg.Text)
}
var errors []error
for _, msg := range result.Errors {
errors = append(errors, createErr(msg))
}
// Return 1, log the rest.
for i, err := range errors {
if i > 0 {
t.c.rs.Logger.Errorf("js.Build failed: %s", err)
}
}
if f == nil {
path = filepath.Join(t.c.rs.WorkingDir, path)
filename = path
f, err = t.c.rs.Fs.Os.Open(path)
}
if err == nil {
fe := herrors.NewFileError("js", 0, loc.Line, loc.Column, errors.New(first.Text))
err, _ := herrors.WithFileContext(fe, filename, f, herrors.SimpleLineMatcher)
f.Close()
return err
}
return fmt.Errorf("%s", result.Errors[0].Text)
return errors[0]
}
ctx.To.Write(result.OutputFiles[0].Contents)