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

@@ -354,26 +354,31 @@ func (h *HugoSites) postProcess() error {
// Write a jsconfig.json file to the project's /asset directory
// to help JS intellisense in VS Code etc.
if !h.ResourceSpec.BuildConfig.NoJSConfigInAssets && h.BaseFs.Assets.Dirs != nil {
m := h.BaseFs.Assets.Dirs[0].Meta()
assetsDir := m.Filename()
if strings.HasPrefix(assetsDir, h.ResourceSpec.WorkingDir) {
if jsConfig := h.ResourceSpec.JSConfigBuilder.Build(assetsDir); jsConfig != nil {
fi, err := h.BaseFs.Assets.Fs.Stat("")
if err != nil {
h.Log.Warnf("Failed to resolve jsconfig.json dir: %s", err)
} else {
m := fi.(hugofs.FileMetaInfo).Meta()
assetsDir := m.SourceRoot()
if strings.HasPrefix(assetsDir, h.ResourceSpec.WorkingDir) {
if jsConfig := h.ResourceSpec.JSConfigBuilder.Build(assetsDir); jsConfig != nil {
b, err := json.MarshalIndent(jsConfig, "", " ")
if err != nil {
h.Log.Warnf("Failed to create jsconfig.json: %s", err)
b, err := json.MarshalIndent(jsConfig, "", " ")
if err != nil {
h.Log.Warnf("Failed to create jsconfig.json: %s", err)
} else {
filename := filepath.Join(assetsDir, "jsconfig.json")
if h.running {
h.skipRebuildForFilenamesMu.Lock()
h.skipRebuildForFilenames[filename] = true
h.skipRebuildForFilenamesMu.Unlock()
}
// Make sure it's written to the OS fs as this is used by
// editors.
if err := afero.WriteFile(hugofs.Os, filename, b, 0666); err != nil {
h.Log.Warnf("Failed to write jsconfig.json: %s", err)
} else {
filename := filepath.Join(assetsDir, "jsconfig.json")
if h.running {
h.skipRebuildForFilenamesMu.Lock()
h.skipRebuildForFilenames[filename] = true
h.skipRebuildForFilenamesMu.Unlock()
}
// Make sure it's written to the OS fs as this is used by
// editors.
if err := afero.WriteFile(hugofs.Os, filename, b, 0666); err != nil {
h.Log.Warnf("Failed to write jsconfig.json: %s", err)
}
}
}
}

View File

@@ -176,12 +176,22 @@ path="github.com/gohugoio/hugoTestProjectJSModImports"
go 1.15
require github.com/gohugoio/hugoTestProjectJSModImports v0.3.0 // indirect
require github.com/gohugoio/hugoTestProjectJSModImports v0.5.0 // indirect
`)
b.WithContent("p1.md", "").WithNothingAdded()
b.WithSourceFile("package.json", `{
"dependencies": {
"date-fns": "^2.16.1"
}
}`)
b.Assert(os.Chdir(workDir), qt.IsNil)
_, err = exec.Command("npm", "install").CombinedOutput()
b.Assert(err, qt.IsNil)
b.Build(BuildCfg{})
b.AssertFileContent("public/js/main.js", `
@@ -189,8 +199,9 @@ greeting: "greeting configured in mod2"
Hello1 from mod1: $
return "Hello2 from mod1";
var Hugo = "Rocks!";
return "Hello3 from mod2";
return "Hello from lib in the main project";
Hello3 from mod2. Date from date-fns: ${today}
Hello from lib in the main project
Hello5 from mod2.
var myparam = "Hugo Rocks!";`)
}