By
Raqueebuddin Aziz
August 4, 2023
Freelance Web Designer & Developer
By
August 4, 2023
Freelance Web Designer & Developer
This guide assumes you are familiar with how JS scopes and modules work and have completed the svelte tutorial.
This is a simple hello world svelte component that you and I will be walking through to see how script context=module
works.
<!-- HelloWorld.svelte -->
<!-- OUTER SCOPE STARTS -->
<script context="module">
import { writable } from 'svelte/store'
let message = 'world!'
const count = writable(0)
setInterval(() => {
count.update(c => c + 1)
}, 1000)
</script>
<!-- INNER SCOPE STARTS -->
<script>
$: console.log($count)
</script>
<p>hello {message}</p>
<button on:click={() => $count = 0}>Reset Count</button>
<!-- INNER SCOPE ENDS -->
<!-- OUTER SCOPE ENDS -->
Let’s see what a svelte component roughly would look like if it was written in JS to understand how context=module
script works.
// HelloWorld.js
// OUTER SCOPE STARTS
import { writable } from 'svelte/store'
let message = 'world!'
const count = writable(0)
setInterval(() => {
count.update((c) => c + 1)
}, 1000)
// @SVELTE-COMPILE
export const HelloWorld = () => {
// INNER SCOPE STARTS
$: console.log($count)
return html`
<p>hello ${message}</p>
<button on:click="${() => ($count = 0)}">Reset Count</button>
`
// INNER SCOPE ENDS
}
export default HelloWorld
// OUTER SCOPE ENDS
context=module
scripts only runs once, no matter how many times you use a component.context=module
script tag. You can do it the other way round though.$
to their name or creating reactive variables using let
in context=module
script tags.Because the global scope (context=module
in svelte files) is only run on the first import of a module, but the component function (the normal script tag and the html) is executed every time a component is used.
Look at the svelte example code above and then compare the scopes to the js example code above.
See how all the non context=module
script variables are inside the inner scope aka inside the function thus variables defined inside a function cannot be used outside it.
The outside of the function in the .js
file corresponding to the context=module
script in the .svelte
file.
You would have noticed that you cannot use any svelte specific syntax in context=module
scripts like $: console.log($count)
, setInterval(() => $count++, 1000)
.
Instead we have to write pure JS inside context=module
scripts.
The reason for this is everything inside the normal script tag inside a svelte component runs through the svelte compiler to transform the svelte specific syntax into pure JS syntax.
Look at the @SVELTE-COMPILE
line in js example code above. This compilation doesn’t happen for the context=module
script.
The next question you might have is why not run the outer scope (context=module
script) through a compiler too and let you use the svelte specific syntax in context=module
scripts? Some of the reasons are
context=module
not supporting svelte syntax is great.
For e.g. if you want to define a variable that’s not reactive you can do that in context=module
scripts using the let x = 1
syntax.The context=module
script tag is an escape hatch so you can break out into the global scope of the current module as with normal script tags you are contained within a function scope and cannot do things you can do in a global scope like exporting variables and functions, running code once per component and sharing state across multiple usage of the same component.
Leave a comment down below if you have any questions!
Get a gorgeous website at just $99/month.
30 DAY FREE TRIAL
We use cookies to enhance your browsing experience and analyze website traffic. By continuing to use our website, you consent to the use of cookies in accordance with our Privacy Policy.