• This repository has been archived on 13/Oct/2022
  • Stars
    star
    703
  • Rank 64,412 (Top 2 %)
  • Language
    JavaScript
  • Created almost 7 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Starter template for Sapper apps

sapper-template

The default template for setting up a Sapper project. Can use either Rollup or webpack as bundler.

Please note

Sapper is no longer being actively developed. You may be interested in using Sapper's succesor, SvelteKit for new projects.

Getting started

Using degit

To create a new Sapper project based on Rollup locally, run

npx degit "sveltejs/sapper-template#rollup" my-app

For a webpack-based project, instead run

npx degit "sveltejs/sapper-template#webpack" my-app

degit is a scaffolding tool that lets you create a directory from a branch in a repository.

Replace my-app with the path where you wish to create the project.

Using GitHub templates

Alternatively, you can create the new project as a GitHub repository using GitHub's template feature.

Go to either sapper-template-rollup or sapper-template-webpack and click on "Use this template" to create a new project repository initialized by the template.

Running the project

Once you have created the project, install dependencies and run the project in development mode:

cd my-app
npm install # or yarn
npm run dev

This will start the development server on localhost:3000. Open it and click around.

You now have a fully functional Sapper project! To get started developing, consult sapper.svelte.dev.

Using TypeScript

By default, the template uses plain JavaScript. If you wish to use TypeScript instead, you need some changes to the project:

  • Add typescript as well as typings as dependences in package.json
  • Configure the bundler to use svelte-preprocess and transpile the TypeScript code.
  • Add a tsconfig.json file
  • Update the project code to TypeScript

The template comes with a script that will perform these changes for you by running

node scripts/setupTypeScript.js

@sapper dependencies are resolved through src/node_modules/@sapper, which is created during the build. You therefore need to run or build the project once to avoid warnings about missing dependencies.

The script does not support webpack at the moment.

Directory structure

Sapper expects to find two directories in the root of your project — src and static.

src

The src directory contains the entry points for your app — client.js, server.js and (optionally) a service-worker.js — along with a template.html file and a routes directory.

src/routes

This is the heart of your Sapper app. There are two kinds of routes — pages, and server routes.

Pages are Svelte components written in .svelte files. When a user first visits the application, they will be served a server-rendered version of the route in question, plus some JavaScript that 'hydrates' the page and initialises a client-side router. From that point forward, navigating to other pages is handled entirely on the client for a fast, app-like feel. (Sapper will preload and cache the code for these subsequent pages, so that navigation is instantaneous.)

Server routes are modules written in .js files, that export functions corresponding to HTTP methods. Each function receives Express request and response objects as arguments, plus a next function. This is useful for creating a JSON API, for example.

There are three simple rules for naming the files that define your routes:

  • A file called src/routes/about.svelte corresponds to the /about route. A file called src/routes/blog/[slug].svelte corresponds to the /blog/:slug route, in which case params.slug is available to the route
  • The file src/routes/index.svelte (or src/routes/index.js) corresponds to the root of your app. src/routes/about/index.svelte is treated the same as src/routes/about.svelte.
  • Files and directories with a leading underscore do not create routes. This allows you to colocate helper modules and components with the routes that depend on them — for example you could have a file called src/routes/_helpers/datetime.js and it would not create a /_helpers/datetime route.

src/node_modules/images

Images added to src/node_modules/images can be imported into your code using import 'images/<filename>'. They will be given a dynamically generated filename containing a hash, allowing for efficient caching and serving the images on a CDN.

See index.svelte for an example.

src/node_modules/@sapper

This directory is managed by Sapper and generated when building. It contains all the code you import from @sapper modules.

static

The static directory contains static assets that should be served publicly. Files in this directory will be available directly under the root URL, e.g. an image.jpg will be available as /image.jpg.

The default service-worker.js will preload and cache these files, by retrieving a list of files from the generated manifest:

import { files } from '@sapper/service-worker';

If you have static files you do not want to cache, you should exclude them from this list after importing it (and before passing it to cache.addAll).

Static files are served using sirv.

Bundler configuration

Sapper uses Rollup or webpack to provide code-splitting and dynamic imports, as well as compiling your Svelte components. With webpack, it also provides hot module reloading. As long as you don't do anything daft, you can edit the configuration files to add whatever plugins you'd like.

Production mode and deployment

To start a production version of your app, run npm run build && npm start. This will disable live reloading, and activate the appropriate bundler plugins.

You can deploy your application to any environment that supports Node 10 or above. As an example, to deploy to Vercel Now when using sapper export, run these commands:

npm install -g vercel
vercel

If your app can't be exported to a static site, you can use the vercel-sapper builder. You can find instructions on how to do so in its README.

Using external components

When using Svelte components installed from npm, such as @sveltejs/svelte-virtual-list, Svelte needs the original component source (rather than any precompiled JavaScript that ships with the component). This allows the component to be rendered server-side, and also keeps your client-side app smaller.

Because of that, it's essential that the bundler doesn't treat the package as an external dependency. You can either modify the external option under server in rollup.config.js or the externals option in webpack.config.js, or simply install the package to devDependencies rather than dependencies, which will cause it to get bundled (and therefore compiled) with your app:

npm install -D @sveltejs/svelte-virtual-list

Troubleshooting

Using Windows and WSL2?

If your project lives outside the WSL root directory, this limitation is known to cause live-reloading to fail. See this issue for details.

Bugs and feedback

Sapper is in early development, and may have the odd rough edge here and there. Please be vocal over on the Sapper issue tracker.

More Repositories

1

svelte

Cybernetically enhanced web apps
JavaScript
77,898
star
2

kit

web development, streamlined
JavaScript
18,346
star
3

sapper

The next small thing in web development, powered by Svelte
TypeScript
7,007
star
4

realworld

SvelteKit implementation of the RealWorld app
Svelte
2,212
star
5

template

Template for building basic applications with Svelte
JavaScript
1,743
star
6

svelte-preprocess

A ✨ magical ✨ Svelte preprocessor with sensible defaults and support for: PostCSS, SCSS, Less, Stylus, Coffeescript, TypeScript, Pug and much more.
TypeScript
1,743
star
7

svelte-devtools

A browser extension to inspect Svelte application by extending your browser devtools capabilities
Svelte
1,457
star
8

language-tools

The Svelte Language Server, and official extensions which use it
TypeScript
1,199
star
9

vite-plugin-svelte

Svelte plugin for http://vitejs.dev/
JavaScript
838
star
10

prettier-plugin-svelte

Format your svelte components using prettier.
TypeScript
726
star
11

svelte-virtual-list

A virtual list component for Svelte apps
JavaScript
673
star
12

integrations

Ways to incorporate Svelte into your stack
632
star
13

gl

A (very experimental) project to bring WebGL to Svelte
JavaScript
603
star
14

svelte-loader

Webpack loader for svelte components.
JavaScript
596
star
15

community-legacy

Svelte community meetups, packages, resources, recipes, showcase websites, and more
Svelte
563
star
16

component-template

A base for building shareable Svelte components
JavaScript
554
star
17

learn.svelte.dev

A soup-to-nuts interactive tutorial on how to build apps with Svelte
Svelte
522
star
18

rollup-plugin-svelte

Compile Svelte components with Rollup
JavaScript
500
star
19

eslint-plugin-svelte3

An ESLint plugin for Svelte v3 components.
JavaScript
372
star
20

svelte-scroller

A <Scroller> component for Svelte apps
Svelte
345
star
21

template-webpack

Template for building basic Svelte applications with webpack
JavaScript
303
star
22

sites

Monorepo for the sites in the Svelte ecosystem
Svelte
291
star
23

eslint-plugin-svelte

ESLint plugin for Svelte using AST
TypeScript
287
star
24

svelte-repl

The <Repl> component used on the Svelte website
Svelte
274
star
25

rfcs

RFCs for changes to Svelte
273
star
26

sapper-studio

An electron app for building Sapper projects
HTML
220
star
27

v2.svelte.dev

The Svelte v2 website
HTML
209
star
28

svelte-hmr

HMR commons for Svelte 3
JavaScript
202
star
29

site-kit

Svelte
196
star
30

hn.svelte.dev

Hacker News clone built with Svelte and Sapper
Svelte
164
star
31

svelte-todomvc

TodoMVC implemented in Svelte
Svelte
139
star
32

svelte-subdivide

A component for building Blender-style layouts in Svelte apps
JavaScript
130
star
33

svelte-cli

Command line interface for Svelte
JavaScript
104
star
34

svelte-eslint-parser

Svelte parser for ESLint
TypeScript
92
star
35

gestures

Svelte actions for cross-platform gesture detection
TypeScript
89
star
36

svelte-hackernews

WIP Hacker News clone written in Svelte
JavaScript
78
star
37

svelte-custom-elements

Turn Svelte components into web components
JavaScript
49
star
38

svelte-extras

Extra methods for Svelte components
JavaScript
43
star
39

sapper-template-rollup

Starter Rollup template for Sapper apps
JavaScript
40
star
40

kit-template-default

The default SvelteKit template, generated with create-svelte
Svelte
36
star
41

svelte-upgrade

Upgrade your Svelte templates for version 2
JavaScript
36
star
42

examples

A collection of Svelte(Kit) examples
35
star
43

branding

Logos etc for Svelte and related projects
35
star
44

hn.svelte.technology

Hacker News, built with Sapper
HTML
35
star
45

svelte-transitions

Officially supported transition plugins for Svelte
JavaScript
33
star
46

sapper-template-webpack

Starter webpack template for Sapper apps
JavaScript
32
star
47

discord-bot

TypeScript
32
star
48

svelte-atom

Syntax, diagnostics, and other smarts for Svelte in Atom
JavaScript
30
star
49

community

28
star
50

svelte-dbmonster

Svelte implementation of DBMonster
JavaScript
27
star
51

template-custom-element

Template for building basic applications with Svelte and custom elements
HTML
21
star
52

generate-ssr

Server-side rendering for Svelte
JavaScript
21
star
53

kit-sandbox

A sandbox for maintainers
JavaScript
18
star
54

template-store

Demonstrating the use of svelte/store
JavaScript
17
star
55

svelte-transitions-draw

Draw transition plugin for Svelte
JavaScript
16
star
56

api.svelte.dev

The API worker source for https://api.svelte.dev
TypeScript
14
star
57

sapper-legacy.svelte.dev

Old docs site for Sapper
HTML
13
star
58

action-deploy-docs

github action for the svelte org to deploy documentation to the svelte api
TypeScript
12
star
59

svelte-bench

Benchmarks for Svelte
JavaScript
10
star
60

sveltegram

Sapper/Svelte remix of nextgram
JavaScript
8
star
61

svelte-transitions-fade

Fade transition plugin for Svelte
JavaScript
8
star
62

assets

Large static files used on the Svelte website
7
star
63

svelte-transitions-slide

Slide transition plugin for Svelte
JavaScript
5
star
64

svelte-transitions-fly

Fly transition plugin for Svelte
JavaScript
4
star
65

redirects

Redirect old Svelte websites to their shiny new equivalents
JavaScript
3
star