| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package js_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"os" |
| 18 |
"path/filepath" |
| 19 |
"strings" |
| 20 |
"testing" |
| 21 |
|
| 22 |
qt "github.com/frankban/quicktest" |
| 23 |
"github.com/gohugoio/hugo/common/paths" |
| 24 |
"github.com/gohugoio/hugo/htesting" |
| 25 |
"github.com/gohugoio/hugo/hugolib" |
| 26 |
"github.com/gohugoio/hugo/internal/js/esbuild" |
| 27 |
) |
| 28 |
|
| 29 |
func TestBuildVariants(t *testing.T) { |
| 30 |
c := qt.New(t) |
| 31 |
|
| 32 |
mainWithImport := ` |
| 33 |
-- hugo.toml -- |
| 34 |
disableKinds=["page", "section", "taxonomy", "term", "sitemap", "robotsTXT"] |
| 35 |
disableLiveReload = true |
| 36 |
-- assets/js/main.js -- |
| 37 |
import { hello1, hello2 } from './util1'; |
| 38 |
hello1(); |
| 39 |
hello2(); |
| 40 |
-- assets/js/util1.js -- |
| 41 |
import { hello3 } from './util2'; |
| 42 |
export function hello1() { |
| 43 |
return 'abcd'; |
| 44 |
} |
| 45 |
export function hello2() { |
| 46 |
return hello3(); |
| 47 |
} |
| 48 |
-- assets/js/util2.js -- |
| 49 |
export function hello3() { |
| 50 |
return 'efgh'; |
| 51 |
} |
| 52 |
-- layouts/home.html -- |
| 53 |
{{ $js := resources.Get "js/main.js" | js.Build }} |
| 54 |
JS Content:{{ $js.Content }}:End: |
| 55 |
|
| 56 |
` |
| 57 |
|
| 58 |
c.Run("Basic", func(c *qt.C) { |
| 59 |
b := hugolib.Test(c, mainWithImport, hugolib.TestOptOsFs()) |
| 60 |
|
| 61 |
b.AssertFileContent("public/index.html", `abcd`) |
| 62 |
}) |
| 63 |
|
| 64 |
c.Run("Edit Import", func(c *qt.C) { |
| 65 |
b := hugolib.Test(c, mainWithImport, hugolib.TestOptRunning(), hugolib.TestOptOsFs()) |
| 66 |
|
| 67 |
b.AssertFileContent("public/index.html", `abcd`) |
| 68 |
b.EditFileReplaceFunc("assets/js/util1.js", func(s string) string { return strings.ReplaceAll(s, "abcd", "1234") }).Build() |
| 69 |
b.AssertFileContent("public/index.html", `1234`) |
| 70 |
}) |
| 71 |
|
| 72 |
c.Run("Edit Import Nested", func(c *qt.C) { |
| 73 |
b := hugolib.Test(c, mainWithImport, hugolib.TestOptRunning(), hugolib.TestOptOsFs()) |
| 74 |
|
| 75 |
b.AssertFileContent("public/index.html", `efgh`) |
| 76 |
b.EditFileReplaceFunc("assets/js/util2.js", func(s string) string { return strings.ReplaceAll(s, "efgh", "1234") }).Build() |
| 77 |
b.AssertFileContent("public/index.html", `1234`) |
| 78 |
}) |
| 79 |
} |
| 80 |
|
| 81 |
// Issue #15173. |
| 82 |
func TestBuildDataArtifacts(t *testing.T) { |
| 83 |
t.Parallel() |
| 84 |
|
| 85 |
files := ` |
| 86 |
-- hugo.toml -- |
| 87 |
baseURL = "https://example.org/" |
| 88 |
disableKinds=["page", "section", "taxonomy", "term", "sitemap", "robotsTXT"] |
| 89 |
-- assets/js/main.js -- |
| 90 |
import { hello } from './util'; |
| 91 |
hello(); |
| 92 |
-- assets/js/util.js -- |
| 93 |
export function hello() { |
| 94 |
return 'abcd'; |
| 95 |
} |
| 96 |
-- layouts/home.html -- |
| 97 |
{{ with resources.Get "js/main.js" | js.Build (dict "minify" true "sourcemap" "external") }} |
| 98 |
COUNT: {{ len .Data.Artifacts }} |
| 99 |
{{ range .Data.Artifacts }} |
| 100 |
ARTIFACT: {{ .RelPermalink }}|{{ .Permalink }}|{{ .MediaType.Type }} |
| 101 |
{{ end }} |
| 102 |
{{ end }} |
| 103 |
{{ with resources.Get "js/main.js" | js.Build }} |
| 104 |
COUNT2: {{ len .Data.Artifacts }} |
| 105 |
{{ end }} |
| 106 |
` |
| 107 |
|
| 108 |
b := hugolib.Test(t, files, hugolib.TestOptOsFs()) |
| 109 |
b.AssertFileContent("public/index.html", |
| 110 |
"COUNT: 1", |
| 111 |
"ARTIFACT: /js/main.js.map|https://example.org/js/main.js.map|application/source-map", |
| 112 |
"COUNT2: 0", |
| 113 |
) |
| 114 |
b.AssertFileExists("public/js/main.js.map", true) |
| 115 |
} |
| 116 |
|
| 117 |
func TestBuildWithModAndNpm(t *testing.T) { |
| •118 |
if os.Getenv("AUTOPKGTEST_TMP") == "" { |
| 119 |
t.Skip("Skip test as network is inaccessible during Debian package build") |
| 120 |
} |
| 121 |
if !htesting.IsCI() { |
| 122 |
t.Skip("skip (relative) long running modules test when running locally") |
| 123 |
} |
| 124 |
|
| 125 |
c := qt.New(t) |
| 126 |
|
| 127 |
files := ` |
| 128 |
-- hugo.toml -- |
| 129 |
baseURL = "https://example.org" |
| 130 |
disableKinds=["page", "section", "taxonomy", "term", "sitemap", "robotsTXT"] |
| 131 |
[module] |
| 132 |
[[module.imports]] |
| 133 |
path="github.com/gohugoio/hugoTestProjectJSModImports" |
| 134 |
-- go.mod -- |
| 135 |
module github.com/gohugoio/tests/testHugoModules |
| 136 |
|
| 137 |
go 1.16 |
| 138 |
|
| 139 |
require github.com/gohugoio/hugoTestProjectJSModImports v0.10.0 // indirect |
| 140 |
-- package.json -- |
| 141 |
{ |
| 142 |
"dependencies": { |
| 143 |
"date-fns": "^2.16.1" |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
` |
| 148 |
b := hugolib.Test(c, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptWithConfig(func(cfg *hugolib.IntegrationTestConfig) { |
| 149 |
cfg.Verbose = true |
| 150 |
})) |
| 151 |
|
| 152 |
b.AssertFileContent("public/js/main.js", ` |
| 153 |
greeting: "greeting configured in mod2" |
| 154 |
Hello1 from mod1: $ |
| 155 |
return "Hello2 from mod1"; |
| 156 |
var Hugo = "Rocks!"; |
| 157 |
Hello3 from mod2. Date from date-fns: ${today} |
| 158 |
Hello from lib in the main project |
| 159 |
Hello5 from mod2. |
| 160 |
var myparam = "Hugo Rocks!"; |
| 161 |
shim cwd |
| 162 |
`) |
| 163 |
|
| 164 |
// React JSX, verify the shimming. |
| 165 |
b.AssertFileContent("public/js/like.js", filepath.FromSlash(`@v0.10.0/assets/js/shims/react.js |
| 166 |
module.exports = window.ReactDOM; |
| 167 |
`)) |
| 168 |
} |
| 169 |
|
| 170 |
func TestBuildWithNpm(t *testing.T) { |
| •171 |
if os.Getenv("AUTOPKGTEST_TMP") == "" { |
| 172 |
t.Skip("Skip test as network is inaccessible during Debian package build") |
| 173 |
} |
| 174 |
if !htesting.IsCI() { |
| 175 |
t.Skip("skip (relative) long running modules test when running locally") |
| 176 |
} |
| 177 |
|
| 178 |
c := qt.New(t) |
| 179 |
|
| 180 |
files := ` |
| 181 |
-- assets/js/included.js -- |
| 182 |
console.log("included"); |
| 183 |
-- assets/js/main.js -- |
| 184 |
import "./included"; |
| 185 |
import { toCamelCase } from "to-camel-case"; |
| 186 |
|
| 187 |
console.log("main"); |
| 188 |
console.log("To camel:", toCamelCase("space case")); |
| 189 |
-- assets/js/myjsx.jsx -- |
| 190 |
import * as React from 'react' |
| 191 |
import * as ReactDOM from 'react-dom' |
| 192 |
|
| 193 |
ReactDOM.render( |
| 194 |
<h1>Hello, world!</h1>, |
| 195 |
document.getElementById('root') |
| 196 |
); |
| 197 |
-- assets/js/myts.ts -- |
| 198 |
function greeter(person: string) { |
| 199 |
return "Hello, " + person; |
| 200 |
} |
| 201 |
let user = [0, 1, 2]; |
| 202 |
document.body.textContent = greeter(user); |
| 203 |
-- hugo.toml -- |
| 204 |
disablekinds = ['taxonomy', 'term', 'page'] |
| 205 |
-- content/p1.md -- |
| 206 |
Content. |
| 207 |
-- data/hugo.toml -- |
| 208 |
slogan = "Hugo Rocks!" |
| 209 |
-- i18n/en.yaml -- |
| 210 |
hello: |
| 211 |
other: "Hello" |
| 212 |
-- i18n/fr.yaml -- |
| 213 |
hello: |
| 214 |
other: "Bonjour" |
| 215 |
-- layouts/home.html -- |
| 216 |
{{ $options := dict "minify" false "externals" (slice "react" "react-dom") "sourcemap" "linked" }} |
| 217 |
{{ $js := resources.Get "js/main.js" | js.Build $options }} |
| 218 |
JS: {{ template "print" $js }} |
| 219 |
{{ $jsx := resources.Get "js/myjsx.jsx" | js.Build $options }} |
| 220 |
JSX: {{ template "print" $jsx }} |
| 221 |
{{ $ts := resources.Get "js/myts.ts" | js.Build (dict "sourcemap" "inline")}} |
| 222 |
TS: {{ template "print" $ts }} |
| 223 |
{{ $ts2 := resources.Get "js/myts.ts" | js.Build (dict "sourcemap" "external" "TargetPath" "js/myts2.js")}} |
| 224 |
TS2: {{ template "print" $ts2 }} |
| 225 |
{{ define "print" }}RelPermalink: {{.RelPermalink}}|MIME: {{ .MediaType }}|Content: {{ .Content | safeJS }}{{ end }} |
| 226 |
-- package.json -- |
| 227 |
{ |
| 228 |
"scripts": {}, |
| 229 |
|
| 230 |
"dependencies": { |
| 231 |
"to-camel-case": "1.0.0" |
| 232 |
} |
| 233 |
} |
| 234 |
` |
| 235 |
|
| 236 |
b := hugolib.Test(c, files, hugolib.TestOptOsFs(), hugolib.TestOptWithNpmInstall()) |
| 237 |
|
| 238 |
b.AssertFileContent("public/js/main.js", `//# sourceMappingURL=main.js.map`) |
| 239 |
b.AssertFileContent("public/js/main.js.map", `"version":3`, "! ns-hugo") // linked |
| 240 |
b.AssertFileContent("public/js/myts.js", `//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJz`) // inline |
| 241 |
b.AssertFileContent("public/index.html", ` |
| 242 |
console.log("included"); |
| 243 |
if (hasSpace.test(string)) |
| 244 |
var React = __toESM(__require("react")); |
| 245 |
function greeter(person) { |
| 246 |
`) |
| 247 |
|
| 248 |
checkMap := func(p string, expectLen int) { |
| 249 |
s := b.FileContent(p) |
| 250 |
sources := esbuild.SourcesFromSourceMap(s) |
| 251 |
b.Assert(sources, qt.HasLen, expectLen) |
| 252 |
|
| 253 |
// Check that all source files exist. |
| 254 |
for _, src := range sources { |
| 255 |
filename, ok := paths.UrlStringToFilename(src) |
| 256 |
b.Assert(ok, qt.IsTrue) |
| 257 |
_, err := os.Stat(filename) |
| 258 |
b.Assert(err, qt.IsNil, qt.Commentf("src: %q", src)) |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
checkMap("public/js/main.js.map", 5) |
| 263 |
} |
| 264 |
|
| 265 |
func TestBuildError(t *testing.T) { |
| 266 |
c := qt.New(t) |
| 267 |
|
| 268 |
filesTemplate := ` |
| 269 |
-- hugo.toml -- |
| 270 |
disableKinds=["page", "section", "taxonomy", "term", "sitemap", "robotsTXT"] |
| 271 |
-- assets/js/main.js -- |
| 272 |
// A comment. |
| 273 |
import { hello1, hello2 } from './util1'; |
| 274 |
hello1(); |
| 275 |
hello2(); |
| 276 |
-- assets/js/util1.js -- |
| 277 |
/* Some |
| 278 |
comments. |
| 279 |
*/ |
| 280 |
import { hello3 } from './util2'; |
| 281 |
export function hello1() { |
| 282 |
return 'abcd'; |
| 283 |
} |
| 284 |
export function hello2() { |
| 285 |
return hello3(); |
| 286 |
} |
| 287 |
-- assets/js/util2.js -- |
| 288 |
export function hello3() { |
| 289 |
return 'efgh'; |
| 290 |
} |
| 291 |
-- layouts/home.html -- |
| 292 |
{{ $js := resources.Get "js/main.js" | js.Build }} |
| 293 |
JS Content:{{ $js.Content }}:End: |
| 294 |
|
| 295 |
` |
| 296 |
|
| 297 |
c.Run("Import from main not found", func(c *qt.C) { |
| 298 |
c.Parallel() |
| 299 |
files := strings.Replace(filesTemplate, "import { hello1, hello2 }", "import { hello1, hello2, FOOBAR }", 1) |
| 300 |
b, err := hugolib.TestE(c, files, hugolib.TestOptOsFs()) |
| 301 |
b.Assert(err, qt.IsNotNil) |
| 302 |
b.Assert(err.Error(), qt.Contains, `main.js:2:25": No matching export`) |
| 303 |
}) |
| 304 |
|
| 305 |
c.Run("Import from import not found", func(c *qt.C) { |
| 306 |
c.Parallel() |
| 307 |
files := strings.Replace(filesTemplate, "import { hello3 } from './util2';", "import { hello3, FOOBAR } from './util2';", 1) |
| 308 |
b, err := hugolib.TestE(c, files, hugolib.TestOptOsFs()) |
| 309 |
b.Assert(err, qt.IsNotNil) |
| 310 |
b.Assert(err.Error(), qt.Contains, `util1.js:4:17": No matching export in`) |
| 311 |
}) |
| 312 |
} |
| 313 |
|
| 314 |
// See issue 10527. |
| 315 |
func TestImportHugoVsESBuild(t *testing.T) { |
| 316 |
c := qt.New(t) |
| 317 |
|
| 318 |
for _, importSrcDir := range []string{"node_modules", "assets"} { |
| 319 |
c.Run(importSrcDir, func(c *qt.C) { |
| 320 |
files := ` |
| 321 |
-- IMPORT_SRC_DIR/imp1/index.js -- |
| 322 |
console.log("IMPORT_SRC_DIR:imp1/index.js"); |
| 323 |
-- IMPORT_SRC_DIR/imp2/index.ts -- |
| 324 |
console.log("IMPORT_SRC_DIR:imp2/index.ts"); |
| 325 |
-- IMPORT_SRC_DIR/imp3/foo.ts -- |
| 326 |
console.log("IMPORT_SRC_DIR:imp3/foo.ts"); |
| 327 |
-- assets/js/main.js -- |
| 328 |
import 'imp1/index.js'; |
| 329 |
import 'imp2/index.js'; |
| 330 |
import 'imp3/foo.js'; |
| 331 |
-- layouts/home.html -- |
| 332 |
{{ $js := resources.Get "js/main.js" | js.Build }} |
| 333 |
{{ $js.RelPermalink }} |
| 334 |
` |
| 335 |
|
| 336 |
files = strings.ReplaceAll(files, "IMPORT_SRC_DIR", importSrcDir) |
| 337 |
|
| 338 |
b := hugolib.Test(c, files, hugolib.TestOptOsFs()) |
| 339 |
|
| 340 |
expected := ` |
| 341 |
IMPORT_SRC_DIR:imp1/index.js |
| 342 |
IMPORT_SRC_DIR:imp2/index.ts |
| 343 |
IMPORT_SRC_DIR:imp3/foo.ts |
| 344 |
` |
| 345 |
expected = strings.ReplaceAll(expected, "IMPORT_SRC_DIR", importSrcDir) |
| 346 |
|
| 347 |
b.AssertFileContent("public/js/main.js", expected) |
| 348 |
}) |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
// See https://github.com/evanw/esbuild/issues/2745 |
| 353 |
func TestPreserveLegalComments(t *testing.T) { |
| 354 |
t.Parallel() |
| 355 |
|
| 356 |
files := ` |
| 357 |
-- assets/js/main.js -- |
| 358 |
/* @license |
| 359 |
* Main license. |
| 360 |
*/ |
| 361 |
import * as foo from 'js/utils'; |
| 362 |
console.log("Hello Main"); |
| 363 |
-- assets/js/utils/index.js -- |
| 364 |
export * from './util1'; |
| 365 |
export * from './util2'; |
| 366 |
-- assets/js/utils/util1.js -- |
| 367 |
/*! License util1 */ |
| 368 |
console.log("Hello 1"); |
| 369 |
-- assets/js/utils/util2.js -- |
| 370 |
//! License util2 */ |
| 371 |
console.log("Hello 2"); |
| 372 |
-- layouts/home.html -- |
| 373 |
{{ $js := resources.Get "js/main.js" | js.Build (dict "minify" false) }} |
| 374 |
{{ $js.RelPermalink }} |
| 375 |
` |
| 376 |
|
| 377 |
b := hugolib.Test(t, files, hugolib.TestOptOsFs()) |
| 378 |
|
| 379 |
b.AssertFileContent("public/js/main.js", ` |
| 380 |
License util1 |
| 381 |
License util2 |
| 382 |
Main license |
| 383 |
|
| 384 |
`) |
| 385 |
} |
| 386 |
|
| 387 |
// Issue #11232 |
| 388 |
func TestTypeScriptExperimentalDecorators(t *testing.T) { |
| 389 |
t.Parallel() |
| 390 |
files := ` |
| 391 |
-- hugo.toml -- |
| 392 |
disableKinds = ['RSS','sitemap','taxonomy','term'] |
| 393 |
-- tsconfig.json -- |
| 394 |
{ |
| 395 |
"compilerOptions": { |
| 396 |
"experimentalDecorators": true, |
| 397 |
} |
| 398 |
} |
| 399 |
-- assets/ts/main.ts -- |
| 400 |
function addFoo(target: any) {target.prototype.foo = 'bar'} |
| 401 |
@addFoo |
| 402 |
class A {} |
| 403 |
-- layouts/home.html -- |
| 404 |
{{ $opts := dict "target" "es2020" "targetPath" "js/main.js" }} |
| 405 |
{{ (resources.Get "ts/main.ts" | js.Build $opts).Publish }} |
| 406 |
` |
| 407 |
b := hugolib.Test(t, files, hugolib.TestOptOsFs()) |
| 408 |
b.AssertFileContent("public/js/main.js", "__decorateClass") |
| 409 |
} |
| 410 |
|
| 411 |
// Issue 13183. |
| 412 |
func TestExternalsInAssets(t *testing.T) { |
| 413 |
files := ` |
| 414 |
-- assets/js/util1.js -- |
| 415 |
export function hello1() { |
| 416 |
return 'abcd'; |
| 417 |
} |
| 418 |
-- assets/js/util2.js -- |
| 419 |
export function hello2() { |
| 420 |
return 'efgh'; |
| 421 |
} |
| 422 |
-- assets/js/main.js -- |
| 423 |
import { hello1 } from './util1.js'; |
| 424 |
import { hello2 } from './util2.js'; |
| 425 |
|
| 426 |
hello1(); |
| 427 |
hello2(); |
| 428 |
-- layouts/home.html -- |
| 429 |
Home. |
| 430 |
{{ $js := resources.Get "js/main.js" | js.Build (dict "externals" (slice "./util1.js")) }} |
| 431 |
{{ $js.Publish }} |
| 432 |
` |
| 433 |
|
| 434 |
b := hugolib.Test(t, files, hugolib.TestOptOsFs()) |
| 435 |
|
| 436 |
b.AssertFileContent("public/js/main.js", "efgh") |
| 437 |
b.AssertFileContent("public/js/main.js", "! abcd") |
| 438 |
} |
| 439 |
|
| 440 |
func TestBuildErrorStack(t *testing.T) { |
| 441 |
files := ` |
| 442 |
-- hugo.toml -- |
| 443 |
disableKinds=["page", "section", "taxonomy", "term", "sitemap", "robotsTXT"] |
| 444 |
-- assets/js/main.js -- |
| 445 |
import { hello1, hello2 } from './util1'; |
| 446 |
hello1(); |
| 447 |
-- layouts/home.html -- |
| 448 |
{{ $js := resources.Get "js/main.js" | js.Build }} |
| 449 |
JS Content:{{ $js.Content }}:End: |
| 450 |
` |
| 451 |
|
| 452 |
b, err := hugolib.TestE(t, files, hugolib.TestOptOsFs()) |
| 453 |
b.Assert(err, qt.IsNotNil) |
| 454 |
b.Assert(err.Error(), qt.Contains, `execute of template failed: template: home.html:2:17`) |
| 455 |
b.Assert(err.Error(), qt.Contains, `main.js:1:31": Could not resolve "./util1"`) |
| 456 |
} |