#Importing rollup bundle via script tag?

72 messages · Page 1 of 1 (latest)

dim path
#

Hey there,
what do I need to do to proplerly import a module into html using a script tag after I've bundled all the scripts together using rollup? I'm loading the bundle via the html script tag as as module, but all my namespaces appear to be gone, and I have no idea how to fix this.

Any idea what I might be doing wrong? I'm transpiling several typescript files to JS, and bundling those together using the rollup typescript plugin.

kind basalt
#

the main purpose of bundling is to produce a single script that contains your application logic plus all of its dependencies

dim path
#

stackoverflow said that I should use a bundler but this has only caused me issues so far

kind basalt
#

yeah, that's like saying "i have a nail sticking up on my deck" and someone suggesting "buy a steamroller" to fix it

dim path
#

so what should I be doing then?

kind basalt
#

the simplest thing would be to add extensions to your import specifiers

#

but i know some people find that very upsetting for reasons i don't understand

dim path
kind basalt
#

no. tsc never touches your import specifiers. like almost everything outside of types in TS, the resulting JS is the same as what you write in your source code

dim path
#

I mean, will ts change a .ts to a .js extension?

kind basalt
#

no. you write import { blah } from './foo.js' and what's what you get in your output

#

in your codebase you might have a file named foo.ts that would get compiled to foo.js

dim path
#

ugh I hope that won't mess with my language server

kind basalt
#

what editor?

#

anything that uses tsserver will be fine

dim path
#

typescript-language-server

kind basalt
#

looks like that's just a wrapper for tsserver, so yeah should be fine

#

this is the standard way to write typescript targeting ESM, so any non-broken dev setup should be happy with it

dim path
#

alright, thanks, that worked!

I'm now stuck with Cannot use import statement outside a module though

#

I was hoping rollup would deal with that as well

kind basalt
#

what's the context?

dim path
#

well it's <script src="./lib.js">

kind basalt
#

you may need a type="module" attribute on the relevant <script> tag?

#

yeah, you have to tell browsers to interpret <script>s as ESM

dim path
kind basalt
#

it's been a while since i've done this kind of frontend stuff, but if you mean like referring to stuff from onclick attributes or whatever then yeah. stuff in modules aren't part of the global scope

dim path
#

yeah that's quite annoying
rollup had a way of dealing with that, but since that's not working I have no idea

kind basalt
#

well aside from the import specifiers, there may be other reasons to bundle your code (and emit something that's not a module). that can often lead to better performance because bundlers can see "the whole picture" and strip out dead code, and also the client ends up having to make fewer requests

#

but if you do that then i would go all-in on the bundler approach and try to use a single script from your frontend

#

if you don't want to do that and instead want to deliver modules, i'd have to do some research but i'm sure there's a way to get exports into the global scope

dim path
kind basalt
#

sourcemaps can help there

kind basalt
#

if that works the way i think it will, sounds kinda nice to have an explicit listing of globals, rather than being able to access absolutely everything like you could in pre-module JS (which could lead to lots of accidental coupling)

#

like i said though i haven't done frontend stuff in a while, so i don't entirely know what i'm talking about

dim path
dim path
kind basalt
#

uh webdev is annoying for a lot of reasons yeah, but what you're hoping to do is kind of a contradiction: modules are (as the name implies) meant to modularize your code. the whole point is to not pollute the global scope. if you specifically don't want that, then don't use modules

dim path
kind basalt
#

you can have as many <script> elements as you want, all referring to different .js files. without type="module" you can think of them as kinda getting all concatenated together into one big imaginary global script

#

and attributes like onclick whose values are JS strings are also evaluated in that same global scope

dim path
#

but how would I then be able to talk between script files?

#

without moving all my code into the html script tags

kind basalt
#

you can access variables willy-nilly across scripts

#

not sure if that's exactly what you're asking though

dim path
#

I mean, it kinda is, but I don't think the language server will play nicely with that

kind basalt
#

TS configured properly can recognize files as scripts rather than modules

#

but i'm mostly speaking in terms of the execution environment here, not how you emit those scripts from your source code

#

another angle which may not be fashionable these days: back when i did do more frontend stuff eons ago there was a push towards "unobtrusive JS". you wouldn't tie your DOM to specific JS via onclick attributes and the like. instead you'd go the other way and query the DOM from your JS and attach event handlers there. so the HTML document itself doesn't have to know anything at all about your JS

#

you'd use class/id attributes and the DOM hierarchy to find the relevant elements from the JS side, and if you need to attach state to the DOM you can write to data-* attributes and similar from JS

#

i get the impression that these days most frontend folks use a giant stack of abstractions like virtual DOMs via libraries like react/vue/etc though, and most people don't even care about what's actually going on under the hood

dim path
dim path
kind basalt
#

what kind of site are you building? would you say it's more of a "web application" or more like a traditional "document"?

dim path
dim path
kind basalt
kind basalt
dim path
#

would definitely be a sane choice but I don't think I'm ready to dive into yet another new environment again
I was trying to avoid webdev anyway haha

kind basalt
#

what's your comfort zone? many other stacks have compile-to-JS (with varying degrees of support for also creating the UIs themselves in a non-webby-way)

#

like i've toyed with Scala.js in the past, for example

#

(feels weird to recommend this kind of approach on a typescript discord server, heh)

dim path
kind basalt
#

oh, just backend/non-browser-runtimes then?

dim path
dim path
kind basalt
#

yeah, that's my happy place too these days. when i actually need to build webpages i try to stick to mostly pure HTML/CSS with as little JS as possible

#

(CSS can be its own special hell though)

dim path
#

CSS scares me haha
but I'm slowly getting used to it

#

but alright, I'll close this then, thanks again!