js: Pass tsconfig.json to esBuild

Note that esBuild only inspects/honors certain fields.
See https://esbuild.github.io/content-types/#tsconfig-json.

Fixes #11232
This commit is contained in:
Joe Mooring 2023-07-11 13:01:52 -07:00 committed by Bjørn Erik Pedersen
parent 5bec50838c
commit f1886f8c37
2 changed files with 40 additions and 10 deletions

View file

@ -86,6 +86,7 @@ func (t *buildTransformation) Transform(ctx *resources.ResourceTransformationCtx
opts.resolveDir = t.c.rs.Cfg.BaseConfig().WorkingDir // where node_modules gets resolved opts.resolveDir = t.c.rs.Cfg.BaseConfig().WorkingDir // where node_modules gets resolved
opts.contents = string(src) opts.contents = string(src)
opts.mediaType = ctx.InMediaType opts.mediaType = ctx.InMediaType
opts.tsConfig = t.c.rs.ResolveJSConfigFile("tsconfig.json")
buildOptions, err := toBuildOptions(opts) buildOptions, err := toBuildOptions(opts)
if err != nil { if err != nil {

View file

@ -48,7 +48,7 @@ export function hello3() {
-- layouts/index.html -- -- layouts/index.html --
{{ $js := resources.Get "js/main.js" | js.Build }} {{ $js := resources.Get "js/main.js" | js.Build }}
JS Content:{{ $js.Content }}:End: JS Content:{{ $js.Content }}:End:
` `
c.Run("Basic", func(c *qt.C) { c.Run("Basic", func(c *qt.C) {
@ -90,9 +90,9 @@ disableKinds=["page", "section", "taxonomy", "term", "sitemap", "robotsTXT"]
path="github.com/gohugoio/hugoTestProjectJSModImports" path="github.com/gohugoio/hugoTestProjectJSModImports"
-- go.mod -- -- go.mod --
module github.com/gohugoio/tests/testHugoModules module github.com/gohugoio/tests/testHugoModules
go 1.16 go 1.16
require github.com/gohugoio/hugoTestProjectJSModImports v0.10.0 // indirect require github.com/gohugoio/hugoTestProjectJSModImports v0.10.0 // indirect
-- package.json -- -- package.json --
{ {
@ -100,7 +100,7 @@ require github.com/gohugoio/hugoTestProjectJSModImports v0.10.0 // indirect
"date-fns": "^2.16.1" "date-fns": "^2.16.1"
} }
} }
` `
b := hugolib.NewIntegrationTestBuilder( b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{ hugolib.IntegrationTestConfig{
@ -142,7 +142,7 @@ console.log("included");
-- assets/js/main.js -- -- assets/js/main.js --
import "./included"; import "./included";
import { toCamelCase } from "to-camel-case"; import { toCamelCase } from "to-camel-case";
console.log("main"); console.log("main");
console.log("To camel:", toCamelCase("space case")); console.log("To camel:", toCamelCase("space case"));
-- assets/js/myjsx.jsx -- -- assets/js/myjsx.jsx --
@ -222,7 +222,7 @@ import { hello1, hello2 } from './util1';
hello1(); hello1();
hello2(); hello2();
-- assets/js/util1.js -- -- assets/js/util1.js --
/* Some /* Some
comments. comments.
*/ */
import { hello3 } from './util2'; import { hello3 } from './util2';
@ -239,7 +239,7 @@ export function hello3() {
-- layouts/index.html -- -- layouts/index.html --
{{ $js := resources.Get "js/main.js" | js.Build }} {{ $js := resources.Get "js/main.js" | js.Build }}
JS Content:{{ $js.Content }}:End: JS Content:{{ $js.Content }}:End:
` `
c.Run("Import from main not found", func(c *qt.C) { c.Run("Import from main not found", func(c *qt.C) {
@ -292,9 +292,9 @@ import 'imp3/foo.js';
}).Build() }).Build()
expected := ` expected := `
IMPORT_SRC_DIR:imp1/index.js IMPORT_SRC_DIR:imp1/index.js
IMPORT_SRC_DIR:imp2/index.ts IMPORT_SRC_DIR:imp2/index.ts
IMPORT_SRC_DIR:imp3/foo.ts IMPORT_SRC_DIR:imp3/foo.ts
` `
expected = strings.ReplaceAll(expected, "IMPORT_SRC_DIR", importSrcDir) expected = strings.ReplaceAll(expected, "IMPORT_SRC_DIR", importSrcDir)
@ -340,7 +340,36 @@ console.log("Hello 2");
License util1 License util1
License util2 License util2
Main license Main license
`) `)
} }
// Issue #11232
func TestTypeScriptExperimentalDecorators(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
disableKinds = ['RSS','sitemap','taxonomy','term']
-- tsconfig.json --
{
"compilerOptions": {
"experimentalDecorators": true,
}
}
-- assets/ts/main.ts --
function addFoo(target: any) {target.prototype.foo = 'bar'}
@addFoo
class A {}
-- layouts/index.html --
{{ $opts := dict "target" "es2020" "targetPath" "js/main.js" }}
{{ (resources.Get "ts/main.ts" | js.Build $opts).Publish }}
`
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
NeedsOsFS: true,
TxtarString: files,
}).Build()
b.AssertFileContent("public/js/main.js", "__decorateClass")
}