Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement svx blog demo #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/runtime": "^7.0.0",
"@rollup/plugin-babel": "^5.0.2",
"@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-node-resolve": "^8.4.0",
"@rollup/plugin-replace": "^2.2.0",
"gray-matter": "^4.0.2",
"mdsvex": "^0.8.3",
"npm-run-all": "^4.1.5",
"rollup": "^2.23.1",
"@rollup/plugin-babel": "^5.0.2",
"@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-node-resolve": "^8.4.0",
"rollup-plugin-svelte": "^5.0.1",
"rollup-plugin-terser": "^6.1.0",
"sapper": "^0.28.0",
Expand Down
21 changes: 18 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,27 @@ import babel from "@rollup/plugin-babel";
import { terser } from "rollup-plugin-terser";
import config from "sapper/config/rollup.js";
import pkg from "./package.json";
import * as matter from 'gray-matter';
import { mdsvex } from "mdsvex";
import {extname, join} from "path";
import {readdirSync} from "fs";

const mode = process.env.NODE_ENV;
const dev = mode === "development";
const legacy = !!process.env.SAPPER_LEGACY_BUILD;

function get_routes() {
const blog_path = join(process.cwd(), 'src', 'routes', 'blog');
return readdirSync(blog_path).filter(p => extname(p) === ".svx").map(post => {
return matter.read(join(blog_path, post))
});
}

const replaceConstants = {
'process.env.NODE_ENV': JSON.stringify(mode),
'__ROUTES__': JSON.stringify(get_routes()),
};

const onwarn = (warning, onwarn) =>
(warning.code === 'MISSING_EXPORT' && /'preload'/.test(warning.message)) ||
(warning.code === 'CIRCULAR_DEPENDENCY' && /[/\\]@sapper[/\\]/.test(warning.message)) ||
Expand All @@ -24,8 +39,8 @@ export default {
output: config.client.output(),
plugins: [
replace({
...replaceConstants,
"process.browser": true,
"process.env.NODE_ENV": JSON.stringify(mode)
}),
svelte({
extensions,
Expand Down Expand Up @@ -78,8 +93,8 @@ export default {
output: config.server.output(),
plugins: [
replace({
...replaceConstants,
"process.browser": false,
"process.env.NODE_ENV": JSON.stringify(mode)
}),
svelte({
extensions,
Expand All @@ -104,8 +119,8 @@ export default {
plugins: [
resolve(),
replace({
...replaceConstants,
"process.browser": true,
"process.env.NODE_ENV": JSON.stringify(mode)
}),
commonjs(),
!dev && terser()
Expand Down
84 changes: 4 additions & 80 deletions src/routes/blog/_posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,86 +7,10 @@
// 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>
const posts = __ROUTES__;

<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].html</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, '');
});
// posts.forEach(post => {
// post.html = post.html.replace(/^\t{3}/gm, '');
// });

export default posts;
7 changes: 7 additions & 0 deletions src/routes/blog/how-can-i-get-involved.svx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: How can I get involved?
slug: how-can-i-get-involved
---
We're so glad you asked! Come on over to the [Svelte](https://github.com/sveltejs/svelte) and
[Sapper](https://github.com/sveltejs/sapper) repos, and join us in the [Discord chatroom](https://svelte.dev/chat).
Everyone is welcome, especially you!
12 changes: 12 additions & 0 deletions src/routes/blog/how-is-sapper-different-from-next.svx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: How is Sapper different from Next.js?
slug: how-is-sapper-different-from-next
---

[Next.js](https://github.com/zeit/next.js) is a React framework from [Zeit](https://zeit.co), and is the inspiration
for Sapper. There are a few notable differences, however:

* It's powered by [Svelte](https://svelte.dev) instead of React, so it's faster and your apps are smaller
* Instead of route masking, we encode route parameters in filenames. For example, the page you're looking at right now is `src/routes/blog/[slug].html`
* As well as pages (Svelte components, which render on server or client), you can create _server routes_ in your `routes` directory. These are just `.js` files that export functions corresponding to HTTP methods, and receive Express `request` and `response` objects as arguments. This makes it very easy to, for example, add a JSON API such as the one [powering this very page](blog/how-is-sapper-different-from-next.json)
* Links are just `<a>` elements, rather than framework-specific `<Link>` components. That means, for example, that [this link right here](blog/how-can-i-get-involved), despite being inside a blob of HTML, works with the router as you'd expect.
26 changes: 26 additions & 0 deletions src/routes/blog/how-to-use-sapper.svx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: How to use Sapper
slug: how-to-use-sapper
---

## Step one

Create a new project, using [degit](https://github.com/Rich-Harris/degit):

> npx degit "sveltejs/sapper-template#rollup" my-app
> cd my-app
> npm install # or yarn!
> npm run dev


## Step two

Go to [localhost:3000](http://localhost:3000). Open `my-app` in your editor. Edit the files in the `src/routes` directory or add new ones.

## Step three

...

## Step four

Resist overdone joke formats.
6 changes: 3 additions & 3 deletions src/routes/blog/index.json.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import posts from './_posts.js';

const contents = JSON.stringify(posts.map(post => {
return {
title: post.title,
slug: post.slug
title: post.data.title,
slug: post.data.slug
};
}));

Expand All @@ -13,4 +13,4 @@ export function get(req, res) {
});

res.end(contents);
}
}
Loading