Skip to content

Commit 29cddb7

Browse files
committed
Allow providing multiple BeforeScript and AfterScript
Summary: Previsouly this code would allow only a single script to run: ``` r.Get("/swagger/*", httpSwagger.Handler( httpSwagger.BeforeScript("console.log('before 1')"), httpSwagger.BeforeScript("console.log('before 2')"), httpSwagger.URL("http://localhost:1323/swagger/doc.json"), //The url pointing to API definition )) // Only 'before 2' will be printed ``` Now both scripts are run: ``` 'before 1' 'before 2' ``` This improves behavior of BeforeScript and AfterScript. If users want to provide multiple scripts, they are forced to combine them using strings. Test Plan: Updated existing example and tests. Added new test TestMultipleScripts
1 parent c8d62bf commit 29cddb7

File tree

8 files changed

+54
-42
lines changed

8 files changed

+54
-42
lines changed

example/go-chi/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ require (
66
github.com/go-chi/chi v4.1.2+incompatible
77
github.com/swaggo/http-swagger v1.2.6
88
github.com/swaggo/swag v1.8.1
9-
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
10-
golang.org/x/tools v0.1.10 // indirect
119
)
10+
11+
replace github.com/swaggo/http-swagger => ../..

example/go-chi/go.sum

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
22
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
33
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
4-
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
54
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
6-
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
75
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
86
github.com/agiledragon/gomonkey/v2 v2.3.1 h1:k+UnUY0EMNYUFUAQVETGY9uUTxjMdnUkP0ARyJS1zzs=
97
github.com/agiledragon/gomonkey/v2 v2.3.1/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
@@ -18,10 +16,12 @@ github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxm
1816
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
1917
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
2018
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
21-
github.com/go-openapi/jsonreference v0.19.6 h1:UBIxjkht+AWIgYzCDSv2GN+E/togfwXUJFRTWhl2Jjs=
2219
github.com/go-openapi/jsonreference v0.19.6/go.mod h1:diGHMEHg2IqXZGKxqyvWdfWU/aim5Dprw5bqpKkTvns=
23-
github.com/go-openapi/spec v0.20.4 h1:O8hJrt0UMnhHcluhIdUgCLRWyM2x7QkBXRvOs7m+O1M=
20+
github.com/go-openapi/jsonreference v0.20.0 h1:MYlu0sBgChmCfJxxUKZ8g1cPWFOB37YSZqewK7OKeyA=
21+
github.com/go-openapi/jsonreference v0.20.0/go.mod h1:Ag74Ico3lPc+zR+qjn4XBUmXymS4zJbYVCZmcgkasdo=
2422
github.com/go-openapi/spec v0.20.4/go.mod h1:faYFR1CvsJZ0mNsmsphTMSoRrNV3TEDoAM7FOEWeq8I=
23+
github.com/go-openapi/spec v0.20.6 h1:ich1RQ3WDbfoeTqTAb+5EIxNmpKVJZWBNah9RAT0jIQ=
24+
github.com/go-openapi/spec v0.20.6/go.mod h1:2OpW+JddWPrpXSCIX8eOx7lZ5iyuWj3RYR6VaaBKcWA=
2525
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
2626
github.com/go-openapi/swag v0.19.15 h1:D2NRCBzS9/pEY3gP9Nl8aDqGUcPFrwG2p+CNFrLyrCM=
2727
github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
@@ -59,11 +59,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
5959
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
6060
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
6161
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
62-
github.com/swaggo/files v0.0.0-20210815190702-a29dd2bc99b2 h1:+iNTcqQJy0OZ5jk6a5NLib47eqXK8uYcPX+O4+cBpEM=
63-
github.com/swaggo/files v0.0.0-20210815190702-a29dd2bc99b2/go.mod h1:lKJPbtWzJ9JhsTN1k1gZgleJWY/cqq0psdoMmaThG3w=
64-
github.com/swaggo/http-swagger v1.2.6 h1:ihTjChUoSRMpFMjWw+0AkL1Ti4r6v8pCgVYLmQVRlRw=
65-
github.com/swaggo/http-swagger v1.2.6/go.mod h1:CcoICgY3yVDk2u1LQUCMHbAj0fjlxIX+873psXlIKNA=
66-
github.com/swaggo/swag v1.7.9/go.mod h1:gZ+TJ2w/Ve1RwQsA2IRoSOTidHz6DX+PIG8GWvbnoLU=
62+
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe h1:K8pHPVoTgxFJt1lXuIzzOX7zZhZFldJQK/CgKx9BFIc=
63+
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe/go.mod h1:lKJPbtWzJ9JhsTN1k1gZgleJWY/cqq0psdoMmaThG3w=
6764
github.com/swaggo/swag v1.8.1 h1:JuARzFX1Z1njbCGz+ZytBR15TFJwF2Q7fu8puJHhQYI=
6865
github.com/swaggo/swag v1.8.1/go.mod h1:ugemnJsPZm/kRwFUnzBlbHRd0JY9zE1M4F+uy2pAaPQ=
6966
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
@@ -101,7 +98,6 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX
10198
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
10299
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
103100
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
104-
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
105101
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
106102
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
107103
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=

example/go-chi/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ func main() {
2727
r := chi.NewRouter()
2828

2929
r.Get("/swagger/*", httpSwagger.Handler(
30+
httpSwagger.BeforeScript("console.log('before 1')"),
31+
httpSwagger.BeforeScript("console.log('before 2')"),
3032
httpSwagger.URL("http://localhost:1323/swagger/doc.json"), //The url pointing to API definition
3133
))
3234

35+
log.Println("Listening :1323")
3336
log.Fatal(http.ListenAndServe(":1323", r))
3437
}

example/gorilla/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ require (
66
github.com/gorilla/mux v1.8.0
77
github.com/swaggo/http-swagger v1.2.6
88
github.com/swaggo/swag v1.8.1
9-
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
10-
golang.org/x/tools v0.1.10 // indirect
119
)
10+
11+
replace github.com/swaggo/http-swagger => ../..

example/gorilla/go.sum

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
22
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
33
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
4-
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
54
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
6-
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
75
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
86
github.com/agiledragon/gomonkey/v2 v2.3.1 h1:k+UnUY0EMNYUFUAQVETGY9uUTxjMdnUkP0ARyJS1zzs=
97
github.com/agiledragon/gomonkey/v2 v2.3.1/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
@@ -16,10 +14,12 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
1614
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
1715
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
1816
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
19-
github.com/go-openapi/jsonreference v0.19.6 h1:UBIxjkht+AWIgYzCDSv2GN+E/togfwXUJFRTWhl2Jjs=
2017
github.com/go-openapi/jsonreference v0.19.6/go.mod h1:diGHMEHg2IqXZGKxqyvWdfWU/aim5Dprw5bqpKkTvns=
21-
github.com/go-openapi/spec v0.20.4 h1:O8hJrt0UMnhHcluhIdUgCLRWyM2x7QkBXRvOs7m+O1M=
18+
github.com/go-openapi/jsonreference v0.20.0 h1:MYlu0sBgChmCfJxxUKZ8g1cPWFOB37YSZqewK7OKeyA=
19+
github.com/go-openapi/jsonreference v0.20.0/go.mod h1:Ag74Ico3lPc+zR+qjn4XBUmXymS4zJbYVCZmcgkasdo=
2220
github.com/go-openapi/spec v0.20.4/go.mod h1:faYFR1CvsJZ0mNsmsphTMSoRrNV3TEDoAM7FOEWeq8I=
21+
github.com/go-openapi/spec v0.20.6 h1:ich1RQ3WDbfoeTqTAb+5EIxNmpKVJZWBNah9RAT0jIQ=
22+
github.com/go-openapi/spec v0.20.6/go.mod h1:2OpW+JddWPrpXSCIX8eOx7lZ5iyuWj3RYR6VaaBKcWA=
2323
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
2424
github.com/go-openapi/swag v0.19.15 h1:D2NRCBzS9/pEY3gP9Nl8aDqGUcPFrwG2p+CNFrLyrCM=
2525
github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
@@ -59,11 +59,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
5959
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
6060
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
6161
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
62-
github.com/swaggo/files v0.0.0-20210815190702-a29dd2bc99b2 h1:+iNTcqQJy0OZ5jk6a5NLib47eqXK8uYcPX+O4+cBpEM=
63-
github.com/swaggo/files v0.0.0-20210815190702-a29dd2bc99b2/go.mod h1:lKJPbtWzJ9JhsTN1k1gZgleJWY/cqq0psdoMmaThG3w=
64-
github.com/swaggo/http-swagger v1.2.6 h1:ihTjChUoSRMpFMjWw+0AkL1Ti4r6v8pCgVYLmQVRlRw=
65-
github.com/swaggo/http-swagger v1.2.6/go.mod h1:CcoICgY3yVDk2u1LQUCMHbAj0fjlxIX+873psXlIKNA=
66-
github.com/swaggo/swag v1.7.9/go.mod h1:gZ+TJ2w/Ve1RwQsA2IRoSOTidHz6DX+PIG8GWvbnoLU=
62+
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe h1:K8pHPVoTgxFJt1lXuIzzOX7zZhZFldJQK/CgKx9BFIc=
63+
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe/go.mod h1:lKJPbtWzJ9JhsTN1k1gZgleJWY/cqq0psdoMmaThG3w=
6764
github.com/swaggo/swag v1.8.1 h1:JuARzFX1Z1njbCGz+ZytBR15TFJwF2Q7fu8puJHhQYI=
6865
github.com/swaggo/swag v1.8.1/go.mod h1:ugemnJsPZm/kRwFUnzBlbHRd0JY9zE1M4F+uy2pAaPQ=
6966
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
@@ -101,7 +98,6 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX
10198
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
10299
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
103100
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
104-
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
105101
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
106102
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
107103
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=

example/gorilla/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func main() {
3030
httpSwagger.URL("http://localhost:1323/swagger/doc.json"), //The url pointing to API definition
3131
httpSwagger.DeepLinking(true),
3232
httpSwagger.DocExpansion("none"),
33-
httpSwagger.DomID("#swagger-ui"),
33+
httpSwagger.DomID("swagger-ui"),
3434
)).Methods(http.MethodGet)
3535

3636
log.Fatal(http.ListenAndServe(":1323", r))

swagger.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ type Config struct {
2121
DocExpansion string
2222
DomID string
2323
InstanceName string
24-
BeforeScript template.JS
25-
AfterScript template.JS
24+
BeforeScripts []template.JS
25+
AfterScripts []template.JS
2626
Plugins []template.JS
2727
UIConfig map[template.JS]template.JS
2828
DeepLinking bool
@@ -50,7 +50,7 @@ func DocExpansion(docExpansion string) func(*Config) {
5050
}
5151
}
5252

53-
// DomID #swagger-ui.
53+
// DomID #swagger-ui, without #.
5454
func DomID(domID string) func(*Config) {
5555
return func(c *Config) {
5656
c.DomID = domID
@@ -98,15 +98,15 @@ func UIConfig(props map[string]string) func(*Config) {
9898
// BeforeScript holds JavaScript to be run right before the Swagger UI object is created.
9999
func BeforeScript(js string) func(*Config) {
100100
return func(c *Config) {
101-
c.BeforeScript = template.JS(js)
101+
c.BeforeScripts = append(c.BeforeScripts, template.JS(js))
102102
}
103103
}
104104

105105
// AfterScript holds JavaScript to be run right after the Swagger UI object is created
106106
// and set on the window.
107107
func AfterScript(js string) func(*Config) {
108108
return func(c *Config) {
109-
c.AfterScript = template.JS(js)
109+
c.AfterScripts = append(c.AfterScripts, template.JS(js))
110110
}
111111
}
112112

@@ -261,8 +261,8 @@ const indexTempl = `<!-- HTML for static distribution bundle build -->
261261
<script src="./swagger-ui-standalone-preset.js"> </script>
262262
<script>
263263
window.onload = function() {
264-
{{- if .BeforeScript}}
265-
{{.BeforeScript}}
264+
{{- range $script := .BeforeScripts }}
265+
{{$script}}
266266
{{- end}}
267267
// Build a system
268268
const ui = SwaggerUIBundle({
@@ -289,8 +289,8 @@ window.onload = function() {
289289
})
290290
291291
window.ui = ui
292-
{{- if .AfterScript}}
293-
{{.AfterScript}}
292+
{{- range $script := .AfterScripts }}
293+
{{$script}}
294294
{{- end}}
295295
}
296296
</script>

swagger_test.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ func TestConfigURL(t *testing.T) {
212212
{
213213
desc: "configure BeforeScript",
214214
exp: &Config{
215-
BeforeScript: `const SomePlugin = (system) => ({
215+
BeforeScripts: []template.JS{`const SomePlugin = (system) => ({
216216
// Some plugin
217-
});`,
217+
});`},
218218
},
219219
cfgfn: BeforeScript(`const SomePlugin = (system) => ({
220220
// Some plugin
@@ -223,9 +223,9 @@ func TestConfigURL(t *testing.T) {
223223
{
224224
desc: "configure AfterScript",
225225
exp: &Config{
226-
AfterScript: `const SomePlugin = (system) => ({
226+
AfterScripts: []template.JS{`const SomePlugin = (system) => ({
227227
// Some plugin
228-
});`,
228+
});`},
229229
},
230230
cfgfn: AfterScript(`const SomePlugin = (system) => ({
231231
// Some plugin
@@ -242,6 +242,23 @@ func TestConfigURL(t *testing.T) {
242242
}
243243
}
244244

245+
func TestMultipleScripts(t *testing.T) {
246+
router := http.NewServeMux()
247+
248+
router.Handle("/", Handler(
249+
BeforeScript("before 1"),
250+
BeforeScript("before 2"),
251+
AfterScript("after 1"),
252+
AfterScript("after 2"),
253+
))
254+
255+
w1 := performRequest(http.MethodGet, "/index.html", router)
256+
assert.Contains(t, w1.Body.String(), "before 1")
257+
assert.Contains(t, w1.Body.String(), "before 2")
258+
assert.Contains(t, w1.Body.String(), "after 1")
259+
assert.Contains(t, w1.Body.String(), "after 2")
260+
}
261+
245262
func TestUIConfigOptions(t *testing.T) {
246263

247264
type fixture struct {
@@ -367,14 +384,14 @@ func TestUIConfigOptions(t *testing.T) {
367384
PersistAuthorization: true,
368385
DocExpansion: "none",
369386
DomID: "swagger-ui-id",
370-
BeforeScript: `const SomePlugin = (system) => ({
387+
BeforeScripts: []template.JS{`const SomePlugin = (system) => ({
371388
// Some plugin
372389
});
373-
`,
374-
AfterScript: `const someOtherCode = function(){
390+
`},
391+
AfterScripts: []template.JS{`const someOtherCode = function(){
375392
// Do something
376393
};
377-
someOtherCode();`,
394+
someOtherCode();`},
378395
Plugins: []template.JS{
379396
"SomePlugin",
380397
"AnotherPlugin",

0 commit comments

Comments
 (0)