diff --git a/client/.gitignore b/client/.gitignore new file mode 100644 index 0000000..5aea041 --- /dev/null +++ b/client/.gitignore @@ -0,0 +1,6 @@ +.DS_Store +/node_modules/ +/src/node_modules/@sapper/ +yarn-error.log +/cypress/screenshots/ +/__sapper__/ diff --git a/client/README.md b/client/README.md new file mode 100644 index 0000000..fecc155 --- /dev/null +++ b/client/README.md @@ -0,0 +1,109 @@ +# sapper-template + +The default [Sapper](https://github.com/sveltejs/sapper) template, available for Rollup and webpack. + + +## Getting started + + +### Using `degit` + +[`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`: + +```bash +# for Rollup +npx degit "sveltejs/sapper-template#rollup" my-app +# for webpack +npx degit "sveltejs/sapper-template#webpack" my-app +``` + + +### Using GitHub templates + +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. + + +### Running the project + +However you get the code, you can install dependencies and run the project in development mode with: + +```bash +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](src/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/client/cypress.json b/client/cypress.json new file mode 100644 index 0000000..f5622fa --- /dev/null +++ b/client/cypress.json @@ -0,0 +1,4 @@ +{ + "baseUrl": "http://localhost:3000", + "video": false +} \ No newline at end of file diff --git a/client/cypress/fixtures/example.json b/client/cypress/fixtures/example.json new file mode 100644 index 0000000..da18d93 --- /dev/null +++ b/client/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/client/cypress/integration/spec.js b/client/cypress/integration/spec.js new file mode 100644 index 0000000..9a7140d --- /dev/null +++ b/client/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/client/cypress/plugins/index.js b/client/cypress/plugins/index.js new file mode 100644 index 0000000..fd170fb --- /dev/null +++ b/client/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/client/cypress/support/commands.js b/client/cypress/support/commands.js new file mode 100644 index 0000000..c1f5a77 --- /dev/null +++ b/client/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/client/cypress/support/index.js b/client/cypress/support/index.js new file mode 100644 index 0000000..d68db96 --- /dev/null +++ b/client/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/client/package.json b/client/package.json new file mode 100644 index 0000000..3644595 --- /dev/null +++ b/client/package.json @@ -0,0 +1,36 @@ +{ + "name": "TODO", + "description": "TODO", + "version": "0.0.1", + "scripts": { + "dev": "sapper dev", + "build": "sapper build --legacy", + "export": "sapper export --legacy", + "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": "next", + "sirv": "^0.4.0" + }, + "devDependencies": { + "npm-run-all": "^4.1.5", + "sapper": "^0.27.0", + "svelte": "^3.0.0", + "@babel/core": "^7.0.0", + "@babel/plugin-syntax-dynamic-import": "^7.0.0", + "@babel/plugin-transform-runtime": "^7.0.0", + "@babel/preset-env": "^7.0.0", + "@babel/runtime": "^7.0.0", + "rollup": "^1.12.0", + "rollup-plugin-babel": "^4.0.2", + "rollup-plugin-commonjs": "^10.0.0", + "rollup-plugin-node-resolve": "^5.2.0", + "rollup-plugin-replace": "^2.0.0", + "rollup-plugin-svelte": "^5.0.1", + "rollup-plugin-terser": "^4.0.4" + } +} diff --git a/client/rollup.config.js b/client/rollup.config.js new file mode 100644 index 0000000..ac0b39b --- /dev/null +++ b/client/rollup.config.js @@ -0,0 +1,101 @@ +import resolve from 'rollup-plugin-node-resolve'; +import replace from 'rollup-plugin-replace'; +import commonjs from 'rollup-plugin-commonjs'; +import svelte from 'rollup-plugin-svelte'; +import babel from 'rollup-plugin-babel'; +import { terser } from 'rollup-plugin-terser'; +import config from 'sapper/config/rollup.js'; +import pkg from './package.json'; + +const mode = process.env.NODE_ENV; +const dev = mode === 'development'; +const legacy = !!process.env.SAPPER_LEGACY_BUILD; + +const onwarn = (warning, onwarn) => (warning.code === 'CIRCULAR_DEPENDENCY' && /[/\\]@sapper[/\\]/.test(warning.message)) || onwarn(warning); +const dedupe = importee => importee === 'svelte' || importee.startsWith('svelte/'); + +export default { + client: { + input: config.client.input(), + output: config.client.output(), + plugins: [ + replace({ + 'process.browser': true, + 'process.env.NODE_ENV': JSON.stringify(mode) + }), + svelte({ + dev, + hydratable: true, + emitCss: true + }), + resolve({ + browser: true, + dedupe + }), + commonjs(), + + legacy && babel({ + extensions: ['.js', '.mjs', '.html', '.svelte'], + runtimeHelpers: true, + exclude: ['node_modules/@babel/**'], + presets: [ + ['@babel/preset-env', { + targets: '> 0.25%, not dead' + }] + ], + plugins: [ + '@babel/plugin-syntax-dynamic-import', + ['@babel/plugin-transform-runtime', { + useESModules: true + }] + ] + }), + + !dev && terser({ + module: true + }) + ], + + onwarn, + }, + + server: { + input: config.server.input(), + output: config.server.output(), + plugins: [ + replace({ + 'process.browser': false, + 'process.env.NODE_ENV': JSON.stringify(mode) + }), + svelte({ + generate: 'ssr', + dev + }), + resolve({ + dedupe + }), + commonjs() + ], + external: Object.keys(pkg.dependencies).concat( + require('module').builtinModules || Object.keys(process.binding('natives')) + ), + + onwarn, + }, + + serviceworker: { + input: config.serviceworker.input(), + output: config.serviceworker.output(), + plugins: [ + resolve(), + replace({ + 'process.browser': true, + 'process.env.NODE_ENV': JSON.stringify(mode) + }), + commonjs(), + !dev && terser() + ], + + onwarn, + } +}; diff --git a/client/src/client.js b/client/src/client.js new file mode 100644 index 0000000..cec9172 --- /dev/null +++ b/client/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/client/src/components/Nav.svelte b/client/src/components/Nav.svelte new file mode 100644 index 0000000..63d5150 --- /dev/null +++ b/client/src/components/Nav.svelte @@ -0,0 +1,60 @@ + + + + + \ No newline at end of file diff --git a/client/src/routes/_error.svelte b/client/src/routes/_error.svelte new file mode 100644 index 0000000..320e587 --- /dev/null +++ b/client/src/routes/_error.svelte @@ -0,0 +1,40 @@ + + + + + + {status} + + +

{status}

+ +

{error.message}

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