Skip to content

Commit

Permalink
js.Build Add automatic building of tsconfig files to map @assets and @js
Browse files Browse the repository at this point in the history


Map @assets to search through root and theme layer assets.
Map @js to search through root and theme layer assets for a js folder with the file.
Read original ts/js config and amend settings to temporary tsconfig file.

Ref gohugoio#7656
  • Loading branch information
richtera committed Sep 13, 2020
1 parent 0bc8711 commit 7b2664b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<<<<<<< Updated upstream
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.37.4/go.mod h1:NHPJ89PdicEuT9hdPXMROBD91xc5uRDxsMtSB16k7hw=
Expand Down Expand Up @@ -138,6 +137,7 @@ github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/evanw/esbuild v0.6.32 h1:hVuqC+IgEENPWnr0gic01EFgGCmyW8dUPnr78zC7K5k=
github.com/evanw/esbuild v0.6.32/go.mod h1:mptxmSXIzBIKKCe4jo9A5SToEd1G+AKZ9JmY85dYRJ0=
github.com/evanw/esbuild v0.7.0 h1:bOZiSNkCm0AiICecv2ivxoq6ZNpy4lCvWthLPnE3oVQ=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fortytw2/leaktest v1.2.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
Expand Down
92 changes: 91 additions & 1 deletion resources/resource_transformers/js/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
package js

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
Expand Down Expand Up @@ -74,6 +76,7 @@ type Options struct {
sourcefile string
resolveDir string
workDir string
tsConfig string
}

func decodeOptions(m map[string]interface{}) (Options, error) {
Expand Down Expand Up @@ -138,12 +141,99 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
opts.contents = string(src)
opts.mediaType = ctx.InMediaType

// Search for original ts/jsconfig file
tsConfig := path.Join(sdir, "tsconfig.json")
_, err = t.sfs.Fs.Stat(tsConfig)
if err != nil {
tsConfig = path.Join(sdir, "jsconfig.json")
_, err = t.sfs.Fs.Stat(tsConfig)
if err != nil {
tsConfig = path.Join(opts.workDir, "tsconfig.json")
_, err = t.sfs.Fs.Stat(tsConfig)
if err != nil {
tsConfig = path.Join(opts.workDir, "jsconfig.json")
_, err = t.sfs.Fs.Stat(tsConfig)
if err != nil {
tsConfig = ""
}
}
}
}

// Get real source path
configDir, _ := path.Split(t.sfs.RealFilename(ctx.SourcePath))

// Resolve paths for @assets and @js (@js is just an alias for assets/js)
dirs := make([]interface{}, 0)
jsDirs := make([]interface{}, 0)
for _, dir := range t.sfs.RealDirs(".") {
rel, _ := filepath.Rel(configDir, dir)
dirs = append(dirs, "./"+rel+"/*")
jsDirs = append(jsDirs, "./"+rel+"/js/*")
}

// Create new temporary tsconfig file
newTSConfig, err := ioutil.TempFile(configDir, "tsconfig.*.json")
if err != nil {
return err
}

// Construct new temporary tsconfig file content
config := make(map[string]interface{})
if tsConfig != "" {
oldConfig, err := ioutil.ReadFile(t.sfs.RealFilename(tsConfig))
if err != nil {
return err
}
err = json.Unmarshal(oldConfig, &config)
if err != nil {
return err
}
} else {
config["compilerOptions"] = map[string]interface{}{
"baseUrl": ".",
}
}

// Assign new global paths to the config file while reading existing ones.
oldCompilerOptions := config["compilerOptions"].(map[string]interface{})
oldPaths := oldCompilerOptions["paths"].(map[string]interface{})
if oldPaths == nil {
oldPaths = make(map[string]interface{})
oldCompilerOptions["paths"] = oldPaths
}
oldPaths["@assets/*"] = dirs
oldPaths["@js/*"] = jsDirs

// Output the new config file
bytes, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}

// Write tsconfig file
_, err = newTSConfig.Write(bytes)
if err != nil {
return err
}
err = newTSConfig.Close()
if err != nil {
return err
}

// Tell ESBuild about this new config file to use
opts.tsConfig = newTSConfig.Name()

buildOptions, err := toBuildOptions(opts)
if err != nil {
os.Remove(opts.tsConfig)
return err
}

result := api.Build(buildOptions)

os.Remove(opts.tsConfig)

if len(result.Warnings) > 0 {
for _, value := range result.Errors {
t.rs.Logger.WARN.Println(fmt.Sprintf("%s:%d: WARN: %s",
Expand Down Expand Up @@ -287,7 +377,7 @@ func toBuildOptions(opts Options) (buildOptions api.BuildOptions, err error) {
JSXFactory: opts.JSXFactory,
JSXFragment: opts.JSXFragment,

//Tsconfig: opts.TSConfig,
Tsconfig: opts.tsConfig,

Stdin: &api.StdinOptions{
Contents: opts.contents,
Expand Down

0 comments on commit 7b2664b

Please sign in to comment.