From 8b6cd87f50dd3eaa6262c3a38e562a9cdfb3010d Mon Sep 17 00:00:00 2001 From: Michal Vanko Date: Fri, 12 Jul 2019 09:49:03 +0200 Subject: [PATCH] Sapper bootstrap --- .gitattributes | 3 ++ .gitignore | 6 +++ README.md | 88 ++++++++++++++++++++++++++++++++ appveyor.yml | 18 +++++++ cypress.json | 4 ++ cypress/fixtures/example.json | 5 ++ cypress/integration/spec.js | 19 +++++++ cypress/plugins/index.js | 17 +++++++ cypress/support/commands.js | 25 +++++++++ cypress/support/index.js | 20 ++++++++ package.json | 26 ++++++++++ src/client.js | 5 ++ src/components/Nav.svelte | 60 ++++++++++++++++++++++ src/routes/_error.svelte | 40 +++++++++++++++ src/routes/_layout.svelte | 22 ++++++++ src/routes/about.svelte | 7 +++ src/routes/blog/[slug].json.js | 28 +++++++++++ src/routes/blog/[slug].svelte | 64 +++++++++++++++++++++++ src/routes/blog/_posts.js | 92 ++++++++++++++++++++++++++++++++++ src/routes/blog/index.json.js | 16 ++++++ src/routes/blog/index.svelte | 34 +++++++++++++ src/routes/index.svelte | 46 +++++++++++++++++ src/server.js | 17 +++++++ src/service-worker.js | 82 ++++++++++++++++++++++++++++++ src/template.html | 33 ++++++++++++ static/favicon.png | 3 ++ static/global.css | 36 +++++++++++++ static/great-success.png | 3 ++ static/logo-192.png | 3 ++ static/logo-512.png | 3 ++ static/manifest.json | 20 ++++++++ webpack.config.js | 75 +++++++++++++++++++++++++++ 32 files changed, 920 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 README.md create mode 100644 appveyor.yml create mode 100644 cypress.json create mode 100644 cypress/fixtures/example.json create mode 100644 cypress/integration/spec.js create mode 100644 cypress/plugins/index.js create mode 100644 cypress/support/commands.js create mode 100644 cypress/support/index.js create mode 100644 package.json create mode 100644 src/client.js create mode 100644 src/components/Nav.svelte create mode 100644 src/routes/_error.svelte create mode 100644 src/routes/_layout.svelte create mode 100644 src/routes/about.svelte create mode 100644 src/routes/blog/[slug].json.js create mode 100644 src/routes/blog/[slug].svelte create mode 100644 src/routes/blog/_posts.js create mode 100644 src/routes/blog/index.json.js create mode 100644 src/routes/blog/index.svelte create mode 100644 src/routes/index.svelte create mode 100644 src/server.js create mode 100644 src/service-worker.js create mode 100644 src/template.html create mode 100644 static/favicon.png create mode 100644 static/global.css create mode 100644 static/great-success.png create mode 100644 static/logo-192.png create mode 100644 static/logo-512.png create mode 100644 static/manifest.json create mode 100644 webpack.config.js diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1c53b46 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +static/**/**.png filter=lfs diff=lfs merge=lfs -text +static/**/**.jpg filter=lfs diff=lfs merge=lfs -text +static/**/**.gif filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5aea041 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.DS_Store +/node_modules/ +/src/node_modules/@sapper/ +yarn-error.log +/cypress/screenshots/ +/__sapper__/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..491cded --- /dev/null +++ b/README.md @@ -0,0 +1,88 @@ +# sapper-template + +The default [Sapper](https://github.com/sveltejs/sapper) template, with branches for Rollup and webpack. To clone it and get started: + +```bash +# for Rollup +npx degit "sveltejs/sapper-template#rollup" my-app +# for webpack +npx degit "sveltejs/sapper-template#webpack" my-app +cd my-app +npm install # or yarn! +npm run dev +``` + +Open up [localhost:3000](http://localhost:3000) and start clicking around. + +Consult [sapper.svelte.dev](https://sapper.svelte.dev) for help getting started. + + +## Structure + +Sapper expects to find two directories in the root of your project — `src` and `static`. + + +### src + +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. + + +#### src/routes + +This is the heart of your Sapper app. There are two kinds of routes — *pages*, and *server routes*. + +**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.) + +**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. + +There are three simple rules for naming the files that define your routes: + +* 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 +* 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`. +* 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 + + +### static + +The [static](static) directory contains any static assets that should be available. These are served using [sirv](https://github.com/lukeed/sirv). + +In your [service-worker.js](app/service-worker.js) file, you can import these as `files` from the generated manifest... + +```js +import { files } from '@sapper/service-worker'; +``` + +...so that you can cache them (though you can choose not to, for example if you don't want to cache very large files). + + +## Bundler config + +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. + + +## Production mode and deployment + +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. + +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: + +```bash +npm install -g now +now +``` + + +## Using external components + +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. + +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: + +```bash +npm install -D @sveltejs/svelte-virtual-list +``` + + +## Bugs and feedback + +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). diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000..e75da3b --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,18 @@ +version: "{build}" + +shallow_clone: true + +init: + - git config --global core.autocrlf false + +build: off + +environment: + matrix: + # node.js + - nodejs_version: stable + +install: + - ps: Install-Product node $env:nodejs_version + - npm install cypress + - npm install diff --git a/cypress.json b/cypress.json new file mode 100644 index 0000000..f5622fa --- /dev/null +++ b/cypress.json @@ -0,0 +1,4 @@ +{ + "baseUrl": "http://localhost:3000", + "video": false +} \ No newline at end of file diff --git a/cypress/fixtures/example.json b/cypress/fixtures/example.json new file mode 100644 index 0000000..da18d93 --- /dev/null +++ b/cypress/fixtures/example.json @@ -0,0 +1,5 @@ +{ + "name": "Using fixtures to represent data", + "email": "hello@cypress.io", + "body": "Fixtures are a great way to mock data for responses to routes" +} \ No newline at end of file diff --git a/cypress/integration/spec.js b/cypress/integration/spec.js new file mode 100644 index 0000000..9a7140d --- /dev/null +++ b/cypress/integration/spec.js @@ -0,0 +1,19 @@ +describe('Sapper template app', () => { + beforeEach(() => { + cy.visit('/') + }); + + it('has the correct

', () => { + cy.contains('h1', 'Great success!') + }); + + it('navigates to /about', () => { + cy.get('nav a').contains('about').click(); + cy.url().should('include', '/about'); + }); + + it('navigates to /blog', () => { + cy.get('nav a').contains('blog').click(); + cy.url().should('include', '/blog'); + }); +}); \ No newline at end of file diff --git a/cypress/plugins/index.js b/cypress/plugins/index.js new file mode 100644 index 0000000..fd170fb --- /dev/null +++ b/cypress/plugins/index.js @@ -0,0 +1,17 @@ +// *********************************************************** +// This example plugins/index.js can be used to load plugins +// +// You can change the location of this file or turn off loading +// the plugins file with the 'pluginsFile' configuration option. +// +// You can read more here: +// https://on.cypress.io/plugins-guide +// *********************************************************** + +// This function is called when a project is opened or re-opened (e.g. due to +// the project's config changing) + +module.exports = (on, config) => { + // `on` is used to hook into various events Cypress emits + // `config` is the resolved Cypress config +} diff --git a/cypress/support/commands.js b/cypress/support/commands.js new file mode 100644 index 0000000..c1f5a77 --- /dev/null +++ b/cypress/support/commands.js @@ -0,0 +1,25 @@ +// *********************************************** +// This example commands.js shows you how to +// create various custom commands and overwrite +// existing commands. +// +// For more comprehensive examples of custom +// commands please read more here: +// https://on.cypress.io/custom-commands +// *********************************************** +// +// +// -- This is a parent command -- +// Cypress.Commands.add("login", (email, password) => { ... }) +// +// +// -- This is a child command -- +// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... }) +// +// +// -- This is a dual command -- +// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... }) +// +// +// -- This is will overwrite an existing command -- +// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }) diff --git a/cypress/support/index.js b/cypress/support/index.js new file mode 100644 index 0000000..d68db96 --- /dev/null +++ b/cypress/support/index.js @@ -0,0 +1,20 @@ +// *********************************************************** +// This example support/index.js is processed and +// loaded automatically before your test files. +// +// This is a great place to put global configuration and +// behavior that modifies Cypress. +// +// You can change the location of this file or turn off +// automatically serving support files with the +// 'supportFile' configuration option. +// +// You can read more here: +// https://on.cypress.io/configuration +// *********************************************************** + +// Import commands.js using ES2015 syntax: +import './commands' + +// Alternatively you can use CommonJS syntax: +// require('./commands') diff --git a/package.json b/package.json new file mode 100644 index 0000000..d9403c2 --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "TODO", + "description": "TODO", + "version": "0.0.1", + "scripts": { + "dev": "sapper dev", + "build": "sapper build", + "export": "sapper export", + "start": "node __sapper__/build", + "cy:run": "cypress run", + "cy:open": "cypress open", + "test": "run-p --race dev cy:run" + }, + "dependencies": { + "compression": "^1.7.1", + "polka": "^0.5.0", + "sirv": "^0.4.0" + }, + "devDependencies": { + "npm-run-all": "^4.1.5", + "sapper": "^0.27.0", + "svelte": "^3.0.0", + "svelte-loader": "^2.9.0", + "webpack": "^4.7.0" + } +} diff --git a/src/client.js b/src/client.js new file mode 100644 index 0000000..cec9172 --- /dev/null +++ b/src/client.js @@ -0,0 +1,5 @@ +import * as sapper from '@sapper/app'; + +sapper.start({ + target: document.querySelector('#sapper') +}); \ No newline at end of file diff --git a/src/components/Nav.svelte b/src/components/Nav.svelte new file mode 100644 index 0000000..63d5150 --- /dev/null +++ b/src/components/Nav.svelte @@ -0,0 +1,60 @@ + + + + + \ No newline at end of file diff --git a/src/routes/_error.svelte b/src/routes/_error.svelte new file mode 100644 index 0000000..320e587 --- /dev/null +++ b/src/routes/_error.svelte @@ -0,0 +1,40 @@ + + + + + + {status} + + +

{status}

+ +

{error.message}

+ +{#if dev && error.stack} +
{error.stack}
+{/if} diff --git a/src/routes/_layout.svelte b/src/routes/_layout.svelte new file mode 100644 index 0000000..8432299 --- /dev/null +++ b/src/routes/_layout.svelte @@ -0,0 +1,22 @@ + + + + +