Skip to content

Commit 421d442

Browse files
committed
Iniciado projeto com Sapper
0 parents  commit 421d442

30 files changed

+945
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
/node_modules/
3+
/src/node_modules/@sapper/
4+
yarn-error.log
5+
/cypress/screenshots/
6+
/__sapper__/
7+
.env

README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# sapper-template
2+
3+
The default [Sapper](https://github.com/sveltejs/sapper) template, available for Rollup and webpack.
4+
5+
6+
## Getting started
7+
8+
9+
### Using `degit`
10+
11+
[`degit`](https://github.com/Rich-Harris/degit) is a scaffolding tool that lets you create a directory from a branch in a repository. Use either the `rollup` or `webpack` branch in `sapper-template`:
12+
13+
```bash
14+
# for Rollup
15+
npx degit "sveltejs/sapper-template#rollup" my-app
16+
# for webpack
17+
npx degit "sveltejs/sapper-template#webpack" my-app
18+
```
19+
20+
21+
### Using GitHub templates
22+
23+
Alternatively, you can use GitHub's template feature with the [sapper-template-rollup](https://github.com/sveltejs/sapper-template-rollup) or [sapper-template-webpack](https://github.com/sveltejs/sapper-template-webpack) repositories.
24+
25+
26+
### Running the project
27+
28+
However you get the code, you can install dependencies and run the project in development mode with:
29+
30+
```bash
31+
cd my-app
32+
npm install # or yarn
33+
npm run dev
34+
```
35+
36+
Open up [localhost:3000](http://localhost:3000) and start clicking around.
37+
38+
Consult [sapper.svelte.dev](https://sapper.svelte.dev) for help getting started.
39+
40+
41+
## Structure
42+
43+
Sapper expects to find two directories in the root of your project — `src` and `static`.
44+
45+
46+
### src
47+
48+
The [src](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.
49+
50+
51+
#### src/routes
52+
53+
This is the heart of your Sapper app. There are two kinds of routes — *pages*, and *server routes*.
54+
55+
**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.)
56+
57+
**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.
58+
59+
There are three simple rules for naming the files that define your routes:
60+
61+
* 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
62+
* 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`.
63+
* 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
64+
65+
66+
### static
67+
68+
The [static](static) directory contains any static assets that should be available. These are served using [sirv](https://github.com/lukeed/sirv).
69+
70+
In your [service-worker.js](src/service-worker.js) file, you can import these as `files` from the generated manifest...
71+
72+
```js
73+
import { files } from '@sapper/service-worker';
74+
```
75+
76+
...so that you can cache them (though you can choose not to, for example if you don't want to cache very large files).
77+
78+
79+
## Bundler config
80+
81+
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.
82+
83+
84+
## Production mode and deployment
85+
86+
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.
87+
88+
You can deploy your application to any environment that supports Node 8 or above. As an example, to deploy to [Now](https://zeit.co/now), run these commands:
89+
90+
```bash
91+
npm install -g now
92+
now
93+
```
94+
95+
96+
## Using external components
97+
98+
When using Svelte components installed from npm, such as [@sveltejs/svelte-virtual-list](https://github.com/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.
99+
100+
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](rollup.config.js) or the `externals` option in [webpack.config.js](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:
101+
102+
```bash
103+
npm install -D @sveltejs/svelte-virtual-list
104+
```
105+
106+
107+
## Bugs and feedback
108+
109+
Sapper is in early development, and may have the odd rough edge here and there. Please be vocal over on the [Sapper issue tracker](https://github.com/sveltejs/sapper/issues).

cypress.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"baseUrl": "http://localhost:3000",
3+
"video": false
4+
}

cypress/fixtures/example.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Using fixtures to represent data",
3+
"email": "[email protected]",
4+
"body": "Fixtures are a great way to mock data for responses to routes"
5+
}

cypress/integration/spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
describe('Sapper template app', () => {
2+
beforeEach(() => {
3+
cy.visit('/')
4+
});
5+
6+
it('has the correct <h1>', () => {
7+
cy.contains('h1', 'Great success!')
8+
});
9+
10+
it('navigates to /about', () => {
11+
cy.get('nav a').contains('about').click();
12+
cy.url().should('include', '/about');
13+
});
14+
15+
it('navigates to /blog', () => {
16+
cy.get('nav a').contains('blog').click();
17+
cy.url().should('include', '/blog');
18+
});
19+
});

cypress/plugins/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// ***********************************************************
2+
// This example plugins/index.js can be used to load plugins
3+
//
4+
// You can change the location of this file or turn off loading
5+
// the plugins file with the 'pluginsFile' configuration option.
6+
//
7+
// You can read more here:
8+
// https://on.cypress.io/plugins-guide
9+
// ***********************************************************
10+
11+
// This function is called when a project is opened or re-opened (e.g. due to
12+
// the project's config changing)
13+
14+
module.exports = (on, config) => {
15+
// `on` is used to hook into various events Cypress emits
16+
// `config` is the resolved Cypress config
17+
}

cypress/support/commands.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ***********************************************
2+
// This example commands.js shows you how to
3+
// create various custom commands and overwrite
4+
// existing commands.
5+
//
6+
// For more comprehensive examples of custom
7+
// commands please read more here:
8+
// https://on.cypress.io/custom-commands
9+
// ***********************************************
10+
//
11+
//
12+
// -- This is a parent command --
13+
// Cypress.Commands.add("login", (email, password) => { ... })
14+
//
15+
//
16+
// -- This is a child command --
17+
// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
18+
//
19+
//
20+
// -- This is a dual command --
21+
// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
22+
//
23+
//
24+
// -- This is will overwrite an existing command --
25+
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })

cypress/support/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ***********************************************************
2+
// This example support/index.js is processed and
3+
// loaded automatically before your test files.
4+
//
5+
// This is a great place to put global configuration and
6+
// behavior that modifies Cypress.
7+
//
8+
// You can change the location of this file or turn off
9+
// automatically serving support files with the
10+
// 'supportFile' configuration option.
11+
//
12+
// You can read more here:
13+
// https://on.cypress.io/configuration
14+
// ***********************************************************
15+
16+
// Import commands.js using ES2015 syntax:
17+
import './commands'
18+
19+
// Alternatively you can use CommonJS syntax:
20+
// require('./commands')

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "TODO",
3+
"description": "TODO",
4+
"version": "0.0.1",
5+
"scripts": {
6+
"dev": "sapper dev",
7+
"build": "sapper build --legacy",
8+
"export": "sapper export --legacy",
9+
"start": "node __sapper__/build",
10+
"cy:run": "cypress run",
11+
"cy:open": "cypress open",
12+
"test": "run-p --race dev cy:run"
13+
},
14+
"dependencies": {
15+
"compression": "^1.7.1",
16+
"polka": "next",
17+
"sirv": "^0.4.0"
18+
},
19+
"devDependencies": {
20+
"npm-run-all": "^4.1.5",
21+
"sapper": "^0.27.0",
22+
"svelte": "^3.0.0",
23+
"@babel/core": "^7.0.0",
24+
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
25+
"@babel/plugin-transform-runtime": "^7.0.0",
26+
"@babel/preset-env": "^7.0.0",
27+
"@babel/runtime": "^7.0.0",
28+
"rollup": "^1.12.0",
29+
"rollup-plugin-babel": "^4.0.2",
30+
"rollup-plugin-commonjs": "^10.0.0",
31+
"rollup-plugin-node-resolve": "^5.2.0",
32+
"rollup-plugin-replace": "^2.0.0",
33+
"rollup-plugin-svelte": "^5.0.1",
34+
"rollup-plugin-terser": "^4.0.4"
35+
}
36+
}

rollup.config.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import resolve from 'rollup-plugin-node-resolve';
2+
import replace from 'rollup-plugin-replace';
3+
import commonjs from 'rollup-plugin-commonjs';
4+
import svelte from 'rollup-plugin-svelte';
5+
import babel from 'rollup-plugin-babel';
6+
import { terser } from 'rollup-plugin-terser';
7+
import config from 'sapper/config/rollup.js';
8+
import pkg from './package.json';
9+
10+
const mode = process.env.NODE_ENV;
11+
const dev = mode === 'development';
12+
const legacy = !!process.env.SAPPER_LEGACY_BUILD;
13+
14+
const onwarn = (warning, onwarn) => (warning.code === 'CIRCULAR_DEPENDENCY' && /[/\\]@sapper[/\\]/.test(warning.message)) || onwarn(warning);
15+
const dedupe = importee => importee === 'svelte' || importee.startsWith('svelte/');
16+
17+
export default {
18+
client: {
19+
input: config.client.input(),
20+
output: config.client.output(),
21+
plugins: [
22+
replace({
23+
'process.browser': true,
24+
'process.env.NODE_ENV': JSON.stringify(mode)
25+
}),
26+
svelte({
27+
dev,
28+
hydratable: true,
29+
emitCss: true
30+
}),
31+
resolve({
32+
browser: true,
33+
dedupe
34+
}),
35+
commonjs(),
36+
37+
legacy && babel({
38+
extensions: ['.js', '.mjs', '.html', '.svelte'],
39+
runtimeHelpers: true,
40+
exclude: ['node_modules/@babel/**'],
41+
presets: [
42+
['@babel/preset-env', {
43+
targets: '> 0.25%, not dead'
44+
}]
45+
],
46+
plugins: [
47+
'@babel/plugin-syntax-dynamic-import',
48+
['@babel/plugin-transform-runtime', {
49+
useESModules: true
50+
}]
51+
]
52+
}),
53+
54+
!dev && terser({
55+
module: true
56+
})
57+
],
58+
59+
onwarn,
60+
},
61+
62+
server: {
63+
input: config.server.input(),
64+
output: config.server.output(),
65+
plugins: [
66+
replace({
67+
'process.browser': false,
68+
'process.env.NODE_ENV': JSON.stringify(mode)
69+
}),
70+
svelte({
71+
generate: 'ssr',
72+
dev
73+
}),
74+
resolve({
75+
dedupe
76+
}),
77+
commonjs()
78+
],
79+
external: Object.keys(pkg.dependencies).concat(
80+
require('module').builtinModules || Object.keys(process.binding('natives'))
81+
),
82+
83+
onwarn,
84+
},
85+
86+
serviceworker: {
87+
input: config.serviceworker.input(),
88+
output: config.serviceworker.output(),
89+
plugins: [
90+
resolve(),
91+
replace({
92+
'process.browser': true,
93+
'process.env.NODE_ENV': JSON.stringify(mode)
94+
}),
95+
commonjs(),
96+
!dev && terser()
97+
],
98+
99+
onwarn,
100+
}
101+
};

src/client.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as sapper from '@sapper/app';
2+
3+
sapper.start({
4+
target: document.querySelector('#sapper')
5+
});

0 commit comments

Comments
 (0)