ES6 import

19 views
Skip to first unread message

Milan Košir

unread,
Feb 28, 2021, 9:31:08 AM2/28/21
to dev
Hi, first of all thank you for this amazing library!
I would like to use it in vitepress project which is built on top of vite, so I'm limited to ES6 imports.
Can somebody give me help how to import hypothesis client library (for my webpage) as ES6 module?
This:
<script src="https://hypothes.is/embed.js" async type="module"></script>
doesn't work for me. 
Thank you in advance for any hint!

Milan Košir

unread,
Feb 28, 2021, 9:54:12 AM2/28/21
to dev, Milan Košir
I also tried to import hypothesis client in main.js like this:
import './js/hypothesis-client/embed'
but it doesn't work... I think it is because hypothesis has no exports defined.
Thanks for any help, I'm not really js expert, as you can see :)

Milan Košir

unread,
Feb 28, 2021, 3:38:46 PM2/28/21
to dev
Just a note: I gave up on importing hypothesis in vitepress.

Robert Knight

unread,
Mar 1, 2021, 3:51:32 AM3/1/21
to Milan Košir, dev
Hello Milan,

As you've found out, you can't add Hypothesis to a web page by `import`-ing it. The https://hypothes.is/embed.js script is conceptually an application that is run rather than a library that you call. The correct approach is to create an ordinary non-module `<script>` element and add it to the page:

```
const script = document.createElement('script');
script.src = 'https://hypothes.is/embed.js'
document.body.appendChild(script);
```

I don't know anything about Vite or Vitepress, but since you have a `main.js` file where you can execute JavaScript, I would expect you can run something like the above code.

Kind Regards,
Robert.

--
You received this message because you are subscribed to the Google Groups "dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dev+uns...@list.hypothes.is.
To view this discussion on the web visit https://groups.google.com/a/list.hypothes.is/d/msgid/dev/fb836ad5-99cd-466d-a834-95a3f15ca0a5n%40list.hypothes.is.

Milan Košir

unread,
Mar 1, 2021, 11:53:38 AM3/1/21
to Robert Knight, dev
Thank you Robert!
Yes, somehow I understood that from beginning, but wanted to be smart :) 
Vite relies on latest JS functionality straight from the browser (Native-ES-Module), so it's just not compatible with hypothesis.

Maybe "library-fying" your client script would open some new possibilities... but I can't even imagine what it all entails...
Thank you again for this amazing tool!

Best regards to all hypothes.is team,
Milan
Reply all
Reply to author
Forward
0 new messages