Debian Codegraph @

internal/js/esbuild/batch_integration_test.go

raw ◀ ▶
Source reconstructed at patch state 0 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 htesting.SkipSlowTestUnlessCI(t)
452 files := `
453 -- hugo.toml --
454 disableKinds = ["taxonomy", "term"]
455 disableLiveReload = true
456 baseURL = "https://example.com"
457 -- package.json --
458 {
459 "devDependencies": {
460 "react": "^18.3.1",
461 "react-dom": "^18.3.1"
462 }
463 }
464 -- assets/js/shims/react.js --
465 -- assets/js/shims/react-dom.js --
466 module.exports = window.ReactDOM;
467 module.exports = window.React;
468 -- content/mybundle/index.md --
469 ---
470 title: "My Bundle"
471 ---
472 -- content/mybundle/mybundlestyles.css --
473 @import './foo.css';
474 @import './bar.css';
475 @import './otherbundlestyles.css';
476
477 .mybundlestyles {
478 background-color: blue;
479 }
480 -- content/mybundle/bundlereact.jsx --
481 import * as React from "react";
482 import './foo.css';
483 import './mybundlestyles.css';
484 window.React1 = React;
485
486 let text = 'Click me, too!'
487
488 export default function MyBundleButton() {
489 return (
490 <button>${text}</button>
491 )
492 }
493
494 -- assets/js/reactrunner.js --
495 import * as ReactDOM from 'react-dom/client';
496 import * as React from 'react';
497
498 export default function Run(group) {
499 for (const module of group.scripts) {
500 for (const instance of module.instances) {
501 /* This is a convention in this project. */
502 let elId = §§${module.id}-${instance.id}§§;
503 let el = document.getElementById(elId);
504 if (!el) {
505 console.warn(§§Element with id ${elId} not found§§);
506 continue;
507 }
508 const root = ReactDOM.createRoot(el);
509 const reactEl = React.createElement(module.mod, instance.params);
510 root.render(reactEl);
511 }
512 }
513 }
514 -- assets/other/otherbundlestyles.css --
515 .otherbundlestyles {
516 background-color: red;
517 }
518 -- assets/other/foo.css --
519 @import './bar.css';
520
521 .foo {
522 background-color: blue;
523 }
524 -- assets/other/bar.css --
525 .bar {
526 background-color: red;
527 }
528 -- assets/js/button.css --
529 button {
530 background-color: red;
531 }
532 -- assets/js/bar.css --
533 .bar-assets {
534 background-color: red;
535 }
536 -- assets/js/helper.js --
537 import './bar.css'
538
539 export function helper() {
540 console.log('helper');
541 }
542
543 -- assets/js/react1styles_nested.css --
544 .react1styles_nested {
545 background-color: red;
546 }
547 -- assets/js/react1styles.css --
548 @import './react1styles_nested.css';
549 .react1styles {
550 background-color: red;
551 }
552 -- assets/js/react1.jsx --
553 import * as React from "react";
554 import './button.css'
555 import './foo.css'
556 import './react1styles.css'
557
558 window.React1 = React;
559
560 let text = 'Click me'
561
562 export default function MyButton() {
563 return (
564 <button>${text}</button>
565 )
566 }
567
568 -- assets/js/react2.jsx --
569 import * as React from "react";
570 import { helper } from './helper.js'
571 import './foo.css'
572
573 window.React2 = React;
574
575 let text = 'Click me, too!'
576
577 export function MyOtherButton() {
578 return (
579 <button>${text}</button>
580 )
581 }
582 -- assets/js/main1.js --
583 import * as React from "react";
584 import * as params from '@params';
585
586 console.log('main1.React', React)
587 console.log('main1.params.id', params.id)
588
589 -- assets/js/main2.js --
590 import * as React from "react";
591 import * as params from '@params';
592
593 console.log('main2.React', React)
594 console.log('main2.params.id', params.id)
595
596 export default function Main2() {};
597
598 -- assets/js/main3.js --
599 import * as React from "react";
600 import * as params from '@params';
601 import * as config from '@params/config';
602
603 console.log('main3.params.id', params.id)
604 console.log('config.params.id', config.id)
605
606 export default function Main3() {};
607
608 -- layouts/single.html --
609 Single.
610
611 {{ $r := .Resources.GetMatch "*.jsx" }}
612 {{ $batch := (js.Batch "mybundle") }}
613 {{ $otherCSS := (resources.Match "/other/*.css").Mount "/other" "." }}
614 {{ with $batch.Config }}
615 {{ $shims := dict "react" "js/shims/react.js" "react-dom/client" "js/shims/react-dom.js" }}
616 {{ .SetOptions (dict
617 "target" "es2018"
618 "params" (dict "id" "config")
619 "shims" $shims
620 )
621 }}
622 {{ end }}
623 {{ with $batch.Group "reactbatch" }}
624 {{ with .Script "r3" }}
625 {{ .SetOptions (dict
626 "resource" $r
627 "importContext" (slice $ $otherCSS)
628 "params" (dict "id" "r3")
629 )
630 }}
631 {{ end }}
632 {{ with .Instance "r3" "r2i1" }}
633 {{ .SetOptions (dict "title" "r2 instance 1")}}
634 {{ end }}
635 {{ end }}
636 -- layouts/home.html --
637 Home.
638 {{ with (templates.Defer (dict "key" "global")) }}
639 {{ $batch := (js.Batch "mybundle") }}
640 {{ range $k, $v := $batch.Build.Groups }}
641 {{ range $kk, $vv := . }}
642 {{ $k }}: {{ $kk }}: {{ .RelPermalink }}
643 {{ end }}
644 {{ end }}
645 {{ end }}
646 {{ $myContentBundle := site.GetPage "mybundle" }}
647 {{ $batch := (js.Batch "mybundle") }}
648 {{ $otherCSS := (resources.Match "/other/*.css").Mount "/other" "." }}
649 {{ with $batch.Group "mains" }}
650 {{ with .Script "main1" }}
651 {{ .SetOptions (dict
652 "resource" (resources.Get "js/main1.js")
653 "params" (dict "id" "main1")
654 )
655 }}
656 {{ end }}
657 {{ with .Script "main2" }}
658 {{ .SetOptions (dict
659 "resource" (resources.Get "js/main2.js")
660 "params" (dict "id" "main2")
661 )
662 }}
663 {{ end }}
664 {{ with .Script "main3" }}
665 {{ .SetOptions (dict
666 "resource" (resources.Get "js/main3.js")
667 )
668 }}
669 {{ end }}
670 {{ with .Instance "main1" "m1i1" }}{{ .SetOptions (dict "params" (dict "title" "Main1 Instance 1"))}}{{ end }}
671 {{ with .Instance "main1" "m1i2" }}{{ .SetOptions (dict "params" (dict "title" "Main1 Instance 2"))}}{{ end }}
672 {{ end }}
673 {{ with $batch.Group "reactbatch" }}
674 {{ with .Runner "reactrunner" }}
675 {{ .SetOptions ( dict "resource" (resources.Get "js/reactrunner.js") )}}
676 {{ end }}
677 {{ with .Script "r1" }}
678 {{ .SetOptions (dict
679 "resource" (resources.Get "js/react1.jsx")
680 "importContext" (slice $myContentBundle $otherCSS)
681 "params" (dict "id" "r1")
682 )
683 }}
684 {{ end }}
685 {{ with .Instance "r1" "i1" }}{{ .SetOptions (dict "params" (dict "title" "Instance 1"))}}{{ end }}
686 {{ with .Instance "r1" "i2" }}{{ .SetOptions (dict "params" (dict "title" "Instance 2"))}}{{ end }}
687 {{ with .Script "r2" }}
688 {{ .SetOptions (dict
689 "resource" (resources.Get "js/react2.jsx")
690 "export" "MyOtherButton"
691 "importContext" $otherCSS
692 "params" (dict "id" "r2")
693 )
694 }}
695 {{ end }}
696 {{ with .Instance "r2" "i1" }}{{ .SetOptions (dict "params" (dict "title" "Instance 2-1"))}}{{ end }}
697 {{ end }}
698
699 `
700
701 b := hugolib.TestRunning(t, files, hugolib.TestOptWithOSFs(), hugolib.TestOptWithNpmInstall(), hugolib.TestOptWarn())
702
703 b.AssertFileContent("public/index.html",
704 "mains: 0: /mybundle/mains.js",
705 "reactbatch: 2: /mybundle/reactbatch.css",
706 )
707
708 b.AssertFileContent("public/mybundle/reactbatch.css",
709 ".bar {",
710 )
711
712 // Verify params resolution.
713 b.AssertFileContent("public/mybundle/mains.js",
714 `
715 var id = "main1";
716 console.log("main1.params.id", id);
717 var id2 = "main2";
718 console.log("main2.params.id", id2);
719
720
721 # Params from top level config.
722 var id3 = "config";
723 console.log("main3.params.id", void 0);
724 console.log("config.params.id", id3);
725 `)
726
727 b.EditFileReplaceAll("content/mybundle/mybundlestyles.css", ".mybundlestyles", ".mybundlestyles-edit").Build()
728 b.AssertFileContent("public/mybundle/reactbatch.css", ".mybundlestyles-edit {")
729
730 b.EditFileReplaceAll("assets/other/bar.css", ".bar {", ".bar-edit {").Build()
731 b.AssertFileContent("public/mybundle/reactbatch.css", ".bar-edit {")
732
733 b.EditFileReplaceAll("assets/other/bar.css", ".bar-edit {", ".bar-edit2 {").Build()
734 b.AssertFileContent("public/mybundle/reactbatch.css", ".bar-edit2 {")
735 }