Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an IIFE to scope define:vars scripts #599

Merged
merged 2 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/seven-nails-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Use an IIFE for define:vars scripts
4 changes: 2 additions & 2 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (p *printer) printDefineVarsOpen(n *astro.Node) {
}
if n.DataAtom == atom.Script {
if !isTypeModuleScript(n) {
p.print("{")
p.print("(function(){")
}
}
for _, attr := range n.Attr {
Expand Down Expand Up @@ -211,7 +211,7 @@ func (p *printer) printDefineVarsClose(n *astro.Node) {
return
}
if !isTypeModuleScript(n) {
p.print("}")
p.print("})();")
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,15 +1148,15 @@ import Widget2 from '../components/Widget2.astro';`},
staticExtraction: true,
source: `<script define:vars={{ value: 0 }}>console.log(value);</script>`,
want: want{
code: `<script>{${$$defineScriptVars({ value: 0 })}console.log(value);}</script>`,
code: `<script>(function(){${$$defineScriptVars({ value: 0 })}console.log(value);})();</script>`,
},
},
{
name: "script define:vars II",
staticExtraction: true,
source: `<script define:vars={{ "dash-case": true }}>console.log(dashCase);</script>`,
want: want{
code: `<script>{${$$defineScriptVars({ "dash-case": true })}console.log(dashCase);}</script>`,
code: `<script>(function(){${$$defineScriptVars({ "dash-case": true })}console.log(dashCase);})();</script>`,
},
},
{
Expand Down Expand Up @@ -2257,7 +2257,7 @@ const items = ["Dog", "Cat", "Platipus"];
source: `<script is:inline>var one = 'one';</script><script>var two = 'two';</script><script define:vars={{foo:'bar'}}>var three = foo;</script><script is:inline define:vars={{foo:'bar'}}>var four = foo;</script>`,
staticExtraction: true,
want: want{
code: `<script>var one = 'one';</script>${$$maybeRenderHead($$result)}<script>{${$$defineScriptVars({foo:'bar'})}var three = foo;}</script><script>{${$$defineScriptVars({foo:'bar'})}var four = foo;}</script>`,
code: `<script>var one = 'one';</script>${$$maybeRenderHead($$result)}<script>(function(){${$$defineScriptVars({foo:'bar'})}var three = foo;})();</script><script>(function(){${$$defineScriptVars({foo:'bar'})}var four = foo;})();</script>`,
metadata: metadata{
hoisted: []string{"{ type: 'inline', value: `var two = 'two';` }"},
},
Expand Down