Files
hugo/resources/jsconfig/jsconfig.go
T
Bjørn Erik Pedersen f88f0a9f59 resources/jsconfig: Drop source root mapping for the current source root
* Intellisense doesn't need it to do its work.
* This also avoids creating a jsconfig.js file in the common cases.

Fixes #15169
2026-08-11 15:57:03 +02:00

84 lines
2.2 KiB
Go

// Copyright 2020 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package jsconfig
import (
"path/filepath"
"sort"
"github.com/bep/helpers/maphelpers"
)
// Builder builds a jsconfig.json file that, currently, is used only to assist
// IntelliSense in editors.
type Builder struct {
sourceRoots *maphelpers.ConcurrentSet[string]
}
// NewBuilder creates a new Builder.
func NewBuilder() *Builder {
return &Builder{sourceRoots: maphelpers.NewConcurrentSet[string]()}
}
// Build builds a new Config with paths relative to dir.
// This method is thread safe.
func (b *Builder) Build(dir string) *Config {
if b.sourceRoots.Len() == 0 {
return nil
}
var roots []string
for root := range b.sourceRoots.All() {
if root == dir {
// We need mappings for other source roots (e.g. mounted modules), but not for the current one.
// See issue 15169.
continue
}
rel, err := filepath.Rel(dir, filepath.Join(root, "*"))
if err == nil {
roots = append(roots, rel)
}
}
if len(roots) == 0 {
return nil
}
sort.Strings(roots)
conf := newJSConfig()
conf.CompilerOptions.Paths["*"] = roots
return conf
}
// AddSourceRoot adds a new source root.
// This method is thread safe.
func (b *Builder) AddSourceRoot(root string) {
b.sourceRoots.AddIfAbsent(root)
}
// CompilerOptions holds compilerOptions for jsonconfig.json.
type CompilerOptions struct {
Paths map[string][]string `json:"paths"`
}
// Config holds the data for jsconfig.json.
type Config struct {
CompilerOptions CompilerOptions `json:"compilerOptions"`
}
func newJSConfig() *Config {
return &Config{
CompilerOptions: CompilerOptions{
Paths: make(map[string][]string),
},
}
}