Debian Codegraph @

internal/js/esbuild/batch_integration_test.go

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
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 provides functions for building JavaScript resources
15 package esbuild_test
16
17 import (
18 "os"
19 "path/filepath"
20 "strings"
21 "testing"
22
23 qt "github.com/frankban/quicktest"
24
25 "github.com/gohugoio/hugo/common/paths"
26 "github.com/gohugoio/hugo/htesting"
27 "github.com/gohugoio/hugo/hugolib"
28 "github.com/gohugoio/hugo/internal/js/esbuild"
29 )
30
31 // Used to test misc. error situations etc.
32 const jsBatchFilesTemplate = `
33 -- hugo.toml --
34 disableKinds = ["taxonomy", "term", "section"]
35 disableLiveReload = true
36 -- assets/js/styles.css --
37 body {
38 background-color: red;
39 }
40 -- assets/js/main.js --
41 import './styles.css';
42 import * as params from '@params';
43 import * as foo from 'mylib';
44 console.log("Hello, Main!");
45 console.log("params.p1", params.p1);
46 export default function Main() {};
47 -- assets/js/runner.js --
48 console.log("Hello, Runner!");
49 -- node_modules/mylib/index.js --
50 console.log("Hello, My Lib!");
51 -- layouts/_shortcodes/hdx.html --
52 {{ $path := .Get "r" }}
53 {{ $r := or (.Page.Resources.Get $path) (resources.Get $path) }}
54 {{ $batch := (js.Batch "mybatch") }}
55 {{ $scriptID := $path | anchorize }}
56 {{ $instanceID := .Ordinal | string }}
57 {{ $group := .Page.RelPermalink | anchorize }}
58 {{ $params := .Params | default dict }}
59 {{ $export := .Get "export" | default "default" }}
60 {{ with $batch.Group $group }}
61 {{ with .Runner "create-elements" }}
62 {{ .SetOptions (dict "resource" (resources.Get "js/runner.js")) }}
63 {{ end }}
64 {{ with .Script $scriptID }}
65 {{ .SetOptions (dict
66 "resource" $r
67 "export" $export
68 "importContext" (slice $.Page)
69 )
70 }}
71 {{ end }}
72 {{ with .Instance $scriptID $instanceID }}
73 {{ .SetOptions (dict "params" $params) }}
74 {{ end }}
75 {{ end }}
76 hdx-instance: {{ $scriptID }}: {{ $instanceID }}|
77 -- layouts/baseof.html --
78 Base.
79 {{ $batch := (js.Batch "mybatch") }}
80 {{ with $batch.Config }}
81 {{ .SetOptions (dict
82 "params" (dict "id" "config")
83 "sourceMap" ""
84 )
85 }}
86 {{ end }}
87 {{ with (templates.Defer (dict "key" "global")) }}
88 Defer:
89 {{ $batch := (js.Batch "mybatch") }}
90 {{ range $k, $v := $batch.Build.Groups }}
91 {{ range $kk, $vv := . -}}
92 {{ $k }}: {{ .RelPermalink }}
93 {{ end }}
94 {{ end -}}
95 {{ end }}
96 {{ block "main" . }}Main{{ end }}
97 End.
98 -- layouts/single.html --
99 {{ define "main" }}
100 ==> Single Template Content: {{ .Content }}$
101 {{ $batch := (js.Batch "mybatch") }}
102 {{ with $batch.Group "mygroup" }}
103 {{ with .Runner "run" }}
104 {{ .SetOptions (dict "resource" (resources.Get "js/runner.js")) }}
105 {{ end }}
106 {{ with .Script "main" }}
107 {{ .SetOptions (dict "resource" (resources.Get "js/main.js") "params" (dict "p1" "param-p1-main" )) }}
108 {{ end }}
109 {{ with .Instance "main" "i1" }}
110 {{ .SetOptions (dict "params" (dict "title" "Instance 1")) }}
111 {{ end }}
112 {{ end }}
113 {{ end }}
114 -- layouts/home.html --
115 {{ define "main" }}
116 Home.
117 {{ end }}
118 -- content/p1/index.md --
119 ---
120 title: "P1"
121 ---
122
123 Some content.
124
125 {{< hdx r="p1script.js" myparam="p1-param-1" >}}
126 {{< hdx r="p1script.js" myparam="p1-param-2" >}}
127
128 -- content/p1/p1script.js --
129 console.log("P1 Script");
130
131
132 `
133
134 // Just to verify that the above file setup works.
135 func TestBatchTemplateOKBuild(t *testing.T) {
136 b := hugolib.Test(t, jsBatchFilesTemplate, hugolib.TestOptWithOSFs())
137 b.AssertPublishDir("mybatch/mygroup.js", "mybatch/mygroup.css")
138 }
139
140 func TestBatchRemoveAllInGroup(t *testing.T) {
141 files := jsBatchFilesTemplate
142 b := hugolib.TestRunning(t, files, hugolib.TestOptWithOSFs())
143
144 b.AssertFileContent("public/p1/index.html", "p1: /mybatch/p1.js")
145
146 b.EditFiles("content/p1/index.md", `
147 ---
148 title: "P1"
149 ---
150 Empty.
151 `)
152 b.Build()
153
154 b.AssertFileContent("public/p1/index.html", "! p1: /mybatch/p1.js")
155
156 // Add one script back.
157 b.EditFiles("content/p1/index.md", `
158 ---
159 title: "P1"
160 ---
161
162 {{< hdx r="p1script.js" myparam="p1-param-1-new" >}}
163 `)
164 b.Build()
165
166 b.AssertFileContent("public/mybatch/p1.js",
167 "p1-param-1-new",
168 "p1script.js")
169 }
170
171 func TestBatchEditInstance(t *testing.T) {
172 files := jsBatchFilesTemplate
173 b := hugolib.TestRunning(t, files, hugolib.TestOptWithOSFs())
174 b.AssertFileContent("public/mybatch/mygroup.js", "Instance 1")
175 b.EditFileReplaceAll("layouts/single.html", "Instance 1", "Instance 1 Edit").Build()
176 b.AssertFileContent("public/mybatch/mygroup.js", "Instance 1 Edit")
177 }
178
179 func TestBatchEditScriptParam(t *testing.T) {
180 files := jsBatchFilesTemplate
181 b := hugolib.TestRunning(t, files, hugolib.TestOptWithOSFs())
182 b.AssertFileContent("public/mybatch/mygroup.js", "param-p1-main")
183 b.EditFileReplaceAll("layouts/single.html", "param-p1-main", "param-p1-main-edited").Build()
184 b.AssertFileContent("public/mybatch/mygroup.js", "param-p1-main-edited")
185 }
186
187 func TestBatchMultiHost(t *testing.T) {
188 files := `
189 -- hugo.toml --
190 disableKinds = ["taxonomy", "term", "section"]
191 [languages]
192 [languages.en]
193 weight = 1
194 baseURL = "https://example.com/en"
195 [languages.fr]
196 weight = 2
197 baseURL = "https://example.com/fr"
198 disableLiveReload = true
199 -- assets/js/styles.css --
200 body {
201 background-color: red;
202 }
203 -- assets/js/main.js --
204 import * as foo from 'mylib';
205 console.log("Hello, Main!");
206 -- assets/js/runner.js --
207 console.log("Hello, Runner!");
208 -- node_modules/mylib/index.js --
209 console.log("Hello, My Lib!");
210 -- layouts/home.html --
211 Home.
212 {{ $batch := (js.Batch "mybatch") }}
213 {{ with $batch.Config }}
214 {{ .SetOptions (dict
215 "params" (dict "id" "config")
216 "sourceMap" ""
217 )
218 }}
219 {{ end }}
220 {{ with (templates.Defer (dict "key" "global")) }}
221 Defer:
222 {{ $batch := (js.Batch "mybatch") }}
223 {{ range $k, $v := $batch.Build.Groups }}
224 {{ range $kk, $vv := . -}}
225 {{ $k }}: {{ .RelPermalink }}
226 {{ end }}
227 {{ end -}}
228 {{ end }}
229 {{ $batch := (js.Batch "mybatch") }}
230 {{ with $batch.Group "mygroup" }}
231 {{ with .Runner "run" }}
232 {{ .SetOptions (dict "resource" (resources.Get "js/runner.js")) }}
233 {{ end }}
234 {{ with .Script "main" }}
235 {{ .SetOptions (dict "resource" (resources.Get "js/main.js") "params" (dict "p1" "param-p1-main" )) }}
236 {{ end }}
237 {{ with .Instance "main" "i1" }}
238 {{ .SetOptions (dict "params" (dict "title" "Instance 1")) }}
239 {{ end }}
240 {{ end }}
241
242
243 `
244 b := hugolib.Test(t, files, hugolib.TestOptWithOSFs())
245 b.AssertPublishDir(
246 "en/mybatch/chunk-MO3Q4WT6.js", "en/mybatch/mygroup.js ",
247 "fr/mybatch/mygroup.js", "fr/mybatch/chunk-MO3Q4WT6.js")
248 }
249
250 func TestBatchRenameBundledScript(t *testing.T) {
251 files := jsBatchFilesTemplate
252 b := hugolib.TestRunning(t, files, hugolib.TestOptWithOSFs())
253 b.AssertFileContent("public/mybatch/p1.js", "P1 Script")
254 b.RenameFile("content/p1/p1script.js", "content/p1/p1script2.js")
255 _, err := b.BuildE()
256 b.Assert(err, qt.IsNotNil)
257 b.Assert(err.Error(), qt.Contains, "resource not set")
258
259 // Rename it back.
260 b.RenameFile("content/p1/p1script2.js", "content/p1/p1script.js")
261 b.Build()
262 }
263
264 func TestBatchMissingImportedAssetIssue13737(t *testing.T) {
265 files := jsBatchFilesTemplate
266 b := hugolib.TestRunning(t, files, hugolib.TestOptWithOSFs())
267 b.AssertFileContent("public/mybatch/mygroup.js", "Hello, Main")
268
269 filename := filepath.Join(b.Cfg.WorkingDir, "assets/js/main.js")
270 content, err := os.ReadFile(filename)
271 b.Assert(err, qt.IsNil)
272 b.Assert(os.Remove(filename), qt.IsNil)
273
274 _, err = b.BuildE()
275 b.Assert(err, qt.IsNotNil)
276 b.Assert(err.Error(), qt.Contains, "failed to read import")
277 b.Assert(err.Error(), qt.Contains, "main.js")
278
279 b.Assert(os.WriteFile(filename, content, 0o666), qt.IsNil)
280 b.Build()
281 b.AssertFileContent("public/mybatch/mygroup.js", "Hello, Main")
282 }
283
284 func TestBatchErrorScriptResourceNotSet(t *testing.T) {
285 files := strings.Replace(jsBatchFilesTemplate, `(resources.Get "js/main.js")`, `(resources.Get "js/doesnotexist.js")`, 1)
286 b, err := hugolib.TestE(t, files, hugolib.TestOptWithOSFs())
287 b.Assert(err, qt.IsNotNil)
288 b.Assert(err.Error(), qt.Contains, `error calling SetOptions: resource not set`)
289 }
290
291 func TestBatchSlashInBatchID(t *testing.T) {
292 files := strings.ReplaceAll(jsBatchFilesTemplate, `"mybatch"`, `"my/batch"`)
293 b, err := hugolib.TestE(t, files, hugolib.TestOptWithOSFs())
294 b.Assert(err, qt.IsNil)
295 b.AssertPublishDir("my/batch/mygroup.js")
296 }
297
298 func TestBatchSourceMaps(t *testing.T) {
299 filesTemplate := `
300 -- hugo.toml --
301 disableKinds = ["taxonomy", "term", "section"]
302 disableLiveReload = true
303 -- assets/js/styles.css --
304 body {
305 background-color: red;
306 }
307 -- assets/js/main.js --
308 import * as foo from 'mylib';
309 console.log("Hello, Main!");
310 -- assets/js/runner.js --
311 console.log("Hello, Runner!");
312 -- node_modules/mylib/index.js --
313 console.log("Hello, My Lib!");
314 -- layouts/_shortcodes/hdx.html --
315 {{ $path := .Get "r" }}
316 {{ $r := or (.Page.Resources.Get $path) (resources.Get $path) }}
317 {{ $batch := (js.Batch "mybatch") }}
318 {{ $scriptID := $path | anchorize }}
319 {{ $instanceID := .Ordinal | string }}
320 {{ $group := .Page.RelPermalink | anchorize }}
321 {{ $params := .Params | default dict }}
322 {{ $export := .Get "export" | default "default" }}
323 {{ with $batch.Group $group }}
324 {{ with .Runner "create-elements" }}
325 {{ .SetOptions (dict "resource" (resources.Get "js/runner.js")) }}
326 {{ end }}
327 {{ with .Script $scriptID }}
328 {{ .SetOptions (dict
329 "resource" $r
330 "export" $export
331 "importContext" (slice $.Page)
332 )
333 }}
334 {{ end }}
335 {{ with .Instance $scriptID $instanceID }}
336 {{ .SetOptions (dict "params" $params) }}
337 {{ end }}
338 {{ end }}
339 hdx-instance: {{ $scriptID }}: {{ $instanceID }}|
340 -- layouts/baseof.html --
341 Base.
342 {{ $batch := (js.Batch "mybatch") }}
343 {{ with $batch.Config }}
344 {{ .SetOptions (dict
345 "params" (dict "id" "config")
346 "sourceMap" ""
347 )
348 }}
349 {{ end }}
350 {{ with (templates.Defer (dict "key" "global")) }}
351 Defer:
352 {{ $batch := (js.Batch "mybatch") }}
353 {{ range $k, $v := $batch.Build.Groups }}
354 {{ range $kk, $vv := . -}}
355 {{ $k }}: {{ .RelPermalink }}
356 {{ end }}
357 {{ end -}}
358 {{ end }}
359 {{ block "main" . }}Main{{ end }}
360 End.
361 -- layouts/single.html --
362 {{ define "main" }}
363 ==> Single Template Content: {{ .Content }}$
364 {{ $batch := (js.Batch "mybatch") }}
365 {{ with $batch.Group "mygroup" }}
366 {{ with .Runner "run" }}
367 {{ .SetOptions (dict "resource" (resources.Get "js/runner.js")) }}
368 {{ end }}
369 {{ with .Script "main" }}
370 {{ .SetOptions (dict "resource" (resources.Get "js/main.js") "params" (dict "p1" "param-p1-main" )) }}
371 {{ end }}
372 {{ with .Instance "main" "i1" }}
373 {{ .SetOptions (dict "params" (dict "title" "Instance 1")) }}
374 {{ end }}
375 {{ end }}
376 {{ end }}
377 -- layouts/home.html --
378 {{ define "main" }}
379 Home.
380 {{ end }}
381 -- content/p1/index.md --
382 ---
383 title: "P1"
384 ---
385
386 Some content.
387
388 {{< hdx r="p1script.js" myparam="p1-param-1" >}}
389 {{< hdx r="p1script.js" myparam="p1-param-2" >}}
390
391 -- content/p1/p1script.js --
392 import * as foo from 'mylib';
393 console.lg("Foo", foo);
394 console.log("P1 Script");
395 export default function P1Script() {};
396
397
398 `
399 files := strings.Replace(filesTemplate, `"sourceMap" ""`, `"sourceMap" "linked"`, 1)
400 b := hugolib.TestRunning(t, files, hugolib.TestOptWithOSFs())
401 b.AssertFileContent("public/mybatch/mygroup.js.map", "main.js", "! ns-hugo")
402 b.AssertFileContent("public/mybatch/mygroup.js", "sourceMappingURL=mygroup.js.map")
403 b.AssertFileContent("public/mybatch/p1.js", "sourceMappingURL=p1.js.map")
404 b.AssertFileContent("public/mybatch/mygroup_run_runner.js", "sourceMappingURL=mygroup_run_runner.js.map")
405 b.AssertFileContent("public/mybatch/chunk-3REXYBSZ.js", "sourceMappingURL=chunk-3REXYBSZ.js.map")
406
407 checkMap := func(p string, expectLen int) {
408 s := b.FileContent(p)
409 sources := esbuild.SourcesFromSourceMap(s)
410 b.Assert(sources, qt.HasLen, expectLen)
411
412 // Check that all source files exist.
413 for _, src := range sources {
414 filename, ok := paths.UrlStringToFilename(src)
415 b.Assert(ok, qt.IsTrue)
416 _, err := os.Stat(filename)
417 b.Assert(err, qt.IsNil)
418 }
419 }
420
421 checkMap("public/mybatch/mygroup.js.map", 1)
422 checkMap("public/mybatch/p1.js.map", 1)
423 checkMap("public/mybatch/mygroup_run_runner.js.map", 0)
424 checkMap("public/mybatch/chunk-3REXYBSZ.js.map", 1)
425 }
426
427 func TestBatchErrorRunnerResourceNotSet(t *testing.T) {
428 files := strings.Replace(jsBatchFilesTemplate, `(resources.Get "js/runner.js")`, `(resources.Get "js/doesnotexist.js")`, 1)
429 b, err := hugolib.TestE(t, files, hugolib.TestOptWithOSFs())
430 b.Assert(err, qt.IsNotNil)
431 b.Assert(err.Error(), qt.Contains, `resource not set`)
432 }
433
434 func TestBatchErrorScriptResourceInAssetsSyntaxError(t *testing.T) {
435 // Introduce JS syntax error in assets/js/main.js
436 files := strings.Replace(jsBatchFilesTemplate, `console.log("Hello, Main!");`, `console.log("Hello, Main!"`, 1)
437 b, err := hugolib.TestE(t, files, hugolib.TestOptWithOSFs())
438 b.Assert(err, qt.IsNotNil)
439 b.Assert(err.Error(), qt.Contains, filepath.FromSlash(`assets/js/main.js:5:0": Expected ")" but found "console"`))
440 }
441
442 func TestBatchErrorScriptResourceInBundleSyntaxError(t *testing.T) {
443 // Introduce JS syntax error in content/p1/p1script.js
444 files := strings.Replace(jsBatchFilesTemplate, `console.log("P1 Script");`, `console.log("P1 Script"`, 1)
445 b, err := hugolib.TestE(t, files, hugolib.TestOptWithOSFs())
446 b.Assert(err, qt.IsNotNil)
447 b.Assert(err.Error(), qt.Contains, filepath.FromSlash(`/content/p1/p1script.js:3:0": Expected ")" but found end of file`))
448 }
449
450 func TestBatch(t *testing.T) {
451 t.Skip("This tests needs npm with internet access")
452 htesting.SkipSlowTestUnlessCI(t)
453 files := `
454 -- hugo.toml --
455 disableKinds = ["taxonomy", "term"]
456 disableLiveReload = true
457 baseURL = "https://example.com"
458 -- package.json --
459 {
460 "devDependencies": {
461 "react": "^18.3.1",
462 "react-dom": "^18.3.1"
463 }
464 }
465 -- assets/js/shims/react.js --
466 -- assets/js/shims/react-dom.js --
467 module.exports = window.ReactDOM;
468 module.exports = window.React;
469 -- content/mybundle/index.md --
470 ---
471 title: "My Bundle"
472 ---
473 -- content/mybundle/mybundlestyles.css --
474 @import './foo.css';
475 @import './bar.css';
476 @import './otherbundlestyles.css';
477
478 .mybundlestyles {
479 background-color: blue;
480 }
481 -- content/mybundle/bundlereact.jsx --
482 import * as React from "react";
483 import './foo.css';
484 import './mybundlestyles.css';
485 window.React1 = React;
486
487 let text = 'Click me, too!'
488
489 export default function MyBundleButton() {
490 return (
491 <button>${text}</button>
492 )
493 }
494
495 -- assets/js/reactrunner.js --
496 import * as ReactDOM from 'react-dom/client';
497 import * as React from 'react';
498
499 export default function Run(group) {
500 for (const module of group.scripts) {
501 for (const instance of module.instances) {
502 /* This is a convention in this project. */
503 let elId = §§${module.id}-${instance.id}§§;
504 let el = document.getElementById(elId);
505 if (!el) {
506 console.warn(§§Element with id ${elId} not found§§);
507 continue;
508 }
509 const root = ReactDOM.createRoot(el);
510 const reactEl = React.createElement(module.mod, instance.params);
511 root.render(reactEl);
512 }
513 }
514 }
515 -- assets/other/otherbundlestyles.css --
516 .otherbundlestyles {
517 background-color: red;
518 }
519 -- assets/other/foo.css --
520 @import './bar.css';
521
522 .foo {
523 background-color: blue;
524 }
525 -- assets/other/bar.css --
526 .bar {
527 background-color: red;
528 }
529 -- assets/js/button.css --
530 button {
531 background-color: red;
532 }
533 -- assets/js/bar.css --
534 .bar-assets {
535 background-color: red;
536 }
537 -- assets/js/helper.js --
538 import './bar.css'
539
540 export function helper() {
541 console.log('helper');
542 }
543
544 -- assets/js/react1styles_nested.css --
545 .react1styles_nested {
546 background-color: red;
547 }
548 -- assets/js/react1styles.css --
549 @import './react1styles_nested.css';
550 .react1styles {
551 background-color: red;
552 }
553 -- assets/js/react1.jsx --
554 import * as React from "react";
555 import './button.css'
556 import './foo.css'
557 import './react1styles.css'
558
559 window.React1 = React;
560
561 let text = 'Click me'
562
563 export default function MyButton() {
564 return (
565 <button>${text}</button>
566 )
567 }
568
569 -- assets/js/react2.jsx --
570 import * as React from "react";
571 import { helper } from './helper.js'
572 import './foo.css'
573
574 window.React2 = React;
575
576 let text = 'Click me, too!'
577
578 export function MyOtherButton() {
579 return (
580 <button>${text}</button>
581 )
582 }
583 -- assets/js/main1.js --
584 import * as React from "react";
585 import * as params from '@params';
586
587 console.log('main1.React', React)
588 console.log('main1.params.id', params.id)
589
590 -- assets/js/main2.js --
591 import * as React from "react";
592 import * as params from '@params';
593
594 console.log('main2.React', React)
595 console.log('main2.params.id', params.id)
596
597 export default function Main2() {};
598
599 -- assets/js/main3.js --
600 import * as React from "react";
601 import * as params from '@params';
602 import * as config from '@params/config';
603
604 console.log('main3.params.id', params.id)
605 console.log('config.params.id', config.id)
606
607 export default function Main3() {};
608
609 -- layouts/single.html --
610 Single.
611
612 {{ $r := .Resources.GetMatch "*.jsx" }}
613 {{ $batch := (js.Batch "mybundle") }}
614 {{ $otherCSS := (resources.Match "/other/*.css").Mount "/other" "." }}
615 {{ with $batch.Config }}
616 {{ $shims := dict "react" "js/shims/react.js" "react-dom/client" "js/shims/react-dom.js" }}
617 {{ .SetOptions (dict
618 "target" "es2018"
619 "params" (dict "id" "config")
620 "shims" $shims
621 )
622 }}
623 {{ end }}
624 {{ with $batch.Group "reactbatch" }}
625 {{ with .Script "r3" }}
626 {{ .SetOptions (dict
627 "resource" $r
628 "importContext" (slice $ $otherCSS)
629 "params" (dict "id" "r3")
630 )
631 }}
632 {{ end }}
633 {{ with .Instance "r3" "r2i1" }}
634 {{ .SetOptions (dict "title" "r2 instance 1")}}
635 {{ end }}
636 {{ end }}
637 -- layouts/home.html --
638 Home.
639 {{ with (templates.Defer (dict "key" "global")) }}
640 {{ $batch := (js.Batch "mybundle") }}
641 {{ range $k, $v := $batch.Build.Groups }}
642 {{ range $kk, $vv := . }}
643 {{ $k }}: {{ $kk }}: {{ .RelPermalink }}
644 {{ end }}
645 {{ end }}
646 {{ end }}
647 {{ $myContentBundle := site.GetPage "mybundle" }}
648 {{ $batch := (js.Batch "mybundle") }}
649 {{ $otherCSS := (resources.Match "/other/*.css").Mount "/other" "." }}
650 {{ with $batch.Group "mains" }}
651 {{ with .Script "main1" }}
652 {{ .SetOptions (dict
653 "resource" (resources.Get "js/main1.js")
654 "params" (dict "id" "main1")
655 )
656 }}
657 {{ end }}
658 {{ with .Script "main2" }}
659 {{ .SetOptions (dict
660 "resource" (resources.Get "js/main2.js")
661 "params" (dict "id" "main2")
662 )
663 }}
664 {{ end }}
665 {{ with .Script "main3" }}
666 {{ .SetOptions (dict
667 "resource" (resources.Get "js/main3.js")
668 )
669 }}
670 {{ end }}
671 {{ with .Instance "main1" "m1i1" }}{{ .SetOptions (dict "params" (dict "title" "Main1 Instance 1"))}}{{ end }}
672 {{ with .Instance "main1" "m1i2" }}{{ .SetOptions (dict "params" (dict "title" "Main1 Instance 2"))}}{{ end }}
673 {{ end }}
674 {{ with $batch.Group "reactbatch" }}
675 {{ with .Runner "reactrunner" }}
676 {{ .SetOptions ( dict "resource" (resources.Get "js/reactrunner.js") )}}
677 {{ end }}
678 {{ with .Script "r1" }}
679 {{ .SetOptions (dict
680 "resource" (resources.Get "js/react1.jsx")
681 "importContext" (slice $myContentBundle $otherCSS)
682 "params" (dict "id" "r1")
683 )
684 }}
685 {{ end }}
686 {{ with .Instance "r1" "i1" }}{{ .SetOptions (dict "params" (dict "title" "Instance 1"))}}{{ end }}
687 {{ with .Instance "r1" "i2" }}{{ .SetOptions (dict "params" (dict "title" "Instance 2"))}}{{ end }}
688 {{ with .Script "r2" }}
689 {{ .SetOptions (dict
690 "resource" (resources.Get "js/react2.jsx")
691 "export" "MyOtherButton"
692 "importContext" $otherCSS
693 "params" (dict "id" "r2")
694 )
695 }}
696 {{ end }}
697 {{ with .Instance "r2" "i1" }}{{ .SetOptions (dict "params" (dict "title" "Instance 2-1"))}}{{ end }}
698 {{ end }}
699
700 `
701
702 b := hugolib.TestRunning(t, files, hugolib.TestOptWithOSFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptWarn())
703
704 b.AssertFileContent("public/index.html",
705 "mains: 0: /mybundle/mains.js",
706 "reactbatch: 2: /mybundle/reactbatch.css",
707 )
708
709 b.AssertFileContent("public/mybundle/reactbatch.css",
710 ".bar {",
711 )
712
713 // Verify params resolution.
714 b.AssertFileContent("public/mybundle/mains.js",
715 `
716 var id = "main1";
717 console.log("main1.params.id", id);
718 var id2 = "main2";
719 console.log("main2.params.id", id2);
720
721
722 # Params from top level config.
723 var id3 = "config";
724 console.log("main3.params.id", void 0);
725 console.log("config.params.id", id3);
726 `)
727
728 b.EditFileReplaceAll("content/mybundle/mybundlestyles.css", ".mybundlestyles", ".mybundlestyles-edit").Build()
729 b.AssertFileContent("public/mybundle/reactbatch.css", ".mybundlestyles-edit {")
730
731 b.EditFileReplaceAll("assets/other/bar.css", ".bar {", ".bar-edit {").Build()
732 b.AssertFileContent("public/mybundle/reactbatch.css", ".bar-edit {")
733
734 b.EditFileReplaceAll("assets/other/bar.css", ".bar-edit {", ".bar-edit2 {").Build()
735 b.AssertFileContent("public/mybundle/reactbatch.css", ".bar-edit2 {")
736 }