#Import TS modules to a script with define[colon]vars set

9 messages · Page 1 of 1 (latest)

quaint fractal
#

Hello, I am attempting to import some typescript modules to a client-side script with define:vars set. This gives me Uncaught SyntaxError: Cannot use import statement outside a module, which makes sense, given define:vars forces the client side script to look like:

(function(){
    const foo = 'bar';
    const baz = 'etc';
    import { something } from './somewhere';
    // ...
})();```

My question is, how do I solve this? The modules in question are not JSON serialisable thus I cannot pass them through with define:vars, https://docs.astro.build/en/reference/directives-reference/#definevars
Astro Documentation

Build faster websites with less client-side Javascript.

#

left gives error, right does not

#

I attempted to solve by moving the import into its own script tag but this did not work

#

to be clear; the above solution gives a different error

uncut heath
#

Hey, please read the caution in their docs. Your script is inlined as is, so you may better choose different approach. Avoid define:vars if possible.

toxic mirage
#

yeah, define:vars not recommended, but sometimes it's convenient to avoid copying, can you try with type="module" ?

<script define:vars={{url,uid,isSvg,baseUrl}} type="module">
quaint fractal
#

Hello, apologies for not seeing this earlier as I was away, I believe I tried type="module" to no avail, but ended up fixing it by moving the if(point) conditional to the id definition, but thank you for the suggestions.

#

Is the correct etiquette to close this post now, or is there a way to mark it as solved?

#

I suppose in a sense it hasnt been solved because I still wasnt able to import the module with define:vars set, but it is solved in that this does not present a problem for the program