Skip to content

Commit

Permalink
add vite+svelte example
Browse files Browse the repository at this point in the history
  • Loading branch information
clbrge committed Aug 28, 2022
1 parent 542b737 commit 94c9b2d
Show file tree
Hide file tree
Showing 16 changed files with 20,348 additions and 16 deletions.
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Use the [web3.js library](https://web3js.readthedocs.io/) as a
collection of [readable Svelte stores](https://svelte.dev/tutorial/readable-stores)
for Svelte, Sapper or SvelteKit.
for Svelte or SvelteKit.

If you prefer to use the [ethers.js
library](https://docs.ethers.io/v5/) to intereact with EVM, you may be
Expand All @@ -23,15 +23,15 @@ Discord](https://discord.gg/7yXuwDwaHF).
npm i svelte-web3
```

2. add the web3.js library in the main HTML page (`public/index.html` in Svelte, `src/template.html` in Sapper or `src/app.html` in SvelteKit)
2. add the web3.js library in the main HTML page (`index.html` in Svelte, `src/template.html` in Sapper or `src/app.html` in SvelteKit)

```html
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
```

This step is necessary for now because the Web3.js library doesn't
play well with bundlers (webpack, vite, snowpack, etc), thus we cannot
simply add a dependency in package.json.
This step is necessary for now because the Web3.js library doesn't play well
with bundlers (Vite, Rollup, Webpack, Snowpack, etc), thus we cannot simply add
a dependency in package.json.


## Basic usage (default stores connected to one chain)
Expand All @@ -51,19 +51,19 @@ import { connected, web3, selectedAccount, chainId, chainData } from 'svelte-web
* web3: store value is a Web3.js instance when connected.
* selectedAccount: store value is the current selected account (when connected).
* chainId: store value is the current chainId when connected.
* chainData: store value is the current blokchain CAIP-2 data (when connected), see below.
* chainData: store value is the current blockchain CAIP-2 data (when connected), see below.

For these stores to be useful in your Svelte application, a connection
to an EVM blockchain first need to established . The abstract helper
`defaultEvmStores` can be used to initiate the connection and
automatically instanciate all stores.
automatically instantiate all stores.

```js
import { defaultEvmStores } from 'svelte-web3'
```

:exclamation: `defaultEvmStores` was named before `defaultChainStore`. The
former naming still works but will be removed in later versions of
former naming has been removed in later versions of
`svelte-web3` package. Please update your code!

### Connection with the browser provider (eg wallets like Metamask)
Expand Down Expand Up @@ -103,6 +103,7 @@ removed in later versions. Please update your code!

To connect to non injected EIP-1193 providers like :

* web3-onboard
* buidler.dev
* ethers.js
* eth-provider
Expand Down Expand Up @@ -383,25 +384,30 @@ If you are using `svelte-web3` to build an open source Dapp, let us know
if you want to be listed in this section.


### Svelte basic example (based on rollup template)

Please check [examples/svelte-app-template-web3 on github](https://github.com/clbrge/svelte-web3/tree/master/examples/svelte-app-template-web3).
### Svelte basic example (using Vite)

Contains demos to use the default store and multi stores.
Please check [examples/svelte-vite-template-web3 on github](https://github.com/clbrge/svelte-web3/tree/master/examples/svelte-vite-template-web3).

### SvelteKit basic example

Please check [examples/sveltekit-app-template-web3 on github](https://github.com/clbrge/svelte-web3/tree/master/examples/sveltekit-app-template-web3).

### Svelte basic example (based on rollup template)

Please check [examples/svelte-app-template-web3 on github](https://github.com/clbrge/svelte-web3/tree/master/examples/svelte-app-template-web3).

This is a legacy example and will be removed in future version of `svelte-web3`.

Contains demos to use the default store and multi stores.

### Sapper basic example (based on webpack template)

Please check [examples/sapper-app-template-web3 on github](https://github.com/clbrge/svelte-web3/tree/master/examples/sapper-app-template-web3).
This is a legacy example and will be removed in future version of `svelte-web3`.


### tradingstrategy.ai presented at EthLisbon 2021

A website presented in EthLisbon 2021, used svelte-web3 (version 2) for building the frontend. :

* The page is live here: https://tradingstrategy.ai/strategy/ethlisbon
* Source code : https://github.com/tradingstrategy-ai/frontend/blob/master/src/lib/web3/Investor.svelte
* Tutorial: https://tradingstrategy.ai/blog/building-cryptocurrency-website
24 changes: 24 additions & 0 deletions examples/svelte-vite-template-web3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
48 changes: 48 additions & 0 deletions examples/svelte-vite-template-web3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Svelte + Vite

This template should help get you started developing with Svelte in Vite.

## Recommended IDE Setup

[VS Code](https://code.visualstudio.com/) + [Svelte](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode).

## Need an official Svelte framework?

Check out [SvelteKit](https://github.com/sveltejs/kit#readme), which is also powered by Vite. Deploy anywhere with its serverless-first approach and adapt to various platforms, with out of the box support for TypeScript, SCSS, and Less, and easily-added support for mdsvex, GraphQL, PostCSS, Tailwind CSS, and more.

## Technical considerations

**Why use this over SvelteKit?**

- It brings its own routing solution which might not be preferable for some users.
- It is first and foremost a framework that just happens to use Vite under the hood, not a Vite app.
`vite dev` and `vite build` wouldn't work in a SvelteKit environment, for example.

This template contains as little as possible to get started with Vite + Svelte, while taking into account the developer experience with regards to HMR and intellisense. It demonstrates capabilities on par with the other `create-vite` templates and is a good starting point for beginners dipping their toes into a Vite + Svelte project.

Should you later need the extended capabilities and extensibility provided by SvelteKit, the template has been structured similarly to SvelteKit so that it is easy to migrate.

**Why `global.d.ts` instead of `compilerOptions.types` inside `jsconfig.json` or `tsconfig.json`?**

Setting `compilerOptions.types` shuts out all other types not explicitly listed in the configuration. Using triple-slash references keeps the default TypeScript setting of accepting type information from the entire workspace, while also adding `svelte` and `vite/client` type information.

**Why include `.vscode/extensions.json`?**

Other templates indirectly recommend extensions via the README, but this file allows VS Code to prompt the user to install the recommended extension upon opening the project.

**Why enable `checkJs` in the JS template?**

It is likely that most cases of changing variable types in runtime are likely to be accidental, rather than deliberate. This provides advanced typechecking out of the box. Should you like to take advantage of the dynamically-typed nature of JavaScript, it is trivial to change the configuration.

**Why is HMR not preserving my local component state?**

HMR state preservation comes with a number of gotchas! It has been disabled by default in both `svelte-hmr` and `@sveltejs/vite-plugin-svelte` due to its often surprising behavior. You can read the details [here](https://github.com/rixo/svelte-hmr#svelte-hmr).

If you have state that's important to retain within a component, consider creating an external store which would not be replaced by HMR.

```js
// store.js
// An extremely simple external store
import { writable } from 'svelte/store'
export default writable(0)
```
14 changes: 14 additions & 0 deletions examples/svelte-vite-template-web3/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Svelte + web3.js</title>
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions examples/svelte-vite-template-web3/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"compilerOptions": {
"moduleResolution": "Node",
"target": "ESNext",
"module": "ESNext",
/**
* svelte-preprocess cannot figure out whether you have
* a value or a type, so tell TypeScript to enforce using
* `import type` instead of `import` for Types.
*/
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"resolveJsonModule": true,
/**
* To have warnings / errors of the Svelte compiler at the
* correct position, enable source maps by default.
*/
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
/**
* Typecheck JS in `.svelte` and `.js` files by default.
* Disable this if you'd like to use dynamic types.
*/
"checkJs": true
},
/**
* Use global.d.ts instead of compilerOptions.types
* to avoid limiting type declarations.
*/
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
}
Loading

0 comments on commit 94c9b2d

Please sign in to comment.