Skip to content

Commit

Permalink
adapter-static acccepts outputFileName option
Browse files Browse the repository at this point in the history
  • Loading branch information
dishuostec committed Aug 25, 2021
1 parent 1aef5a4 commit e6d556d
Show file tree
Hide file tree
Showing 17 changed files with 188 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-pears-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-static': patch
---

add outputFileName option
19 changes: 19 additions & 0 deletions packages/adapter-static/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ The directory to write static assets (the contents of `static`, plus client-side

Specify a fallback page for SPA mode, e.g. `index.html` or `200.html` or `404.html`.

### outputFileName

A optional function that is called per entry to return the destination to write to.

Default value:

```javascript
/** @type {(opts: {path: string, is_html: boolean}) => string} */
function default_output_file_name({ path, is_html }) {
if (!is_html) return path;

const parts = path.split('/');
if (parts[parts.length - 1] === 'index.html') return path;

parts.push('index.html');
return parts.join('/');
}
```

## SPA mode

You can use `adapter-static` to create a single-page app or SPA by specifying a **fallback page**.
Expand Down
1 change: 1 addition & 0 deletions packages/adapter-static/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ interface AdapterOptions {
pages?: string;
assets?: string;
fallback?: string;
outputFileName?: (opts: { path: string, is_html: boolean }) => string;
}

declare function plugin(options?: AdapterOptions): Adapter;
Expand Down
16 changes: 13 additions & 3 deletions packages/adapter-static/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
/** @type {import('.')} */
export default function ({ pages = 'build', assets = pages, fallback } = {}) {
/**
* @param {{
* pages?: string;
* assets?: string;
* fallback?: string;
* outputFileName?: (opts: {path: string, is_html: boolean}) => string;
* }} [opts]
*/
export default function({ pages = 'build', assets = pages, fallback, outputFileName } = {}) {
/** @type {import('@sveltejs/kit').Adapter} */
return {
name: '@sveltejs/adapter-static',

/** @type {import('@sveltejs/kit').Adapter['adapt']} */
async adapt({ utils }) {
utils.rimraf(assets);
utils.rimraf(pages);
Expand All @@ -13,7 +22,8 @@ export default function ({ pages = 'build', assets = pages, fallback } = {}) {
await utils.prerender({
fallback,
all: !fallback,
dest: pages
dest: pages,
output_file_name: outputFileName
});
}
};
Expand Down
5 changes: 5 additions & 0 deletions packages/adapter-static/test/apps/spa-entry-name/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
node_modules
/.svelte-kit
/build
/functions
1 change: 1 addition & 0 deletions packages/adapter-static/test/apps/spa-entry-name/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
38 changes: 38 additions & 0 deletions packages/adapter-static/test/apps/spa-entry-name/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# create-svelte

Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte);

## Creating a project

If you're seeing this, you've probably already done this step. Congrats!

```bash
# create a new project in the current directory
npm init svelte@next

# create a new project in my-app
npm init svelte@next my-app
```

> Note: the `@next` is temporary
## Developing

Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:

```bash
npm run dev

# or start the server and open the app in a new browser tab
npm run dev -- --open
```

## Building

Before creating a production version of your app, install an [adapter](https://kit.svelte.dev/docs#adapters) for your target environment. Then:

```bash
npm run build
```

> You can preview the built app with `npm run preview`, regardless of whether you installed an adapter. This should _not_ be used to serve your app in production.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
}
15 changes: 15 additions & 0 deletions packages/adapter-static/test/apps/spa-entry-name/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "~TODO~",
"version": "0.0.1",
"scripts": {
"dev": "../../../../kit/svelte-kit.js dev",
"build": "../../../../kit/svelte-kit.js build",
"start": "../../../../kit/svelte-kit.js start"
},
"devDependencies": {
"@sveltejs/adapter-node": "next",
"@sveltejs/kit": "workspace:*",
"svelte": "^3.40.0"
},
"type": "module"
}
11 changes: 11 additions & 0 deletions packages/adapter-static/test/apps/spa-entry-name/src/app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%svelte.head%
</head>
<body>
<div id="svelte">%svelte.body%</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="@sveltejs/kit" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<nav>
<a href="/">home</a>
<a href="/page">page</a>
<a href="/about.html">about</a>
</nav>

<slot></slot>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script context="module">
export const prerender = true;
</script>

<h1>This page was prerendered</h1>

<h2>about</h2>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>This page was not prerendered</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script context="module">
export const prerender = true;
</script>

<h1>This page was prerendered</h1>

<h2>page</h2>
23 changes: 23 additions & 0 deletions packages/adapter-static/test/apps/spa-entry-name/svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { format, parse } from 'path';
import adapter from '../../../index.js';

/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter({
fallback: '200.html',
outputFileName: ({ path, is_html }) => {
if (!is_html) return path;

let { root, dir, name, ext } = parse(path);

if (!ext) ext = '.html';

return format({ root, dir, name, ext });
}
}),
target: '#svelte'
}
};

export default config;
25 changes: 25 additions & 0 deletions packages/adapter-static/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,28 @@ run('spa', (test) => {
assert.equal(await page.textContent('h1'), '404');
});
});

run('spa-entry-name', (test) => {
test('generates a fallback page', ({ cwd }) => {
assert.ok(fs.existsSync(`${cwd}/build/200.html`));
});

test('does not prerender pages without prerender=true', ({ cwd }) => {
assert.ok(!fs.existsSync(`${cwd}/build/index.html`));
});

test('prerenders page with prerender=true', ({ cwd }) => {
assert.ok(fs.existsSync(`${cwd}/build/about.html`));
assert.ok(fs.existsSync(`${cwd}/build/page.html`));
});

test('renders content in fallback page when JS runs', async ({ base, page }) => {
await page.goto(base);
assert.equal(await page.textContent('h1'), 'This page was not prerendered');
});

test('renders error page for missing page', async ({ base, page }) => {
await page.goto(`${base}/nosuchpage`);
assert.equal(await page.textContent('h1'), '404');
});
});

0 comments on commit e6d556d

Please sign in to comment.