Skip to content
/ hugo Public
forked from gohugoio/hugo

Commit

Permalink
js: Pass tsconfig.json to esBuild
Browse files Browse the repository at this point in the history
Note that esBuild only inspects/honors certain fields.
See https://esbuild.github.io/content-types/#tsconfig-json.

Related to gohugoio#11232
  • Loading branch information
jmooring committed Jul 11, 2023
1 parent 5bec508 commit ff027c0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions resources/resource_transformers/js/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.contents = string(src)
opts.mediaType = ctx.InMediaType
opts.tsConfig = t.c.rs.ResolveJSConfigFile("tsconfig.json")

buildOptions, err := toBuildOptions(opts)
if err != nil {
Expand Down
49 changes: 39 additions & 10 deletions resources/resource_transformers/js/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function hello3() {
-- layouts/index.html --
{{ $js := resources.Get "js/main.js" | js.Build }}
JS Content:{{ $js.Content }}:End:
`

c.Run("Basic", func(c *qt.C) {
Expand Down Expand Up @@ -90,17 +90,17 @@ disableKinds=["page", "section", "taxonomy", "term", "sitemap", "robotsTXT"]
path="github.com/gohugoio/hugoTestProjectJSModImports"
-- go.mod --
module github.com/gohugoio/tests/testHugoModules
go 1.16
require github.com/gohugoio/hugoTestProjectJSModImports v0.10.0 // indirect
-- package.json --
{
"dependencies": {
"date-fns": "^2.16.1"
}
}
`
b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
Expand Down Expand Up @@ -142,7 +142,7 @@ console.log("included");
-- assets/js/main.js --
import "./included";
import { toCamelCase } from "to-camel-case";
console.log("main");
console.log("To camel:", toCamelCase("space case"));
-- assets/js/myjsx.jsx --
Expand Down Expand Up @@ -222,7 +222,7 @@ import { hello1, hello2 } from './util1';
hello1();
hello2();
-- assets/js/util1.js --
/* Some
/* Some
comments.
*/
import { hello3 } from './util2';
Expand All @@ -239,7 +239,7 @@ export function hello3() {
-- layouts/index.html --
{{ $js := resources.Get "js/main.js" | js.Build }}
JS Content:{{ $js.Content }}:End:
`

c.Run("Import from main not found", func(c *qt.C) {
Expand Down Expand Up @@ -292,9 +292,9 @@ import 'imp3/foo.js';
}).Build()

expected := `
IMPORT_SRC_DIR:imp1/index.js
IMPORT_SRC_DIR:imp1/index.js
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)

Expand Down Expand Up @@ -340,7 +340,36 @@ console.log("Hello 2");
License util1
License util2
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")
}

0 comments on commit ff027c0

Please sign in to comment.