#JS does not get build in some case

7 messages · Page 1 of 1 (latest)

karmic lance
#

In this test, JS is build and bundle, I can verify the hello() is exist in the bundle.

page/test1.astro

<script>
function hello() {
    alert("hello")
}
hello()
</script>

JS doesn't work get build, no trace of hello() found in the bundle

<Layout>
<input onclick="hello()">
</Layout>
<script>
function hello() {
alert("hello")
}
</script>
``

echo ibex
#

You can either bind that event to the input element or make the function available in the global scope meaning using is:inline

#

javascript inside script tags in astro are modules so they're not avaible to use by onclick and similar which looks for global functions

#

The hello function is getting treeshaken because the bundler doesn't see it used

karmic lance
#

is:inline I see is scoped to the current page.

That will mean addEventLisener is the only option?

echo ibex
#

Yup pretty much

#

Or maybe attaching the function to the window object