docs: set-up sapper template

This commit is contained in:
Eric Liu 2020-05-09 07:19:03 -07:00
commit 04b8978ec2
22 changed files with 5209 additions and 0 deletions

5
docs/.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
.DS_Store
/__sapper__
/**/node_modules
/public
yarn-error.log

30
docs/package.json Normal file
View file

@ -0,0 +1,30 @@
{
"name": "sapper-webpack",
"description": "Custom Sapper webpack template",
"version": "0.0.1",
"private": true,
"scripts": {
"develop": "sapper dev",
"build": "sapper export"
},
"dependencies": {
"polka": "next",
"sirv": "^0.4.0"
},
"devDependencies": {
"@metonym/sapper": "^0.27.12",
"autoprefixer": "^9.7.6",
"carbon-components": "^10.11.2",
"css-loader": "^3.5.3",
"mini-css-extract-plugin": "^0.9.0",
"node-sass": "^4.14.1",
"optimize-css-assets-webpack-plugin": "^5.0.3",
"postcss-loader": "^3.0.0",
"sass-loader": "^8.0.2",
"style-loader": "^1.2.1",
"svelte": "~3.20.0",
"svelte-loader": "^2.9.0",
"webpack": "^4.43.0",
"carbon-components-svelte": "../"
}
}

4
docs/src/client.js Normal file
View file

@ -0,0 +1,4 @@
import "./style.scss";
import * as sapper from "@sapper/app";
sapper.start({ target: document.querySelector("#sapper") });

View file

@ -0,0 +1,20 @@
<script>
export let segment;
import {
Button,
Header,
HeaderNav,
HeaderNavItem
} from "carbon-components-svelte";
$: {
console.log(segment);
}
</script>
<Header company="" platformName="Carbon Components Svelte" href="/">
<HeaderNav>
<HeaderNavItem href="/about" text="Link 1" />
</HeaderNav>
</Header>

View file

@ -0,0 +1,18 @@
<script>
export let status;
export let error;
const dev = process.env.NODE_ENV === "development";
</script>
<svelte:head>
<title>{status}</title>
</svelte:head>
<h1>{status}</h1>
<p>{error.message}</p>
{#if dev && error.stack}
<pre>{error.stack}</pre>
{/if}

View file

@ -0,0 +1,17 @@
<script>
import Nav from "../components/Nav.svelte";
export let segment;
</script>
<style>
main {
margin-top: 3rem;
}
</style>
<Nav {segment} />
<main>
<slot />
</main>

View file

@ -0,0 +1,18 @@
<script>
import {
Button,
Header,
HeaderNav,
HeaderNavItem
} from "carbon-components-svelte";
import { theme } from "../store";
</script>
<svelte:head>
<title>About</title>
</svelte:head>
<h1>About this site</h1>
{$theme}
<Button>Hello world</Button>
<p>This is the 'about' page. There's not much here.</p>

View file

@ -0,0 +1,21 @@
import posts from "./_posts.js";
const lookup = new Map();
posts.forEach((post) => {
lookup.set(post.slug, JSON.stringify(post));
});
export function get(req, res, next) {
// the `slug` parameter is available because
// this file is called [slug].json.js
const { slug } = req.params;
if (lookup.has(slug)) {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(lookup.get(slug));
} else {
res.writeHead(404, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: `Not found` }));
}
}

View file

@ -0,0 +1,28 @@
<script context="module">
export async function preload({ params, query }) {
// the `slug` parameter is available because
// this file is called [slug].svelte
const res = await this.fetch(`blog/${params.slug}.json`);
const data = await res.json();
if (res.status === 200) {
return { post: data };
} else {
this.error(res.status, data.message);
}
}
</script>
<script>
export let post;
</script>
<svelte:head>
<title>{post.title}</title>
</svelte:head>
<h1>{post.title}</h1>
<div class="content">
{@html post.html}
</div>

View file

@ -0,0 +1,92 @@
// Ordinarily, you'd generate this data from markdown files in your
// repo, or fetch them from a database of some kind. But in order to
// avoid unnecessary dependencies in the starter template, and in the
// service of obviousness, we're just going to leave it here.
// This file is called `_posts.js` rather than `posts.js`, because
// we don't want to create an `/blog/posts` route — the leading
// underscore tells Sapper not to do that.
const posts = [
{
title: "What is Sapper?",
slug: "what-is-sapper",
html: `
<p>First, you have to know what <a href='https://svelte.dev'>Svelte</a> is. Svelte is a UI framework with a bold new idea: rather than providing a library that you write code with (like React or Vue, for example), it's a compiler that turns your components into highly optimized vanilla JavaScript. If you haven't already read the <a href='https://svelte.dev/blog/frameworks-without-the-framework'>introductory blog post</a>, you should!</p>
<p>Sapper is a Next.js-style framework (<a href='blog/how-is-sapper-different-from-next'>more on that here</a>) built around Svelte. It makes it embarrassingly easy to create extremely high performance web apps. Out of the box, you get:</p>
<ul>
<li>Code-splitting, dynamic imports and hot module replacement, powered by webpack</li>
<li>Server-side rendering (SSR) with client-side hydration</li>
<li>Service worker for offline support, and all the PWA bells and whistles</li>
<li>The nicest development experience you've ever had, or your money back</li>
</ul>
<p>It's implemented as Express middleware. Everything is set up and waiting for you to get started, but you keep complete control over the server, service worker, webpack config and everything else, so it's as flexible as you need it to be.</p>
`,
},
{
title: "How to use Sapper",
slug: "how-to-use-sapper",
html: `
<h2>Step one</h2>
<p>Create a new project, using <a href='https://github.com/Rich-Harris/degit'>degit</a>:</p>
<pre><code>npx degit "sveltejs/sapper-template#rollup" my-app
cd my-app
npm install # or yarn!
npm run dev
</code></pre>
<h2>Step two</h2>
<p>Go to <a href='http://localhost:3000'>localhost:3000</a>. Open <code>my-app</code> in your editor. Edit the files in the <code>src/routes</code> directory or add new ones.</p>
<h2>Step three</h2>
<p>...</p>
<h2>Step four</h2>
<p>Resist overdone joke formats.</p>
`,
},
{
title: "Why the name?",
slug: "why-the-name",
html: `
<p>In war, the soldiers who build bridges, repair roads, clear minefields and conduct demolitions all under combat conditions are known as <em>sappers</em>.</p>
<p>For web developers, the stakes are generally lower than those for combat engineers. But we face our own hostile environment: underpowered devices, poor network connections, and the complexity inherent in front-end engineering. Sapper, which is short for <strong>S</strong>velte <strong>app</strong> mak<strong>er</strong>, is your courageous and dutiful ally.</p>
`,
},
{
title: "How is Sapper different from Next.js?",
slug: "how-is-sapper-different-from-next",
html: `
<p><a href='https://github.com/zeit/next.js'>Next.js</a> is a React framework from <a href='https://zeit.co'>Zeit</a>, and is the inspiration for Sapper. There are a few notable differences, however:</p>
<ul>
<li>It's powered by <a href='https://svelte.dev'>Svelte</a> instead of React, so it's faster and your apps are smaller</li>
<li>Instead of route masking, we encode route parameters in filenames. For example, the page you're looking at right now is <code>src/routes/blog/[slug].svelte</code></li>
<li>As well as pages (Svelte components, which render on server or client), you can create <em>server routes</em> in your <code>routes</code> directory. These are just <code>.js</code> files that export functions corresponding to HTTP methods, and receive Express <code>request</code> and <code>response</code> objects as arguments. This makes it very easy to, for example, add a JSON API such as the one <a href='blog/how-is-sapper-different-from-next.json'>powering this very page</a></li>
<li>Links are just <code>&lt;a&gt;</code> elements, rather than framework-specific <code>&lt;Link&gt;</code> components. That means, for example, that <a href='blog/how-can-i-get-involved'>this link right here</a>, despite being inside a blob of HTML, works with the router as you'd expect.</li>
</ul>
`,
},
{
title: "How can I get involved?",
slug: "how-can-i-get-involved",
html: `
<p>We're so glad you asked! Come on over to the <a href='https://github.com/sveltejs/svelte'>Svelte</a> and <a href='https://github.com/sveltejs/sapper'>Sapper</a> repos, and join us in the <a href='https://svelte.dev/chat'>Discord chatroom</a>. Everyone is welcome, especially you!</p>
`,
},
];
posts.forEach((post) => {
post.html = post.html.replace(/^\t{3}/gm, "");
});
export default posts;

View file

@ -0,0 +1,6 @@
import posts from "./_posts.js";
export function get(req, res) {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(posts));
}

View file

@ -0,0 +1,34 @@
<script context="module">
export function preload({ params, query }) {
return this.fetch(`blog.json`)
.then(r => r.json())
.then(posts => {
return { posts };
});
}
</script>
<script>
export let posts;
</script>
<style>
ul {
margin: 0 0 1em 0;
line-height: 1.5;
}
</style>
<svelte:head>
<title>Blog</title>
</svelte:head>
<h1>Recent posts</h1>
<ul>
{#each posts as post}
<li>
<a rel="prefetch" href="blog/{post.slug}">{post.title}</a>
</li>
{/each}
</ul>

View file

@ -0,0 +1,36 @@
<script>
export let inline = false;
import { onMount, afterUpdate } from "svelte";
import { Select, SelectItem } from "carbon-components-svelte";
import { theme } from "../store";
onMount(() => {
let currentTheme = localStorage.getItem("theme");
if (currentTheme) {
theme.set(currentTheme);
} else {
if (
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches
) {
theme.set("g90");
}
}
});
afterUpdate(() => {
localStorage.setItem("theme", $theme);
document.documentElement.setAttribute("carbon-theme", $theme);
});
</script>
<Select {inline} labelText="Theme" bind:selected={$theme}>
<SelectItem value="white" text="White" />
<SelectItem value="g10" text="Gray 10" />
<SelectItem value="g90" text="Gray 90" />
<SelectItem value="g100" text="Gray 100" />
</Select>
<!--
<iframe width="900" height="500" src="about/" /> -->

12
docs/src/server.js Normal file
View file

@ -0,0 +1,12 @@
import sirv from "sirv";
import polka from "polka";
import * as sapper from "@sapper/server";
const { PORT, NODE_ENV } = process.env;
const dev = NODE_ENV === "development";
polka()
.use(sirv("static", { dev }), sapper.middleware())
.listen(PORT, (err) => {
if (err) console.log("error", err);
});

5
docs/src/store/index.js Normal file
View file

@ -0,0 +1,5 @@
import { writable } from "svelte/store";
export const sideNavToggled = writable(false);
export const appSwitcherToggled = writable(false);
export const theme = writable("g10");

42
docs/src/style.scss Normal file
View file

@ -0,0 +1,42 @@
$feature-flags: (
grid-columns-16: true,
enable-css-custom-properties: true
);
@import 'carbon-components/scss/globals/scss/_css--helpers.scss';
@import 'carbon-components/scss/globals/scss/vendor/@carbon/elements/scss/themes/_mixins.scss';
:root {
@include carbon--theme($carbon--theme--white, true);
}
:root[carbon-theme='g10'] {
@include carbon--theme($carbon--theme--g10, true);
}
:root[carbon-theme='g90'] {
@include carbon--theme($carbon--theme--g90, true);
}
:root[carbon-theme='g100'] {
@include carbon--theme($carbon--theme--g100, true);
}
$css--font-face: true;
$css--helpers: true;
$css--body: true;
$css--use-layer: true;
$css--reset: true;
$css--default-type: true;
$css--plex: true;
@import '~carbon-components/scss/globals/scss/_css--reset.scss';
@import '~carbon-components/scss/globals/scss/_css--font-face.scss';
@import '~carbon-components/scss/globals/scss/_css--helpers.scss';
@import '~carbon-components/scss/globals/scss/_css--body.scss';
@import '~carbon-components/scss/globals/grid/_grid.scss';
@import '~carbon-components/scss/globals/scss/styles.scss';
/* @import '~carbon-components/scss/components/button/button';
@import '~carbon-components/scss/components/select/select'; */

14
docs/src/template.html Normal file
View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
%sapper.base%
<link rel="icon" type="image/png" href="favicon.png" />
%sapper.styles% %sapper.head%
</head>
<body>
<div id="sapper">%sapper.html%</div>
%sapper.scripts%
</body>
</html>

BIN
docs/static/favicon.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
docs/static/logo-192.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

BIN
docs/static/logo-512.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

91
docs/webpack.config.js Normal file
View file

@ -0,0 +1,91 @@
const webpack = require("webpack");
const path = require("path");
const config = require("@metonym/sapper/config/webpack.js");
const pkg = require("./package.json");
const OptimizeCssAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const AutoprefixerPlugin = require("autoprefixer");
const mode = process.env.NODE_ENV || "production";
const dev = mode === "development";
const alias = {
svelte: path.resolve("node_modules", "svelte"),
sapper: path.resolve("node_modules", "@metonym/sapper"),
};
const extensions = [".mjs", ".js", ".json", ".svelte", ".html"];
const mainFields = ["svelte", "module", "browser", "main"];
module.exports = {
client: {
entry: config.client.entry(),
output: config.client.output(),
resolve: { alias, extensions, mainFields },
module: {
rules: [
{
test: /\.(svelte|html)$/,
use: {
loader: "svelte-loader",
options: {
dev,
hydratable: true,
hotReload: false, // pending https://github.com/sveltejs/svelte/issues/2377
},
},
},
{
test: [/\.s[ac]ss$/i, /\.css$/],
use: [
!dev ? MiniCssExtractPlugin.loader : "style-loader",
"css-loader",
{
loader: "postcss-loader",
options: {
plugins: [
AutoprefixerPlugin({
overrideBrowserslist: ["last 1 version", "ie >= 11"],
}),
],
},
},
"sass-loader",
],
},
],
},
mode,
plugins: [
new MiniCssExtractPlugin({ filename: "[name].[chunkhash].css" }),
new OptimizeCssAssetsPlugin({}),
// pending https://github.com/sveltejs/svelte/issues/2377
// dev && new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin({
"process.browser": true,
"process.env.NODE_ENV": JSON.stringify(mode),
}),
].filter(Boolean),
devtool: dev && "inline-source-map",
},
server: {
entry: config.server.entry(),
output: config.server.output(),
target: "node",
resolve: { alias, extensions, mainFields },
externals: Object.keys(pkg.dependencies).concat("encoding"),
module: {
rules: [
{
test: /\.(svelte|html)$/,
use: {
loader: "svelte-loader",
options: { css: false, generate: "ssr", dev },
},
},
],
},
mode,
performance: { hints: false },
},
};

4716
docs/yarn.lock Normal file

File diff suppressed because it is too large Load diff