Skip to content

Commit

Permalink
add a sveltejs example folder
Browse files Browse the repository at this point in the history
  • Loading branch information
simeydotme committed Mar 9, 2020
1 parent 0936069 commit c180693
Show file tree
Hide file tree
Showing 10 changed files with 1,473 additions and 0 deletions.
4 changes: 4 additions & 0 deletions example/svelte/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/node_modules/
/public/build/

.DS_Store
93 changes: 93 additions & 0 deletions example/svelte/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
*Looking for a shareable component template? Go here --> [sveltejs/component-template](https://github.com/sveltejs/component-template)*

---

# svelte app

This is a project template for [Svelte](https://svelte.dev) apps. It lives at https://github.com/sveltejs/template.

To create a new project based on this template using [degit](https://github.com/Rich-Harris/degit):

```bash
npx degit sveltejs/template svelte-app
cd svelte-app
```

*Note that you will need to have [Node.js](https://nodejs.org) installed.*


## Get started

Install the dependencies...

```bash
cd svelte-app
npm install
```

...then start [Rollup](https://rollupjs.org):

```bash
npm run dev
```

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.

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`.


## Building and running in production mode

To create an optimised version of the app:

```bash
npm run build
```

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).


## Single-page app mode

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.

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:

```js
"start": "sirv public --single"
```


## Deploying to the web

### With [now](https://zeit.co/now)

Install `now` if you haven't already:

```bash
npm install -g now
```

Then, from within your project folder:

```bash
cd public
now deploy --name my-project
```

As an alternative, use the [Now desktop client](https://zeit.co/download) and simply drag the unzipped project folder to the taskbar icon.

### With [surge](https://surge.sh/)

Install `surge` if you haven't already:

```bash
npm install -g surge
```

Then, from within your project folder:

```bash
npm run build
surge public my-project.surge.sh
```
22 changes: 22 additions & 0 deletions example/svelte/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "svelte-app",
"version": "1.0.0",
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"start": "sirv public"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^11.0.0",
"@rollup/plugin-node-resolve": "^7.0.0",
"rollup": "^1.20.0",
"rollup-plugin-livereload": "^1.0.0",
"rollup-plugin-svelte": "^5.0.3",
"rollup-plugin-terser": "^5.1.2",
"svelte": "^3.0.0",
"sparticles": "^0.12.0"
},
"dependencies": {
"sirv-cli": "^0.4.4"
}
}
Binary file added example/svelte/public/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions example/svelte/public/global.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
* {
box-sizing: border-box;
}

html, body {
color: rgb(237, 231, 255);
background: rgb(33, 32, 39);
padding: 10px;
margin: 0;
height: 100%;
}

nav {
position: fixed;
bottom: 10px;
left: 10px;
}

nav ul {
list-style: none;
}

nav li {
margin: 0 10px 0 -2px;
padding: 0 10px 0 0;
display: inline-block;
}

nav li:not(:last-child) {
border-right: 1px solid white;
}

nav a {
color: rgba(255, 255, 255, 0.6);
line-height: 1.4;
display: inline-block;
}

nav a.current,
nav a:hover,
nav a:active,
nav a:focus {
color: white;
}
18 changes: 18 additions & 0 deletions example/svelte/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width,initial-scale=1'>

<title>Svelte app</title>

<link rel='icon' type='image/png' href='/favicon.png'>
<link rel='stylesheet' href='/global.css'>
<link rel='stylesheet' href='/build/bundle.css'>

<script defer src='/build/bundle.js'></script>
</head>

<body>
</body>
</html>
71 changes: 71 additions & 0 deletions example/svelte/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';

const production = !process.env.ROLLUP_WATCH;

export default {
input: 'src/main.js',
output: {
sourcemap: true,
format: 'iife',
name: 'app',
file: 'public/build/bundle.js'
},
plugins: [
svelte({
// enable run-time checks when not in production
dev: !production,
// we'll extract any component CSS out into
// a separate file - better for performance
css: css => {
css.write('public/build/bundle.css');
}
}),

// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In
// some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs(),

// In dev mode, call `npm run start` once
// the bundle has been generated
!production && serve(),

// Watch the `public` directory and refresh the
// browser on changes when not in production
!production && livereload('public'),

// If we're building for production (npm run build
// instead of npm run dev), minify
production && terser()
],
watch: {
clearScreen: false
}
};

function serve() {
let started = false;

return {
writeBundle() {
if (!started) {
started = true;

require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
stdio: ['ignore', 'inherit', 'inherit'],
shell: true
});
}
}
};
}
17 changes: 17 additions & 0 deletions example/svelte/src/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script>
import Sparticles from "../../../dist/sparticles.esm.js";
let sparticles;
let options = { color: "gold", shape: "star", speed: 50 };
function addSparticles(node) {
new Sparticles(node, options);
}
</script>

<main use:addSparticles>
</main>

<style>
main {
height: 100%;
}
</style>
10 changes: 10 additions & 0 deletions example/svelte/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import App from './App.svelte';

const app = new App({
target: document.body,
props: {
name: 'world'
}
});

export default app;
Loading

0 comments on commit c180693

Please sign in to comment.