Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JavaScript libraries with Sapper/Svelte?

Tags:

svelte

Using Sappers export feature to build a static site, I would love to be able to use JavaScript libraries like Conversational Form and GSAP. Trying to add them to client.js or my components, I can't access the window object.

How do I best approach this?

like image 859
Jonas K. Avatar asked Mar 20 '18 16:03

Jonas K.


People also ask

What is Sapper Javascript?

Sapper is a framework for building web applications of all sizes, with a beautiful development experience and flexible filesystem-based routing.

Is Svelte a framework or library?

Svelte is a tool for building web applications that is an alternative to currently popular web frameworks like React, Vue, and Angular. Svelte is a web application compiler, not a library.

What is routing in sapper?

Server routes are Node-based API/REST services. Sapper supports implementing these and it hosts them so the client-side code can send requests to them. It enables collocating server-side code with client-side code in the same project.


1 Answers

The standard way is to import those libraries into your components:

<script>
  import { TweenMax, Power2, TimelineLite } from 'gsap';

  export default {
    oncreate() {
      // use GSAP in here, or in custom methods
    }
  };
</script>
like image 188
Rich Harris Avatar answered Oct 06 '22 12:10

Rich Harris