Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
jsconfig: Add support for subdirectory mounts
  • Loading branch information
UtkarshVerma committed Jun 20, 2021
commit a3d7b5fcc0aac293f565eea90b75cce9a8d73937
25 changes: 12 additions & 13 deletions resources/jsconfig/jsconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ package jsconfig

import (
"path/filepath"
"sort"
"sync"
)

// Builder builds a jsconfig.json file that, currently, is used only to assist
// intellinsense in editors.
type Builder struct {
sourceRootsMu sync.RWMutex
sourceRoots map[string]bool
sourceRoots map[string]string
}

// NewBuilder creates a new Builder.
func NewBuilder() *Builder {
return &Builder{sourceRoots: make(map[string]bool)}
return &Builder{sourceRoots: make(map[string]string)}
}

// Build builds a new Config with paths relative to dir.
Expand All @@ -42,32 +41,32 @@ func (b *Builder) Build(dir string) *Config {
}
conf := newJSConfig()

var roots []string
for root := range b.sourceRoots {
rel, err := filepath.Rel(dir, filepath.Join(root, "*"))
paths := make(map[string][]string)
for sourceRoot, mountRoot := range b.sourceRoots {
rel, err := filepath.Rel(dir, filepath.Join(sourceRoot, "*"))
if err == nil {
roots = append(roots, rel)
globPattern := filepath.Join(mountRoot, "*")
paths[globPattern] = append(paths[globPattern], rel)
}
}
sort.Strings(roots)
conf.CompilerOptions.Paths["*"] = roots

conf.CompilerOptions.Paths = paths
return conf
}

// AddSourceRoot adds a new source root.
// AddRoots adds a new source root and mount root.
// This method is thread safe.
func (b *Builder) AddSourceRoot(root string) {
func (b *Builder) AddRoots(sourceRoot, mountRoot string) {
b.sourceRootsMu.RLock()
found := b.sourceRoots[root]
_, found := b.sourceRoots[sourceRoot]
b.sourceRootsMu.RUnlock()

if found {
return
}

b.sourceRootsMu.Lock()
b.sourceRoots[root] = true
b.sourceRoots[sourceRoot] = mountRoot
b.sourceRootsMu.Unlock()
}

Expand Down
12 changes: 9 additions & 3 deletions resources/jsconfig/jsconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ func TestJsConfigBuilder(t *testing.T) {
c := qt.New(t)

b := NewBuilder()
b.AddSourceRoot("/c/assets")
b.AddSourceRoot("/d/assets")
b.AddRoots("/c/assets", "")
b.AddRoots("/d/assets", "d")

conf := b.Build("/a/b")
c.Assert(conf.CompilerOptions.BaseURL, qt.Equals, ".")
c.Assert(conf.CompilerOptions.Paths["*"], qt.DeepEquals, []string{filepath.FromSlash("../../c/assets/*"), filepath.FromSlash("../../d/assets/*")})
c.Assert(
conf.CompilerOptions.Paths,
qt.DeepEquals,
map[string][]string{
"*": {filepath.FromSlash("../../c/assets/*")},
"d/*": {filepath.FromSlash("../../d/assets/*")},
})

c.Assert(NewBuilder().Build("/a/b"), qt.IsNil)
}
2 changes: 1 addition & 1 deletion resources/resource_transformers/js/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func createBuildPlugins(c *Client, opts Options) ([]api.Plugin, error) {
// This should be a small number of elements, and when
// in server mode, we may get stale entries on renames etc.,
// but that shouldn't matter too much.
c.rs.JSConfigBuilder.AddSourceRoot(m.SourceRoot())
c.rs.JSConfigBuilder.AddRoots(m.SourceRoot(), m.MountRoot())
return api.OnResolveResult{Path: m.Filename(), Namespace: nsImportHugo}, nil
}

Expand Down