diff --git a/.gitignore b/.gitignore index f20aae2..7800c23 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ node_modules yarn.lock cypress/screenshots templates/.* -dist \ No newline at end of file +dist +app/manifest \ No newline at end of file diff --git a/app/client.js b/app/client.js new file mode 100644 index 0000000..05d30aa --- /dev/null +++ b/app/client.js @@ -0,0 +1,7 @@ +import { init } from 'sapper/runtime.js'; +import { routes } from './manifest/client.js'; + +// `routes` is an array of route objects injected by Sapper +init(document.querySelector('#sapper'), routes); + +if (module.hot) module.hot.accept(); \ No newline at end of file diff --git a/server.js b/app/server.js similarity index 52% rename from server.js rename to app/server.js index cd79704..0af7864 100644 --- a/server.js +++ b/app/server.js @@ -1,13 +1,16 @@ -const fs = require('fs'); -const app = require('express')(); -const compression = require('compression'); -const sapper = require('sapper'); -const static = require('serve-static'); +import fs from 'fs'; +import express from 'express'; +import compression from 'compression'; +import sapper from 'sapper'; +import serve from 'serve-static'; +import fetch from 'node-fetch'; +import { routes } from './manifest/server.js'; + +const app = express(); const { PORT = 3000 } = process.env; // this allows us to do e.g. `fetch('/api/blog-posts')` on the server -const fetch = require('node-fetch'); global.fetch = (url, opts) => { if (url[0] === '/') url = `http://localhost:${PORT}${url}`; return fetch(url, opts); @@ -15,9 +18,11 @@ global.fetch = (url, opts) => { app.use(compression({ threshold: 0 })); -app.use(static('assets')); +app.use(serve('assets')); -app.use(sapper()); +app.use(sapper({ + routes +})); app.listen(PORT, () => { console.log(`listening on port ${PORT}`); diff --git a/templates/service-worker.js b/app/service-worker.js similarity index 90% rename from templates/service-worker.js rename to app/service-worker.js index cccb12a..542371d 100644 --- a/templates/service-worker.js +++ b/app/service-worker.js @@ -1,15 +1,12 @@ -const timestamp = '__timestamp__'; +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 to_cache = shell.concat(assets); const cached = new Set(to_cache); -// `routes` is an array of `{ pattern: RegExp }` objects that -// match the pages in your app -const routes = __routes__; - self.addEventListener('install', event => { event.waitUntil( caches diff --git a/templates/2xx.html b/app/template.html similarity index 89% rename from templates/2xx.html rename to app/template.html index 5dc665a..a7e7d49 100644 --- a/templates/2xx.html +++ b/app/template.html @@ -10,9 +10,9 @@ - %sapper.status% - - - - -

%sapper.title%

-

Could not %sapper.method% %sapper.url%

- - %sapper.scripts% - - \ No newline at end of file diff --git a/templates/5xx.html b/templates/5xx.html deleted file mode 100644 index 357152d..0000000 --- a/templates/5xx.html +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - - - - %sapper.status% - - - - -

%sapper.title%

-
%sapper.error%
-
%sapper.stack%
- - \ No newline at end of file diff --git a/templates/main.js b/templates/main.js deleted file mode 100644 index aa08cb0..0000000 --- a/templates/main.js +++ /dev/null @@ -1,4 +0,0 @@ -import { init } from 'sapper/runtime.js'; - -// `routes` is an array of route objects injected by Sapper -init(document.querySelector('#sapper'), __routes__); \ No newline at end of file diff --git a/webpack.client.config.js b/webpack/client.config.js similarity index 100% rename from webpack.client.config.js rename to webpack/client.config.js diff --git a/webpack.server.config.js b/webpack/server.config.js similarity index 100% rename from webpack.server.config.js rename to webpack/server.config.js diff --git a/webpack/service-worker.config.js b/webpack/service-worker.config.js new file mode 100644 index 0000000..d29ca30 --- /dev/null +++ b/webpack/service-worker.config.js @@ -0,0 +1,17 @@ +const path = require('path'); +const config = require('sapper/webpack/config.js'); +const webpack = require('webpack'); + +module.exports = { + entry: { + 'service-worker': './app/service-worker.js' + }, + output: { + path: path.resolve(`.sapper`), + filename: '[name].js', + chunkFilename: '[name].[id].[hash].js' + }, + plugins: [ + !config.dev && new webpack.optimize.ModuleConcatenationPlugin() + ].filter(Boolean) +}; \ No newline at end of file