• Stars
    star
    375
  • Rank 114,096 (Top 3 %)
  • Language
    HTML
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 2 years ago

Reviews

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

Repository Details

Build hybrid mobile apps using Svelte and CapacitorJS with live reloading on Android and iOS!

Svelte + Capacitor (w/ live reload) Template

This is a project template for Svelte + CapacitorJS apps with live reload, granting the ability to quickly build production ready applications for iOS and Android at near-native performance using JavaScript (Svelte).

Changelog

v2.0.0 - The new version is here, this comes with some better defaults, vite as the build manager, built in routing library, TypeScript, and aliases. While this could be seen as an 'incremental' update I think this works as a v2.0.0 as it was a complete rewrite.

  • Build Tool: Changed the build tool to Vite from Rollup, this is the new and recommended way to develop Svelte applications, it's faster, cleaner, and easier to handle.
  • Routing: Built in routing library, using Routify, I spent a considerable time debating on including one - I decided I would. This is mainly due to the amount of messages and emails(!) I received asking how to implement one, Routify is by far my favorite and so here it is. This is very easily removed and replaced with your preferred.
  • Typescript: I personally do not use typescript as much as I should, but others do. I have included some TS configs for you.
  • @components, @store, and @utils: If you have ever taken a look at sveltekit they have these nifty aliases throughout their code so you don't have to write '../../../components' or what have you. Now you can just do '@components/file' and Vite will readily load them from the components folder, this makes it easy to create changes as you don't need to change the file location on every move or copy. Please check out the Vite Aliases package for more questions.

Setup

Note that you will need to have Node.js installed.

Get started

Install the dependencies...

git clone "https://github.com/drannex42/svelte-capacitor.git"
cd svelte-capacitor
npm install

Run local dev server (no capacitor)

npm run dev:start

Add your Capacitor platform

npx cap add android // (or ios)

Run local dev server (with capacitor)

Please see section below (Develop on your device with livereload)

Develop on your device with livereload (hot reload)

If you're only targeting Android, you only need to change the server.url section in capacitor.config.json to use http://10.0.2.2:5001, since Android Studio already adds a localhost proxy. Just remember to remove it when building your app for production.

If you're targeting iOS or both, you will need to append your workstation IP to the server.url section in capacitor.config.json instead. To discover your workstation IP, just run ifconfig or find it on the network settings.

Tip: Remember you will need the http:// before the server IP.

Back in the root folder:

npm run dev:android or npm run dev:ios

This will run the capacitor/svelte project with a web view pointing to your workstation's IP. You should see the message Welcome to Svelte! if svelte loaded correctly.

Try to change something in App.svelte, and you should see the content reload on your device.

  • You need to have an emulator/device connected to adb
  • Your device has to be connected to the same wifi network as your workstation.

Change Splash screen and app icon:

  • Add icon.png and splash.png to resources dir
  • Recomended size:
    • Icon - 512x512
    • Splash - 1920x1920
  • Next, run the following to generate all images then copy them into the native projects:
cordova-res ios --skip-config --copy
cordova-res android --skip-config --copy

To build a production application:

Remember to remove the server.url in capacitor.config.json

npm run build:android or npm run build:ios

Quick Tips

Developer Tools

You can use any Chromium-based browser and use their Developer Tools (for Android atleast, have not tested iOS) to debug and access console commands on your personal device, by going to chrome://inspect#devices., edge://inspect#devices., vivaldi://inspect#devices., brave://inspect#devices, &c.

The standard web inspector will also work for debugging and rewriting styling and html without using your IDE just like when building a normal website. This may have some issues with some forms of SVG-related svelte templating (I have had issues with using some chart libraries displaying in the web inspector), they will still show up on the device but not in the preview.

You may use the address bar in the developer tools to navigate to direct views in your application, even without any tappable links to do so. This allows you to create hidden routes for testing.

Assets

Place any assets, CSS, client JS, Images in the assets folder - not the public folder! On build and usage this will add everything to the public dist folder for copying to the device application. See Vite.js on this settings.

Aliases

@components, @store, and @utils: If you have ever taken a look at sveltekit they have these nify aliases throughout their code so you don't have to write '../../../components' or what have you. Now you can just do '@components/file' and Vite will readily load them from the components folder, this makes it easy to create changes as you don't need to change the file location on every move or copy. Please check out the Vite Aliases package for more questions.

Routing Libraries

Since this is a Svelte application you will be able to use any routing manager that you prefer. I prefer routify, but others such as the following will work as well (Routify is built into this project, but easily changed)

Accessing Device APIs

CapacitorJS is/was based on Cordova and has complete backwards compatibility with cordova plugins. To find plugins that allow you to access the device api's easier, attempt to find cordova or capacitor plugins. You will need to view their (capacitorjs) docs to learn how to properly accesss those.

Why didn't the second version use Sveltekit?

This is ready for production (this template is being used in several production applications already!) and Sveltekit is not, I also believe this is much more easily extensible and doesn't lock you into the Sveltekit way of doing things (no opinionated API's) while adding in defaults for Capacitor.js for application development on Android and iOS. You likely don't need to learn a new paradigm, just use what you know with Javascript and Svelte to build cross platform applications at near native performance.

Find a bug? Want to add a feature?

Submit a PR! I would love to have more people working on this, the advantages of a system such as this vs NativeScript or React Native are innumerable especially in regards for how quickly you can get started and instant developer options and this could be built out to something great!

Are you using this project! Let us know!

I would love to keep a collection of all the projects using this!


This template was created by Drannex42 (Macleod Sawyer) of DNX Industries - Source can be found here

More Repositories

1

FirefoxSidebar

Vertical tab design for Firefox with dynamic indentation:: Sidebery and TreeStyleTabs (Legacy) themes available!
CSS
218
star
2

vivaldi-mods

CSS
38
star
3

svelte-vibe.d

Vibe.d (D-Lang) with Svelte! This requires node.js (to build svelte, nothing else) and Dub/Dlang to run.
JavaScript
12
star
4

NoPo

No Post, Allows you to block a specific tagged post (for example "x") from being seen on your tumblr homepage/theme but still be visible to your followers on your dashboard
12
star
5

avadabrowser

Got bored, made a browser, currently renders 138% faster than chrome.
CSS
4
star
6

tumblr-desktop-client

A desktop client for tumblr with superpowers (Multiple Account Support) - Runs using Electron
JavaScript
4
star
7

twitter-scripts

Follow and/or favorite all twitter users on a page.
JavaScript
3
star
8

tildes-for-lemmy-theme

This is a theme built for the forum software Lemmy.ml inspired by(!) the design of tildes.net.
CSS
3
star
9

utils

Moving my linux-utils && rust-utils (and elixir... and D... and, &, et, etc...) into one repo.
Rust
2
star
10

coolgrids

just mobile responsive css grids
CSS
2
star
11

Sabertooth.jl

Julia Library to manage the Sabertooth motor controller
Julia
2
star
12

video-frame-extractor

simple to use bulk video frame extractor using node.js // this is quick and dirty code.... but works.
JavaScript
2
star
13

Tweepi-Select-All-JS

Auto selects all columns with the default option on Tweepi.com
JavaScript
1
star
14

GiftYourEnemies

CSS
1
star
15

charitydrops

Way old version when I first started leaning Nodejs
CSS
1
star
16

node-mongo-starter

boilerplate for node.js with mongodb and account management (with authentication)
JavaScript
1
star
17

squire

Ghost theme for my new personal blog @ macleodsawyer.com
HTML
1
star
18

tumblr-themes

Themes I have used on tumblr
HTML
1
star
19

touchfreeze

TouchFreeze is simple utility for Windows to automatically disable the touchpad while you are typing text.
C
1
star
20

minimal-bold-decorations

Minimal Bold window decorations for XFCE/XFWM4/Picom/&c.
1
star
21

sabertooth.js

Simple to use Javascript API for Sabertooth motor controllers (made by Dimension Engineering)
JavaScript
1
star