Skip to content

Commit 0feb1ac

Browse files
committed
Adds svelte example of useRef()
1 parent f6f0ea9 commit 0feb1ac

11 files changed

+474
-0
lines changed

use-ref/svelte/.eslintrc.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
es6: true,
5+
node: true
6+
},
7+
plugins: ['svelte3'],
8+
ignorePatterns: ['node_modules/', 'public/build'],
9+
extends: 'eslint:recommended',
10+
globals: {
11+
Atomics: 'readonly',
12+
SharedArrayBuffer: 'readonly'
13+
},
14+
parserOptions: {
15+
ecmaVersion: 2019,
16+
sourceType: 'module'
17+
},
18+
overrides: [
19+
{
20+
files: ['**/*.svelte'],
21+
processor: 'svelte3/svelte3'
22+
}
23+
],
24+
rules: {
25+
indent: [
26+
'error',
27+
2
28+
],
29+
'linebreak-style': [
30+
'error',
31+
'unix'
32+
],
33+
quotes: [
34+
'error',
35+
'single'
36+
],
37+
semi: [
38+
'error',
39+
'never'
40+
]
41+
}
42+
}

use-ref/svelte/.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/node_modules/
2+
/public/build/
3+
/yarn-error.log
4+
/yarn.lock
5+
6+
.DS_Store

use-ref/svelte/README.md

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
*Looking for a shareable component template? Go here --> [sveltejs/component-template](https://github.com/sveltejs/component-template)*
2+
3+
---
4+
5+
# svelte app
6+
7+
This is a project template for [Svelte](https://svelte.dev) apps. It lives at https://github.com/sveltejs/template.
8+
9+
To create a new project based on this template using [degit](https://github.com/Rich-Harris/degit):
10+
11+
```bash
12+
npx degit sveltejs/template svelte-app
13+
cd svelte-app
14+
```
15+
16+
*Note that you will need to have [Node.js](https://nodejs.org) installed.*
17+
18+
19+
## Get started
20+
21+
Install the dependencies...
22+
23+
```bash
24+
cd svelte-app
25+
npm install
26+
```
27+
28+
...then start [Rollup](https://rollupjs.org):
29+
30+
```bash
31+
npm run dev
32+
```
33+
34+
Navigate to [localhost:5000](http://localhost:5000). You should see your app running. Edit a component file in `src`, save it, and reload the page to see your changes.
35+
36+
By default, the server will only respond to requests from localhost. To allow connections from other computers, edit the `sirv` commands in package.json to include the option `--host 0.0.0.0`.
37+
38+
If you're using [Visual Studio Code](https://code.visualstudio.com/) we recommend installing the official extension [Svelte for VS Code](https://marketplace.visualstudio.com/items?itemName=svelte.svelte-vscode). If you are using other editors you may need to install a plugin in order to get syntax highlighting and intellisense.
39+
40+
## Building and running in production mode
41+
42+
To create an optimised version of the app:
43+
44+
```bash
45+
npm run build
46+
```
47+
48+
You can run the newly built app with `npm run start`. This uses [sirv](https://github.com/lukeed/sirv), which is included in your package.json's `dependencies` so that the app will work when you deploy to platforms like [Heroku](https://heroku.com).
49+
50+
51+
## Single-page app mode
52+
53+
By default, sirv will only respond to requests that match files in `public`. This is to maximise compatibility with static fileservers, allowing you to deploy your app anywhere.
54+
55+
If you're building a single-page app (SPA) with multiple routes, sirv needs to be able to respond to requests for *any* path. You can make it so by editing the `"start"` command in package.json:
56+
57+
```js
58+
"start": "sirv public --single"
59+
```
60+
61+
## Using TypeScript
62+
63+
This template comes with a script to set up a TypeScript development environment, you can run it immediately after cloning the template with:
64+
65+
```bash
66+
node scripts/setupTypeScript.js
67+
```
68+
69+
Or remove the script via:
70+
71+
```bash
72+
rm scripts/setupTypeScript.js
73+
```
74+
75+
## Deploying to the web
76+
77+
### With [Vercel](https://vercel.com)
78+
79+
Install `vercel` if you haven't already:
80+
81+
```bash
82+
npm install -g vercel
83+
```
84+
85+
Then, from within your project folder:
86+
87+
```bash
88+
cd public
89+
vercel deploy --name my-project
90+
```
91+
92+
### With [surge](https://surge.sh/)
93+
94+
Install `surge` if you haven't already:
95+
96+
```bash
97+
npm install -g surge
98+
```
99+
100+
Then, from within your project folder:
101+
102+
```bash
103+
npm run build
104+
surge public my-project.surge.sh
105+
```

use-ref/svelte/package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "svelte-app",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"build": "rollup -c",
6+
"dev": "rollup -c -w",
7+
"start": "sirv public",
8+
"lint": "eslint . --ext .js,.svelte"
9+
},
10+
"devDependencies": {
11+
"@rollup/plugin-commonjs": "^14.0.0",
12+
"@rollup/plugin-node-resolve": "^8.0.0",
13+
"eslint": "^7.11.0",
14+
"eslint-plugin-svelte3": "^2.7.3",
15+
"rollup": "^2.3.4",
16+
"rollup-plugin-livereload": "^2.0.0",
17+
"rollup-plugin-svelte": "^6.0.0",
18+
"rollup-plugin-terser": "^7.0.0",
19+
"svelte": "^3.0.0"
20+
},
21+
"dependencies": {
22+
"sirv-cli": "^1.0.0"
23+
}
24+
}

use-ref/svelte/public/favicon.png

3.05 KB
Loading

use-ref/svelte/public/global.css

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
html, body {
2+
position: relative;
3+
width: 100%;
4+
height: 100%;
5+
}
6+
7+
body {
8+
color: #333;
9+
margin: 0;
10+
padding: 8px;
11+
box-sizing: border-box;
12+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
13+
}
14+
15+
a {
16+
color: rgb(0,100,200);
17+
text-decoration: none;
18+
}
19+
20+
a:hover {
21+
text-decoration: underline;
22+
}
23+
24+
a:visited {
25+
color: rgb(0,80,160);
26+
}
27+
28+
label {
29+
display: block;
30+
}
31+
32+
input, button, select, textarea {
33+
font-family: inherit;
34+
font-size: inherit;
35+
-webkit-padding: 0.4em 0;
36+
padding: 0.4em;
37+
margin: 0 0 0.5em 0;
38+
box-sizing: border-box;
39+
border: 1px solid #ccc;
40+
border-radius: 2px;
41+
}
42+
43+
input:disabled {
44+
color: #ccc;
45+
}
46+
47+
button {
48+
color: #333;
49+
background-color: #f4f4f4;
50+
outline: none;
51+
}
52+
53+
button:disabled {
54+
color: #999;
55+
}
56+
57+
button:not(:disabled):active {
58+
background-color: #ddd;
59+
}
60+
61+
button:focus {
62+
border-color: #666;
63+
}

use-ref/svelte/public/index.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset='utf-8'>
5+
<meta name='viewport' content='width=device-width,initial-scale=1'>
6+
7+
<title>Svelte app</title>
8+
9+
<link rel='icon' type='image/png' href='/favicon.png'>
10+
<link rel='stylesheet' href='/global.css'>
11+
<link rel='stylesheet' href='/build/bundle.css'>
12+
13+
<script defer src='/build/bundle.js'></script>
14+
</head>
15+
16+
<body>
17+
</body>
18+
</html>

use-ref/svelte/rollup.config.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import svelte from 'rollup-plugin-svelte'
2+
import resolve from '@rollup/plugin-node-resolve'
3+
import commonjs from '@rollup/plugin-commonjs'
4+
import livereload from 'rollup-plugin-livereload'
5+
import { terser } from 'rollup-plugin-terser'
6+
7+
const production = !process.env.ROLLUP_WATCH
8+
9+
function serve() {
10+
let server
11+
12+
function toExit() {
13+
if (server) server.kill(0)
14+
}
15+
16+
return {
17+
writeBundle() {
18+
if (server) return
19+
server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
20+
stdio: ['ignore', 'inherit', 'inherit'],
21+
shell: true
22+
})
23+
24+
process.on('SIGTERM', toExit)
25+
process.on('exit', toExit)
26+
}
27+
}
28+
}
29+
30+
export default {
31+
input: 'src/main.js',
32+
output: {
33+
sourcemap: true,
34+
format: 'iife',
35+
name: 'app',
36+
file: 'public/build/bundle.js'
37+
},
38+
plugins: [
39+
svelte({
40+
// enable run-time checks when not in production
41+
dev: !production,
42+
// we'll extract any component CSS out into
43+
// a separate file - better for performance
44+
css: css => {
45+
css.write('bundle.css')
46+
}
47+
}),
48+
49+
// If you have external dependencies installed from
50+
// npm, you'll most likely need these plugins. In
51+
// some cases you'll need additional configuration -
52+
// consult the documentation for details:
53+
// https://github.com/rollup/plugins/tree/master/packages/commonjs
54+
resolve({
55+
browser: true,
56+
dedupe: ['svelte']
57+
}),
58+
commonjs(),
59+
60+
// In dev mode, call `npm run start` once
61+
// the bundle has been generated
62+
!production && serve(),
63+
64+
// Watch the `public` directory and refresh the
65+
// browser on changes when not in production
66+
!production && livereload('public'),
67+
68+
// If we're building for production (npm run build
69+
// instead of npm run dev), minify
70+
production && terser()
71+
],
72+
watch: {
73+
clearScreen: false
74+
}
75+
}

0 commit comments

Comments
 (0)