commit a69b70e7454e67185c30ad1cb4ce34d296246f5e Author: Rich Harris Date: Sat Aug 25 13:03:34 2018 -0400 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..33cade9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +.DS_Store +node_modules +/.sapper/ +yarn.lock +yarn-error.log +/cypress/screenshots/ +/templates/.* +/export/ +/build/ +/app/manifest/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8ad1fc3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ +sudo: false +language: node_js +node_js: + - "stable" +env: + global: + - BUILD_TIMEOUT=10000 +install: + - npm install + - npm install cypress + diff --git a/README.md b/README.md new file mode 100644 index 0000000..4897f79 --- /dev/null +++ b/README.md @@ -0,0 +1,93 @@ +# sapper-template-rollup + +**EXPERIMENTAL!** See the [related PR](https://github.com/sveltejs/sapper/pull/379) for more information. You will need to install Sapper from that branch (or clone and link it) to use this template. + +Original README follows: + +--- + +# sapper-template + +The default [Sapper](https://github.com/sveltejs/sapper) template. To clone it and get started: + +```bash +npx degit sveltejs/sapper-template 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.technology](https://sapper.svelte.technology) for help getting started. + + +## Structure + +Sapper expects to find three directories in the root of your project — `app`, `assets` and `routes`. + + +### app + +The [app](app) directory contains the entry points for your app — `client.js`, `server.js` and (optionally) a `service-worker.js` — along with a `template.html` file. + + +### assets + +The [assets](assets) 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 `assets` from the generated manifest... + +```js +import { assets } from './manifest/service-worker.js'; +``` + +...so that you can cache them (though you can choose not to, for example if you don't want to cache very large files). + + +### 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 `.html` 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 `routes/about.html` corresponds to the `/about` route. A file called `routes/blog/[slug].html` corresponds to the `/blog/:slug` route, in which case `params.slug` is available to the route +* The file `routes/index.html` (or `routes/index.js`) corresponds to the root of your app. `routes/about/index.html` is treated the same as `routes/about.html`. +* 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 `routes/_helpers/datetime.js` and it would *not* create a `/_helpers/datetime` route + + +## Webpack config + +Sapper uses webpack to provide code-splitting, dynamic imports and hot module reloading, as well as compiling your Svelte components. As long as you don't do anything daft, you can edit the configuration files to add whatever loaders and 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 hot module replacement, and activate the appropriate webpack 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 webpack doesn't treat the package as an *external dependency*. You can either modify the `externals` option in [webpack/server.config.js](webpack/server.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 +yarn add -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/app/client.js b/app/client.js new file mode 100644 index 0000000..b83ce1a --- /dev/null +++ b/app/client.js @@ -0,0 +1,7 @@ +import { init } from 'sapper/runtime.js'; +import { manifest } from './manifest/client.js'; + +init({ + target: document.querySelector('#sapper'), + manifest +}); \ No newline at end of file diff --git a/app/server.js b/app/server.js new file mode 100644 index 0000000..76cf0a0 --- /dev/null +++ b/app/server.js @@ -0,0 +1,19 @@ +import sirv from 'sirv'; +import polka from 'polka'; +import sapper from 'sapper'; +import compression from 'compression'; +import { manifest } from './manifest/server.js'; + +const { PORT, NODE_ENV } = process.env; +const dev = NODE_ENV === 'development'; + +polka() // You can also use Express + .use( + compression({ threshold: 0 }), + sirv('assets', { dev }), + sapper({ manifest }) + ) + .listen(PORT) + .catch(err => { + console.log('error', err); + }) diff --git a/app/service-worker.js b/app/service-worker.js new file mode 100644 index 0000000..c102c12 --- /dev/null +++ b/app/service-worker.js @@ -0,0 +1,82 @@ +import { timestamp, assets, shell, routes } from './manifest/service-worker.js'; + +const ASSETS = `cache${timestamp}`; + +// `shell` is an array of all the files generated by webpack, +// `assets` is an array of everything in the `assets` directory +const to_cache = shell.concat(assets); +const cached = new Set(to_cache); + +self.addEventListener('install', event => { + event.waitUntil( + caches + .open(ASSETS) + .then(cache => cache.addAll(to_cache)) + .then(() => { + self.skipWaiting(); + }) + ); +}); + +self.addEventListener('activate', event => { + event.waitUntil( + caches.keys().then(async keys => { + // delete old caches + for (const key of keys) { + if (key !== ASSETS) await caches.delete(key); + } + + self.clients.claim(); + }) + ); +}); + +self.addEventListener('fetch', event => { + if (event.request.method !== 'GET') return; + + const url = new URL(event.request.url); + + // don't try to handle e.g. data: URIs + if (!url.protocol.startsWith('http')) return; + + // ignore dev server requests + if (url.hostname === self.location.hostname && url.port !== self.location.port) return; + + // always serve assets and webpack-generated files from cache + if (url.host === self.location.host && cached.has(url.pathname)) { + event.respondWith(caches.match(event.request)); + return; + } + + // for pages, you might want to serve a shell `index.html` file, + // which Sapper has generated for you. It's not right for every + // app, but if it's right for yours then uncomment this section + /* + if (url.origin === self.origin && routes.find(route => route.pattern.test(url.pathname))) { + event.respondWith(caches.match('/index.html')); + return; + } + */ + + if (event.request.cache === 'only-if-cached') return; + + // for everything else, try the network first, falling back to + // cache if the user is offline. (If the pages never change, you + // might prefer a cache-first approach to a network-first one.) + event.respondWith( + caches + .open(`offline${timestamp}`) + .then(async cache => { + try { + const response = await fetch(event.request); + cache.put(event.request, response.clone()); + return response; + } catch(err) { + const response = await cache.match(event.request); + if (response) return response; + + throw err; + } + }) + ); +}); diff --git a/app/template.html b/app/template.html new file mode 100644 index 0000000..54906f7 --- /dev/null +++ b/app/template.html @@ -0,0 +1,33 @@ + + + + + + + + %sapper.base% + + + + + + + %sapper.styles% + + + %sapper.head% + + + +
%sapper.html%
+ + + %sapper.scripts% + + 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/assets/favicon.png b/assets/favicon.png new file mode 100644 index 0000000..9378861 Binary files /dev/null and b/assets/favicon.png differ diff --git a/assets/global.css b/assets/global.css new file mode 100644 index 0000000..3566e73 --- /dev/null +++ b/assets/global.css @@ -0,0 +1,36 @@ +body { + margin: 0; + font-family: Roboto, -apple-system, BlinkMacSystemFont, Segoe UI, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif; + font-size: 14px; + line-height: 1.5; + color: #333; +} + +h1, h2, h3, h4, h5, h6 { + margin: 0 0 0.5em 0; + font-weight: 400; + line-height: 1.2; +} + +h1 { + font-size: 2em; +} + +a { + color: inherit; +} + +code { + font-family: menlo, inconsolata, monospace; + font-size: calc(1em - 2px); + color: #555; + background-color: #f0f0f0; + padding: 0.2em 0.4em; + border-radius: 2px; +} + +@media (min-width: 400px) { + body { + font-size: 16px; + } +} \ No newline at end of file diff --git a/assets/great-success.png b/assets/great-success.png new file mode 100644 index 0000000..c38720c Binary files /dev/null and b/assets/great-success.png differ diff --git a/assets/manifest.json b/assets/manifest.json new file mode 100644 index 0000000..2b4ac3e --- /dev/null +++ b/assets/manifest.json @@ -0,0 +1,20 @@ +{ + "background_color": "#ffffff", + "theme_color": "#aa1e1e", + "name": "TODO", + "short_name": "TODO", + "display": "minimal-ui", + "start_url": "/", + "icons": [ + { + "src": "svelte-logo-192.png", + "sizes": "192x192", + "type": "image/png" + }, + { + "src": "svelte-logo-512.png", + "sizes": "512x512", + "type": "image/png" + } + ] +} diff --git a/assets/svelte-logo-192.png b/assets/svelte-logo-192.png new file mode 100644 index 0000000..3073b2d Binary files /dev/null and b/assets/svelte-logo-192.png differ diff --git a/assets/svelte-logo-512.png b/assets/svelte-logo-512.png new file mode 100644 index 0000000..454e5f2 Binary files /dev/null and b/assets/svelte-logo-512.png differ diff --git a/components/Nav.html b/components/Nav.html new file mode 100644 index 0000000..23c0631 --- /dev/null +++ b/components/Nav.html @@ -0,0 +1,56 @@ + + + \ No newline at end of file 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..87f7da1 --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "TODO", + "description": "TODO", + "version": "0.0.1", + "scripts": { + "dev": "sapper dev", + "build": "sapper build", + "export": "sapper export", + "start": "node build", + "cy:run": "cypress run", + "cy:open": "cypress open", + "test": "run-p --race dev cy:run" + }, + "dependencies": { + "compression": "^1.7.1", + "polka": "^0.4.0", + "sapper": "^0.17.0", + "sirv": "^0.2.0" + }, + "devDependencies": { + "@ampproject/rollup-plugin-closure-compiler": "^0.6.1", + "npm-run-all": "^4.1.2", + "rollup": "^0.64.1", + "rollup-plugin-commonjs": "^9.1.6", + "rollup-plugin-node-resolve": "^3.3.0", + "rollup-plugin-replace": "^2.0.0", + "rollup-plugin-svelte": "^4.2.1", + "rollup-plugin-terser": "^1.0.1", + "svelte": "^2.0.0" + } +} diff --git a/rollup/client.config.js b/rollup/client.config.js new file mode 100644 index 0000000..b21f725 --- /dev/null +++ b/rollup/client.config.js @@ -0,0 +1,34 @@ +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 { terser } from 'rollup-plugin-terser'; +import config from 'sapper/rollup.js'; +import pkg from '../package.json'; + +const mode = process.env.NODE_ENV; +const dev = mode === 'development'; +const prod = !dev; + +export default { + input: config.client.input(), + output: config.client.output(), + plugins: [ + resolve(), + replace({ + 'process.browser': true, + 'process.env.NODE_ENV': JSON.stringify(mode) + }), + commonjs(), + svelte({ + dev, + hydratable: true + }), + + prod && terser() + ], + external: Object.keys(pkg.dependencies).concat( + require('module').builtinModules + ), + experimentalCodeSplitting: true +}; \ No newline at end of file diff --git a/rollup/server.config.js b/rollup/server.config.js new file mode 100644 index 0000000..5152870 --- /dev/null +++ b/rollup/server.config.js @@ -0,0 +1,32 @@ +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 config from 'sapper/rollup.js'; +import pkg from '../package.json'; + +const mode = process.env.NODE_ENV; +const dev = mode === 'development'; +const prod = !dev; + +export default { + input: config.server.input(), + output: config.server.output(), + plugins: [ + resolve(), + replace({ + 'process.browser': true, + 'process.env.NODE_ENV': JSON.stringify(mode) + }), + commonjs(), + svelte({ + generate: 'ssr', + dev, + hydratable: true + }) + ], + external: Object.keys(pkg.dependencies).concat( + require('module').builtinModules + ), + experimentalCodeSplitting: true +}; \ No newline at end of file diff --git a/rollup/service-worker.config.js b/rollup/service-worker.config.js new file mode 100644 index 0000000..143cdbb --- /dev/null +++ b/rollup/service-worker.config.js @@ -0,0 +1,28 @@ +import resolve from 'rollup-plugin-node-resolve'; +import replace from 'rollup-plugin-replace'; +import commonjs from 'rollup-plugin-commonjs'; +import { terser } from 'rollup-plugin-terser'; +import config from 'sapper/rollup.js'; +import pkg from '../package.json'; + +const mode = process.env.NODE_ENV; +const dev = mode === 'development'; +const prod = !dev; + +export default { + input: config.serviceworker.input(), + output: config.serviceworker.output(), + plugins: [ + resolve(), + replace({ + 'process.browser': true, + 'process.env.NODE_ENV': JSON.stringify(mode) + }), + commonjs(), + prod && terser() + ], + external: Object.keys(pkg.dependencies).concat( + require('module').builtinModules + ), + experimentalCodeSplitting: true +}; \ No newline at end of file diff --git a/routes/_error.html b/routes/_error.html new file mode 100644 index 0000000..72b3395 --- /dev/null +++ b/routes/_error.html @@ -0,0 +1,29 @@ + + {status} + + +

{status}

+ +

{error.message}

+ + \ No newline at end of file diff --git a/routes/_layout.html b/routes/_layout.html new file mode 100644 index 0000000..efa6c15 --- /dev/null +++ b/routes/_layout.html @@ -0,0 +1,24 @@ +