From fd672a4d8af2b6d6b75ce005d6b19161638a9105 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 22 Apr 2021 19:41:42 -0400 Subject: [PATCH 01/17] rename ssr to respond, since ssr is sometimes false --- packages/kit/src/core/build/index.js | 4 ++-- packages/kit/src/core/dev/index.js | 4 ++-- packages/kit/src/runtime/server/index.js | 4 ++-- packages/kit/src/runtime/server/page/load_node.js | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/kit/src/core/build/index.js b/packages/kit/src/core/build/index.js index 831a21ac60d6..832221aff0f5 100644 --- a/packages/kit/src/core/build/index.js +++ b/packages/kit/src/core/build/index.js @@ -271,7 +271,7 @@ async function build_server( fs.writeFileSync( app_file, ` - import { ssr } from '${runtime}'; + import { respond } from '${runtime}'; import root from './generated/root.svelte'; import { set_paths } from './runtime/paths.js'; import { set_prerendering } from './runtime/env.js'; @@ -382,7 +382,7 @@ async function build_server( prerender } = {}) { const host = ${config.kit.host ? s(config.kit.host) : `request.headers[${s(config.kit.hostHeader || 'host')}]`}; - return ssr({ ...request, host }, options, { prerender }); + return respond({ ...request, host }, options, { prerender }); } ` .replace(/^\t{3}/gm, '') diff --git a/packages/kit/src/core/dev/index.js b/packages/kit/src/core/dev/index.js index 60b616f778fd..eef6f72ff344 100644 --- a/packages/kit/src/core/dev/index.js +++ b/packages/kit/src/core/dev/index.js @@ -9,7 +9,7 @@ import colors from 'kleur'; import create_manifest_data from '../../core/create_manifest_data/index.js'; import { create_app } from '../../core/create_app/index.js'; import { rimraf } from '../filesystem/index.js'; -import { ssr } from '../../runtime/server/index.js'; +import { respond } from '../../runtime/server/index.js'; import { getRawBody } from '../http/index.js'; import { copy_assets, get_no_external } from '../utils.js'; import svelte from '@sveltejs/vite-plugin-svelte'; @@ -155,7 +155,7 @@ class Watcher extends EventEmitter { const host = /** @type {string} */ (this.config.kit.host || req.headers[this.config.kit.hostHeader || 'host']); - const rendered = await ssr( + const rendered = await respond( { headers: /** @type {import('types/helper').Headers} */ (req.headers), method: req.method, diff --git a/packages/kit/src/runtime/server/index.js b/packages/kit/src/runtime/server/index.js index 5b95bfadd243..58371644d4e2 100644 --- a/packages/kit/src/runtime/server/index.js +++ b/packages/kit/src/runtime/server/index.js @@ -7,7 +7,7 @@ import { parse_body } from './parse_body/index.js'; * @param {import('types/internal').SSRRenderOptions} options * @param {import('types/internal').SSRRenderState} [state] */ -export async function ssr(incoming, options, state = {}) { +export async function respond(incoming, options, state = {}) { if (incoming.path.endsWith('/') && incoming.path !== '/') { const q = incoming.query.toString(); @@ -79,7 +79,7 @@ export async function ssr(incoming, options, state = {}) { } /** @param {string} str */ -export function hash(str) { +function hash(str) { let hash = 5381, i = str.length; while (i) hash = (hash * 33) ^ str.charCodeAt(--i); diff --git a/packages/kit/src/runtime/server/page/load_node.js b/packages/kit/src/runtime/server/page/load_node.js index 58c9cb33ecad..a8b94646dd06 100644 --- a/packages/kit/src/runtime/server/page/load_node.js +++ b/packages/kit/src/runtime/server/page/load_node.js @@ -1,5 +1,5 @@ import { normalize } from '../../load.js'; -import { ssr } from '../index.js'; +import { respond } from '../index.js'; import { resolve } from './resolve.js'; const s = JSON.stringify; @@ -145,7 +145,7 @@ export async function load_node({ } } - const rendered = await ssr( + const rendered = await respond( { host: request.host, method: opts.method || 'GET', From 2e15946d5dde92e850686023e9537d501682e2ef Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 22 Apr 2021 20:52:20 -0400 Subject: [PATCH 02/17] add tests to adapter-static --- packages/adapter-static/.gitignore | 2 + packages/adapter-static/index.cjs | 18 ++++ packages/adapter-static/index.js | 20 +--- packages/adapter-static/package.json | 16 +++- .../test/apps/prerendered/.gitignore | 5 + .../test/apps/prerendered/.npmrc | 1 + .../test/apps/prerendered/package.json | 15 +++ .../test/apps/prerendered/src/app.html | 11 +++ .../test/apps/prerendered/src/global.d.ts | 3 + .../apps/prerendered/src/routes/index.svelte | 1 + .../test/apps/prerendered/svelte.config.cjs | 7 ++ packages/adapter-static/test/test.js | 15 +++ packages/adapter-static/test/utils.js | 91 +++++++++++++++++++ packages/adapter-static/tsconfig.json | 5 +- 14 files changed, 188 insertions(+), 22 deletions(-) create mode 100644 packages/adapter-static/index.cjs create mode 100644 packages/adapter-static/test/apps/prerendered/.gitignore create mode 100644 packages/adapter-static/test/apps/prerendered/.npmrc create mode 100644 packages/adapter-static/test/apps/prerendered/package.json create mode 100644 packages/adapter-static/test/apps/prerendered/src/app.html create mode 100644 packages/adapter-static/test/apps/prerendered/src/global.d.ts create mode 100644 packages/adapter-static/test/apps/prerendered/src/routes/index.svelte create mode 100644 packages/adapter-static/test/apps/prerendered/svelte.config.cjs create mode 100644 packages/adapter-static/test/test.js create mode 100644 packages/adapter-static/test/utils.js diff --git a/packages/adapter-static/.gitignore b/packages/adapter-static/.gitignore index 9daa8247da45..0e04bb264232 100644 --- a/packages/adapter-static/.gitignore +++ b/packages/adapter-static/.gitignore @@ -1,2 +1,4 @@ .DS_Store node_modules +.svelte +build \ No newline at end of file diff --git a/packages/adapter-static/index.cjs b/packages/adapter-static/index.cjs new file mode 100644 index 000000000000..e296ba924a41 --- /dev/null +++ b/packages/adapter-static/index.cjs @@ -0,0 +1,18 @@ +module.exports = function ({ pages = 'build', assets = 'build' } = {}) { + /** @type {import('@sveltejs/kit').Adapter} */ + const adapter = { + name: '@sveltejs/adapter-static', + + async adapt(utils) { + utils.copy_static_files(assets); + utils.copy_client_files(assets); + + await utils.prerender({ + force: true, + dest: pages + }); + } + }; + + return adapter; +}; diff --git a/packages/adapter-static/index.js b/packages/adapter-static/index.js index e296ba924a41..1167b35ece79 100644 --- a/packages/adapter-static/index.js +++ b/packages/adapter-static/index.js @@ -1,18 +1,2 @@ -module.exports = function ({ pages = 'build', assets = 'build' } = {}) { - /** @type {import('@sveltejs/kit').Adapter} */ - const adapter = { - name: '@sveltejs/adapter-static', - - async adapt(utils) { - utils.copy_static_files(assets); - utils.copy_client_files(assets); - - await utils.prerender({ - force: true, - dest: pages - }); - } - }; - - return adapter; -}; +import commonjs from './index.cjs'; +export default commonjs.default; diff --git a/packages/adapter-static/package.json b/packages/adapter-static/package.json index b4585282b8ba..4107b0e07b2a 100644 --- a/packages/adapter-static/package.json +++ b/packages/adapter-static/package.json @@ -3,11 +3,23 @@ "version": "1.0.0-next.4", "scripts": { "lint": "eslint --ignore-path .gitignore \"**/*.{ts,js,svelte}\" && npm run check-format", + "check": "tsc", "format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore", - "check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore" + "check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore", + "test": "uvu test test.js" }, "devDependencies": { "@sveltejs/kit": "workspace:*", - "typescript": "^4.2.3" + "playwright-chromium": "^1.10.0", + "port-authority": "^1.1.2", + "sirv": "^1.0.11", + "typescript": "^4.2.4" + }, + "type": "module", + "main": "index.cjs", + "module": "index.js", + "exports": { + "import": "./index.js", + "require": "./index.cjs" } } diff --git a/packages/adapter-static/test/apps/prerendered/.gitignore b/packages/adapter-static/test/apps/prerendered/.gitignore new file mode 100644 index 000000000000..e7002107841e --- /dev/null +++ b/packages/adapter-static/test/apps/prerendered/.gitignore @@ -0,0 +1,5 @@ +.DS_Store +node_modules +/.svelte +/build +/functions diff --git a/packages/adapter-static/test/apps/prerendered/.npmrc b/packages/adapter-static/test/apps/prerendered/.npmrc new file mode 100644 index 000000000000..b6f27f135954 --- /dev/null +++ b/packages/adapter-static/test/apps/prerendered/.npmrc @@ -0,0 +1 @@ +engine-strict=true diff --git a/packages/adapter-static/test/apps/prerendered/package.json b/packages/adapter-static/test/apps/prerendered/package.json new file mode 100644 index 000000000000..6fe4ec5da955 --- /dev/null +++ b/packages/adapter-static/test/apps/prerendered/package.json @@ -0,0 +1,15 @@ +{ + "name": "~TODO~", + "version": "0.0.1", + "scripts": { + "dev": "svelte-kit dev", + "build": "svelte-kit build", + "start": "svelte-kit start" + }, + "devDependencies": { + "@sveltejs/kit": "next", + "svelte": "^3.29.0", + "vite": "^2.1.0" + }, + "type": "module" +} diff --git a/packages/adapter-static/test/apps/prerendered/src/app.html b/packages/adapter-static/test/apps/prerendered/src/app.html new file mode 100644 index 000000000000..ab9a75726ecc --- /dev/null +++ b/packages/adapter-static/test/apps/prerendered/src/app.html @@ -0,0 +1,11 @@ + + + + + + %svelte.head% + + +
%svelte.body%
+ + diff --git a/packages/adapter-static/test/apps/prerendered/src/global.d.ts b/packages/adapter-static/test/apps/prerendered/src/global.d.ts new file mode 100644 index 000000000000..79d7d7faa607 --- /dev/null +++ b/packages/adapter-static/test/apps/prerendered/src/global.d.ts @@ -0,0 +1,3 @@ +/// +/// +/// diff --git a/packages/adapter-static/test/apps/prerendered/src/routes/index.svelte b/packages/adapter-static/test/apps/prerendered/src/routes/index.svelte new file mode 100644 index 000000000000..404fd77511d2 --- /dev/null +++ b/packages/adapter-static/test/apps/prerendered/src/routes/index.svelte @@ -0,0 +1 @@ +

This page was prerendered

\ No newline at end of file diff --git a/packages/adapter-static/test/apps/prerendered/svelte.config.cjs b/packages/adapter-static/test/apps/prerendered/svelte.config.cjs new file mode 100644 index 000000000000..40f26e69fd62 --- /dev/null +++ b/packages/adapter-static/test/apps/prerendered/svelte.config.cjs @@ -0,0 +1,7 @@ +/** @type {import('@sveltejs/kit').Config} */ +module.exports = { + kit: { + adapter: require('../../../index.cjs')(), + target: '#svelte' + } +}; diff --git a/packages/adapter-static/test/test.js b/packages/adapter-static/test/test.js new file mode 100644 index 000000000000..aca6874cf552 --- /dev/null +++ b/packages/adapter-static/test/test.js @@ -0,0 +1,15 @@ +import fs from 'fs'; +import * as assert from 'uvu/assert'; +import { run } from './utils.js'; + +run('prerendered', (test) => { + test('generates HTML files', ({ cwd }) => { + assert.ok(fs.existsSync(`${cwd}/build/index.html`)); + }); + + test('prerenders content', async ({ base, page }) => { + await page.goto(base); + + assert.equal(await page.textContent('h1'), 'This page was prerendered'); + }); +}); diff --git a/packages/adapter-static/test/utils.js b/packages/adapter-static/test/utils.js new file mode 100644 index 000000000000..0ccde48d76c2 --- /dev/null +++ b/packages/adapter-static/test/utils.js @@ -0,0 +1,91 @@ +import child_process from 'child_process'; +import http from 'http'; +import { fileURLToPath } from 'url'; +import * as ports from 'port-authority'; +import sirv from 'sirv'; +import { chromium } from 'playwright-chromium'; +import * as uvu from 'uvu'; + +/** + * @typedef {{ + * cwd: string; + * port: number; + * server: import('http').Server; + * base: string; + * browser: import('playwright-chromium').Browser; + * page: import('playwright-chromium').Page; + * }} TestContext + */ + +/** + * @param {string} app + * @param {(test: import('uvu').Test) => void} callback + */ +export function run(app, callback) { + /** @type {import('uvu').Test} */ + const suite = uvu.suite(app); + + suite.before(async (context) => { + try { + const cwd = fileURLToPath(new URL(`apps/${app}`, import.meta.url)); + + await spawn('npm run build', { + cwd, + stdio: 'inherit' + }); + + context.cwd = cwd; + context.port = await ports.find(4000); + const handler = sirv(`${cwd}/build`); + context.server = await create_server(context.port, handler); + + context.base = `http://localhost:${context.port}`; + context.browser = await chromium.launch(); + context.page = await context.browser.newPage(); + } catch (e) { + // TODO remove unnecessary try-catch https://github.com/lukeed/uvu/pull/61 + console.error(e); + } + }); + + suite.after(async (context) => { + context.server.close(); + context.browser.close(); + }); + + callback(suite); + + suite.run(); +} + +/** + * @param {string} str + * @param {child_process.SpawnOptions} opts + */ +function spawn(str, opts) { + return new Promise((fulfil, reject) => { + const [cmd, ...args] = str.split(' '); + + const child = child_process.spawn(cmd, args, opts); + + child.on('error', reject); + + child.on('exit', (code) => { + fulfil(); + }); + }); +} + +/** + * @param {number} port + * @param {(req: http.IncomingMessage, res: http.ServerResponse) => void} handler + * @returns {Promise} + */ +function create_server(port, handler) { + return new Promise((fulfil) => { + const server = http.createServer(handler); + server.listen(port, () => { + fulfil(server); + }); + }); +} diff --git a/packages/adapter-static/tsconfig.json b/packages/adapter-static/tsconfig.json index 3b817fb6b17a..98f56a3c9581 100644 --- a/packages/adapter-static/tsconfig.json +++ b/packages/adapter-static/tsconfig.json @@ -7,7 +7,8 @@ "target": "es2020", "module": "es2020", "moduleResolution": "node", - "allowSyntheticDefaultImports": true + "allowSyntheticDefaultImports": true, + "skipLibCheck": true }, - "include": ["./index.js", "src"] + "include": ["index.cjs", "test"] } From d6f4c3b565a2793067982bd7894b98ea903e4237 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 22 Apr 2021 22:08:00 -0400 Subject: [PATCH 03/17] add failing test for #754 --- .../adapter-static/test/apps/spa/.gitignore | 5 +++ packages/adapter-static/test/apps/spa/.npmrc | 1 + .../adapter-static/test/apps/spa/README.md | 38 +++++++++++++++++++ .../test/apps/spa/jsconfig.json | 9 +++++ .../adapter-static/test/apps/spa/package.json | 16 ++++++++ .../adapter-static/test/apps/spa/src/app.html | 12 ++++++ .../test/apps/spa/src/global.d.ts | 3 ++ .../test/apps/spa/src/routes/$layout.svelte | 6 +++ .../test/apps/spa/src/routes/about.svelte | 5 +++ .../test/apps/spa/src/routes/index.svelte | 1 + .../test/apps/spa/svelte.config.cjs | 9 +++++ packages/adapter-static/test/test.js | 20 +++++++++- packages/adapter-static/test/utils.js | 4 +- 13 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 packages/adapter-static/test/apps/spa/.gitignore create mode 100644 packages/adapter-static/test/apps/spa/.npmrc create mode 100644 packages/adapter-static/test/apps/spa/README.md create mode 100644 packages/adapter-static/test/apps/spa/jsconfig.json create mode 100644 packages/adapter-static/test/apps/spa/package.json create mode 100644 packages/adapter-static/test/apps/spa/src/app.html create mode 100644 packages/adapter-static/test/apps/spa/src/global.d.ts create mode 100644 packages/adapter-static/test/apps/spa/src/routes/$layout.svelte create mode 100644 packages/adapter-static/test/apps/spa/src/routes/about.svelte create mode 100644 packages/adapter-static/test/apps/spa/src/routes/index.svelte create mode 100644 packages/adapter-static/test/apps/spa/svelte.config.cjs diff --git a/packages/adapter-static/test/apps/spa/.gitignore b/packages/adapter-static/test/apps/spa/.gitignore new file mode 100644 index 000000000000..e7002107841e --- /dev/null +++ b/packages/adapter-static/test/apps/spa/.gitignore @@ -0,0 +1,5 @@ +.DS_Store +node_modules +/.svelte +/build +/functions diff --git a/packages/adapter-static/test/apps/spa/.npmrc b/packages/adapter-static/test/apps/spa/.npmrc new file mode 100644 index 000000000000..b6f27f135954 --- /dev/null +++ b/packages/adapter-static/test/apps/spa/.npmrc @@ -0,0 +1 @@ +engine-strict=true diff --git a/packages/adapter-static/test/apps/spa/README.md b/packages/adapter-static/test/apps/spa/README.md new file mode 100644 index 000000000000..eefd796f3175 --- /dev/null +++ b/packages/adapter-static/test/apps/spa/README.md @@ -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 start`, regardless of whether you installed an adapter. This should _not_ be used to serve your app in production. diff --git a/packages/adapter-static/test/apps/spa/jsconfig.json b/packages/adapter-static/test/apps/spa/jsconfig.json new file mode 100644 index 000000000000..893781f65ca0 --- /dev/null +++ b/packages/adapter-static/test/apps/spa/jsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "paths": { + "$lib/*": ["src/lib/*"] + } + }, + "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"] +} diff --git a/packages/adapter-static/test/apps/spa/package.json b/packages/adapter-static/test/apps/spa/package.json new file mode 100644 index 000000000000..057797abf170 --- /dev/null +++ b/packages/adapter-static/test/apps/spa/package.json @@ -0,0 +1,16 @@ +{ + "name": "~TODO~", + "version": "0.0.1", + "scripts": { + "dev": "svelte-kit dev", + "build": "svelte-kit build", + "start": "svelte-kit start" + }, + "devDependencies": { + "@sveltejs/adapter-node": "next", + "@sveltejs/kit": "next", + "svelte": "^3.29.0", + "vite": "^2.1.0" + }, + "type": "module" +} \ No newline at end of file diff --git a/packages/adapter-static/test/apps/spa/src/app.html b/packages/adapter-static/test/apps/spa/src/app.html new file mode 100644 index 000000000000..245305c4ef5b --- /dev/null +++ b/packages/adapter-static/test/apps/spa/src/app.html @@ -0,0 +1,12 @@ + + + + + + + %svelte.head% + + +
%svelte.body%
+ + diff --git a/packages/adapter-static/test/apps/spa/src/global.d.ts b/packages/adapter-static/test/apps/spa/src/global.d.ts new file mode 100644 index 000000000000..79d7d7faa607 --- /dev/null +++ b/packages/adapter-static/test/apps/spa/src/global.d.ts @@ -0,0 +1,3 @@ +/// +/// +/// diff --git a/packages/adapter-static/test/apps/spa/src/routes/$layout.svelte b/packages/adapter-static/test/apps/spa/src/routes/$layout.svelte new file mode 100644 index 000000000000..d89ceb3fed43 --- /dev/null +++ b/packages/adapter-static/test/apps/spa/src/routes/$layout.svelte @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/packages/adapter-static/test/apps/spa/src/routes/about.svelte b/packages/adapter-static/test/apps/spa/src/routes/about.svelte new file mode 100644 index 000000000000..c898cf32ec92 --- /dev/null +++ b/packages/adapter-static/test/apps/spa/src/routes/about.svelte @@ -0,0 +1,5 @@ + + +

This page was prerendered

\ No newline at end of file diff --git a/packages/adapter-static/test/apps/spa/src/routes/index.svelte b/packages/adapter-static/test/apps/spa/src/routes/index.svelte new file mode 100644 index 000000000000..913f0784cbb6 --- /dev/null +++ b/packages/adapter-static/test/apps/spa/src/routes/index.svelte @@ -0,0 +1 @@ +

This page was not prerendered

\ No newline at end of file diff --git a/packages/adapter-static/test/apps/spa/svelte.config.cjs b/packages/adapter-static/test/apps/spa/svelte.config.cjs new file mode 100644 index 000000000000..fdd98ce5120a --- /dev/null +++ b/packages/adapter-static/test/apps/spa/svelte.config.cjs @@ -0,0 +1,9 @@ +/** @type {import('@sveltejs/kit').Config} */ +module.exports = { + kit: { + adapter: require('../../../index.cjs')({ + fallback: '200.html' + }), + target: '#svelte' + } +}; diff --git a/packages/adapter-static/test/test.js b/packages/adapter-static/test/test.js index aca6874cf552..9f6bbbde9dce 100644 --- a/packages/adapter-static/test/test.js +++ b/packages/adapter-static/test/test.js @@ -9,7 +9,25 @@ run('prerendered', (test) => { test('prerenders content', async ({ base, page }) => { await page.goto(base); - assert.equal(await page.textContent('h1'), 'This page was prerendered'); }); }); + +run('spa', (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`)); + }); + + 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'); + }); +}); diff --git a/packages/adapter-static/test/utils.js b/packages/adapter-static/test/utils.js index 0ccde48d76c2..35e8583852a6 100644 --- a/packages/adapter-static/test/utils.js +++ b/packages/adapter-static/test/utils.js @@ -36,7 +36,9 @@ export function run(app, callback) { context.cwd = cwd; context.port = await ports.find(4000); - const handler = sirv(`${cwd}/build`); + const handler = sirv(`${cwd}/build`, { + single: '200.html' + }); context.server = await create_server(context.port, handler); context.base = `http://localhost:${context.port}`; From 66ca7f85af4218eb9ecedb6d5b730c4556e5fd42 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 22 Apr 2021 22:50:56 -0400 Subject: [PATCH 04/17] implement fallback rendering --- packages/kit/src/core/adapt/prerender.js | 35 ++++++++++++++++--- packages/kit/src/core/adapt/test/index.js | 2 +- packages/kit/src/core/adapt/utils.js | 9 ++--- packages/kit/src/runtime/server/index.js | 13 +++++++ .../kit/src/runtime/server/page/render.js | 3 +- .../kit/src/runtime/server/page/respond.js | 3 +- .../runtime/server/page/respond_with_error.js | 1 - packages/kit/types/config.d.ts | 10 +++++- packages/kit/types/internal.d.ts | 8 +++-- 9 files changed, 65 insertions(+), 19 deletions(-) diff --git a/packages/kit/src/core/adapt/prerender.js b/packages/kit/src/core/adapt/prerender.js index cca396a9ed97..e8d60382cb8b 100644 --- a/packages/kit/src/core/adapt/prerender.js +++ b/packages/kit/src/core/adapt/prerender.js @@ -53,9 +53,10 @@ const REDIRECT = 3; * log: import('types/internal').Logger; * config: import('types/config').ValidatedConfig; * build_data: import('types/internal').BuildData; - * force: boolean; // disregard `export const prerender = true` + * fallback: string; + * all: boolean; // disregard `export const prerender = true` * }} opts */ -export async function prerender({ cwd, out, log, config, build_data, force }) { +export async function prerender({ cwd, out, log, config, build_data, fallback, all }) { const dir = resolve_path(cwd, '.svelte/output'); const seen = new Set(); @@ -110,9 +111,9 @@ export async function prerender({ cwd, out, log, config, build_data, force }) { }, { prerender: { - force, - dependencies, - error: null + fallback: null, + all, + dependencies } } ); @@ -221,4 +222,28 @@ export async function prerender({ cwd, out, log, config, build_data, force }) { await visit(entry, null); } } + + if (fallback) { + const rendered = await app.render( + { + host: config.kit.host, + method: 'GET', + headers: {}, + path: '[fallback]', // this doesn't matter, but it's easiest if it's a string + rawBody: null, + query: new URLSearchParams() + }, + { + prerender: { + fallback, + all: false, + dependencies: null + } + } + ); + + const file = join(out, fallback); + mkdirp(dirname(file)); + writeFileSync(file, rendered.body); + } } diff --git a/packages/kit/src/core/adapt/test/index.js b/packages/kit/src/core/adapt/test/index.js index b2a7d8999f94..775381995c3a 100644 --- a/packages/kit/src/core/adapt/test/index.js +++ b/packages/kit/src/core/adapt/test/index.js @@ -92,7 +92,7 @@ suite('prerender', async () => { rimraf.sync(dest); await utils.prerender({ - force: true, + all: true, dest }); diff --git a/packages/kit/src/core/adapt/utils.js b/packages/kit/src/core/adapt/utils.js index 56062e761458..bf84b7c1c30e 100644 --- a/packages/kit/src/core/adapt/utils.js +++ b/packages/kit/src/core/adapt/utils.js @@ -33,16 +33,17 @@ export function get_utils({ cwd, config, build_data, log }) { copy(config.kit.files.assets, dest); }, - /** @param {{ force: boolean, dest: string }} opts */ - async prerender({ force = false, dest }) { + /** @param {{ all: boolean, dest: string, fallback: string }} opts */ + async prerender({ all = false, dest, fallback }) { if (config.kit.prerender.enabled) { await prerender({ out: dest, - force, + all, cwd, config, build_data, - log: this.log + fallback, + log }); } } diff --git a/packages/kit/src/runtime/server/index.js b/packages/kit/src/runtime/server/index.js index 58371644d4e2..16e2868f1f79 100644 --- a/packages/kit/src/runtime/server/index.js +++ b/packages/kit/src/runtime/server/index.js @@ -1,4 +1,5 @@ import render_page from './page/index.js'; +import { render_response } from './page/render.js'; import render_endpoint from './endpoint.js'; import { parse_body } from './parse_body/index.js'; @@ -34,6 +35,18 @@ export async function respond(incoming, options, state = {}) { context }, render: async (request) => { + if (state.prerender && state.prerender.fallback) { + return await render_response({ + options, + $session: await options.hooks.getSession({ context }), + page_config: { ssr: false, router: true, hydrate: true }, + status: 200, + error: null, + branch: [], + page: null + }); + } + for (const route of options.manifest.routes) { if (!route.pattern.test(request.path)) continue; diff --git a/packages/kit/src/runtime/server/page/render.js b/packages/kit/src/runtime/server/page/render.js index ed6f1ed6195c..f4c37cd6ceda 100644 --- a/packages/kit/src/runtime/server/page/render.js +++ b/packages/kit/src/runtime/server/page/render.js @@ -7,7 +7,6 @@ const s = JSON.stringify; /** * @param {{ - * request: import('types/endpoint').ServerRequest; * options: import('types/internal').SSRRenderOptions; * $session: any; * page_config: { hydrate: boolean, router: boolean, ssr: boolean }; @@ -121,7 +120,7 @@ export async function render_response({ session: ${try_serialize($session, (error) => { throw new Error(`Failed to serialize session data: ${error.message}`); })}, - host: ${page.host ? s(page.host) : 'location.host'}, + host: ${page && page.host ? s(page.host) : 'location.host'}, route: ${!!page_config.router}, spa: ${!page_config.ssr}, hydrate: ${page_config.ssr && page_config.hydrate? `{ diff --git a/packages/kit/src/runtime/server/page/respond.js b/packages/kit/src/runtime/server/page/respond.js index 74972064f88e..fd28bdc72d22 100644 --- a/packages/kit/src/runtime/server/page/respond.js +++ b/packages/kit/src/runtime/server/page/respond.js @@ -50,7 +50,7 @@ export async function respond({ request, options, state, $session, route }) { hydrate: 'hydrate' in leaf ? leaf.hydrate : options.hydrate }; - if (!leaf.prerender && state.prerender && !state.prerender.force) { + if (!leaf.prerender && state.prerender && !state.prerender.all) { // if the page has `export const prerender = true`, continue, // otherwise bail out at this point return { @@ -186,7 +186,6 @@ export async function respond({ request, options, state, $session, route }) { try { return await render_response({ - request, options, $session, page_config, diff --git a/packages/kit/src/runtime/server/page/respond_with_error.js b/packages/kit/src/runtime/server/page/respond_with_error.js index 1960f157e40b..beb06728a908 100644 --- a/packages/kit/src/runtime/server/page/respond_with_error.js +++ b/packages/kit/src/runtime/server/page/respond_with_error.js @@ -55,7 +55,6 @@ export async function respond_with_error({ request, options, state, $session, st try { return await render_response({ - request, options, $session, page_config: { diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index 8b23b04ba6e5..b51d7636aa4b 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -9,7 +9,15 @@ export type AdapterUtils = { copy_server_files: (dest: string) => void; copy_static_files: (dest: string) => void; copy: (from: string, to: string, filter?: (basename: string) => boolean) => void; - prerender: ({ force, dest }: { force?: boolean; dest: string }) => Promise; + prerender: ({ + all, + dest, + fallback + }: { + all?: boolean; + dest: string; + fallback?: string; + }) => Promise; }; export type Adapter = { diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts index b33d0c79eea8..878e46fa86d1 100644 --- a/packages/kit/types/internal.d.ts +++ b/packages/kit/types/internal.d.ts @@ -36,9 +36,9 @@ export type App = { incoming: Incoming, options?: { prerender: { - force: boolean; + fallback: string; + all: boolean; dependencies: Map; - error: Error; }; } ) => ServerResponse; @@ -155,10 +155,12 @@ export type SSRRenderState = { fetched?: string; initiator?: SSRPage; prerender?: { - force: boolean; + fallback: string; + all: boolean; dependencies: Map; error: Error; }; + fallback?: string; }; export type Asset = { From 2c0750b3c1ff00f8235a011f31621264e72a1136 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 22 Apr 2021 22:51:13 -0400 Subject: [PATCH 05/17] render fallback page --- packages/adapter-static/index.cjs | 5 +++-- packages/adapter-static/package.json | 2 +- packages/adapter-static/test/apps/spa/src/app.html | 1 - packages/adapter-static/test/test.js | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/adapter-static/index.cjs b/packages/adapter-static/index.cjs index e296ba924a41..5e51c495b5cb 100644 --- a/packages/adapter-static/index.cjs +++ b/packages/adapter-static/index.cjs @@ -1,4 +1,4 @@ -module.exports = function ({ pages = 'build', assets = 'build' } = {}) { +module.exports = function ({ pages = 'build', assets = 'build', fallback = null } = {}) { /** @type {import('@sveltejs/kit').Adapter} */ const adapter = { name: '@sveltejs/adapter-static', @@ -8,7 +8,8 @@ module.exports = function ({ pages = 'build', assets = 'build' } = {}) { utils.copy_client_files(assets); await utils.prerender({ - force: true, + fallback, + all: !fallback, dest: pages }); } diff --git a/packages/adapter-static/package.json b/packages/adapter-static/package.json index 4107b0e07b2a..806a8173436e 100644 --- a/packages/adapter-static/package.json +++ b/packages/adapter-static/package.json @@ -6,7 +6,7 @@ "check": "tsc", "format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore", "check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore", - "test": "uvu test test.js" + "test": "rm -rf test/apps/*/build && uvu test test.js" }, "devDependencies": { "@sveltejs/kit": "workspace:*", diff --git a/packages/adapter-static/test/apps/spa/src/app.html b/packages/adapter-static/test/apps/spa/src/app.html index 245305c4ef5b..ab9a75726ecc 100644 --- a/packages/adapter-static/test/apps/spa/src/app.html +++ b/packages/adapter-static/test/apps/spa/src/app.html @@ -2,7 +2,6 @@ - %svelte.head% diff --git a/packages/adapter-static/test/test.js b/packages/adapter-static/test/test.js index 9f6bbbde9dce..f3137603481e 100644 --- a/packages/adapter-static/test/test.js +++ b/packages/adapter-static/test/test.js @@ -19,11 +19,11 @@ run('spa', (test) => { }); test('does not prerender pages without prerender=true', ({ cwd }) => { - assert.ok(fs.existsSync(`${cwd}/build/index.html`)); + 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/about/index.html`)); }); test('renders content in fallback page when JS runs', async ({ base, page }) => { From c96415ddccbf20756a902cd5259cae737c62fce6 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 22 Apr 2021 22:51:28 -0400 Subject: [PATCH 06/17] update lockfile --- pnpm-lock.yaml | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6d2cd9c85d7a..84ca11c71e16 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -116,10 +116,16 @@ importers: packages/adapter-static: specifiers: '@sveltejs/kit': workspace:* - typescript: ^4.2.3 + playwright-chromium: ^1.10.0 + port-authority: ^1.1.2 + sirv: ^1.0.11 + typescript: ^4.2.4 devDependencies: '@sveltejs/kit': link:../kit - typescript: 4.2.3 + playwright-chromium: 1.10.0 + port-authority: 1.1.2 + sirv: 1.0.11 + typescript: 4.2.4 packages/adapter-vercel: specifiers: @@ -544,10 +550,6 @@ packages: fastq: 1.11.0 dev: true - /@polka/url/1.0.0-next.11: - resolution: {integrity: sha512-3NsZsJIA/22P3QUyrEDNA2D133H4j224twJrdipXN38dpnIOzAbUDtOwkcJ5pXmn75w7LSQDjA4tO9dm1XlqlA==} - dev: true - /@polka/url/1.0.0-next.12: resolution: {integrity: sha512-6RglhutqrGFMO1MNUXp95RBuYIuc8wTnMAV5MUhLmjTOy78ncwOw7RgeQ/HeymkKXRhZd0s2DNrM1rL7unk3MQ==} dev: true @@ -741,11 +743,6 @@ packages: resolution: {integrity: sha512-Lt+wj8NVPx0zUmUwumiVXapmaLUcAk3yPuHCFVXras9k5VT9TdhJqKqGVUQCD60OTMCl0qxJ57OiTL0Mic3Iag==} dev: true - /@types/node/14.14.37: - resolution: {integrity: sha512-XYmBiy+ohOR4Lh5jE379fV2IU+6Jn4g5qASinhitfyO71b/sCo6MKsMLF5tc7Zf2CE8hViVQyYSobJNke8OvUw==} - dev: true - optional: true - /@types/node/14.14.41: resolution: {integrity: sha512-dueRKfaJL4RTtSa7bWeTK1M+VH+Gns73oCgzvYfHZywRCoPSd8EkXBL0mZ9unPTveBn+D9phZBaxuzpwjWkW0g==} dev: true @@ -800,7 +797,7 @@ packages: /@types/yauzl/2.9.1: resolution: {integrity: sha512-A1b8SU4D10uoPjwb0lnHmmu8wZhR9d+9o2PKBQT2jU5YPTKsxac6M2qGAdY7VcL+dHHhARVUDmeg0rOrcd9EjA==} dependencies: - '@types/node': 14.14.37 + '@types/node': 14.14.41 dev: true optional: true @@ -2879,7 +2876,7 @@ packages: proxy-from-env: 1.1.0 rimraf: 3.0.2 stack-utils: 2.0.3 - ws: 7.4.4 + ws: 7.4.5 transitivePeerDependencies: - bufferutil - supports-color @@ -3228,7 +3225,7 @@ packages: resolution: {integrity: sha512-SR36i3/LSWja7AJNRBz4fF/Xjpn7lQFI30tZ434dIy+bitLYSP+ZEenHg36i23V2SGEz+kqjksg0uOGZ5LPiqg==} engines: {node: '>= 10'} dependencies: - '@polka/url': 1.0.0-next.11 + '@polka/url': 1.0.0-next.12 mime: 2.5.2 totalist: 1.1.0 dev: true @@ -3863,8 +3860,8 @@ packages: resolution: {integrity: sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=} dev: true - /ws/7.4.4: - resolution: {integrity: sha512-Qm8k8ojNQIMx7S+Zp8u/uHOx7Qazv3Yv4q68MiWWWOJhiwG5W3x7iqmRtJo8xxrciZUY4vRxUTJCKuRnF28ZZw==} + /ws/7.4.5: + resolution: {integrity: sha512-xzyu3hFvomRfXKH8vOFMU3OguG6oOvhXMo3xsGy3xWExqaM2dxBbVxuD99O7m3ZUFMvvscsZDqxfgMaRr/Nr1g==} engines: {node: '>=8.3.0'} peerDependencies: bufferutil: ^4.0.1 From 60d08f11e958b6dd05ad4865a07408746ac1f568 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 22 Apr 2021 22:53:21 -0400 Subject: [PATCH 07/17] changeset --- .changeset/popular-masks-cheat.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/popular-masks-cheat.md diff --git a/.changeset/popular-masks-cheat.md b/.changeset/popular-masks-cheat.md new file mode 100644 index 000000000000..46b887ecfd37 --- /dev/null +++ b/.changeset/popular-masks-cheat.md @@ -0,0 +1,6 @@ +--- +'@sveltejs/adapter-static': patch +'@sveltejs/kit': patch +--- + +Prerender fallback page for SPAs From 14dc97c48803720dd5aa90a6f8b2f2da40e2a616 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 22 Apr 2021 23:11:20 -0400 Subject: [PATCH 08/17] add readme for adapter-static --- packages/adapter-static/README.md | 63 +++++++++++++++++++++++++++++-- packages/adapter-static/index.cjs | 2 +- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/packages/adapter-static/README.md b/packages/adapter-static/README.md index 63add3e24a87..70be46f36e28 100644 --- a/packages/adapter-static/README.md +++ b/packages/adapter-static/README.md @@ -1,5 +1,62 @@ -# adapter-static +# @sveltejs/adapter-static -Adapter for Svelte apps that prerenders your entire site as a collection of static files, which is equivalent to `sapper export`. +[Adapter](https://kit.svelte.dev/docs#adapters) for SvelteKit apps that prerenders your site as a collection of static files. -This is very experimental. The adapter API is still in flux and will likely change before 1.0. +```js +// svelte.config.cjs +const adapter = require('@sveltejs/adapter-static'); + +module.exports = { + kit: { + adapter: adapter({ + // default options are shown + pages: 'build', + assets: 'build', + fallback: null + }) + } +}; +``` + +Unless you're in [SPA mode](#spa-mode), the adapter will attempt to prerender every page of your app, regardless of whether the [`prerender`](https://kit.svelte.dev/docs#ssr-and-javascript-prerender) option is set. + +## Options + +### pages + +The directory to write prerendered pages to. It defaults to `build`. + +### assets + +The directory to write static assets (the contents of `static`, plus client-side JS and CSS generated by SvelteKit) to. Ordinarily this should be the same as `pages`, and it will default to whatever the value of `pages` is, but in rare circumstances you might need to output pages and assets to separate locations + +### fallback + +Specify a fallback page for SPA mode, e.g. `index.html` or `200.html` or `404.html`. + +## SPA mode + +You can use `adapter-static` to create a single-page app or SPA by specifying a **fallback page**. + +> In most situations this is not recommended: it harms SEO, tends to slow down perceived performance, and makes your app inaccessible to users if JavaScript fails or is disabled (which happens [more often than you probably think](https://kryogenix.org/code/browser/everyonehasjs.html)). + +The fallback page is a blank HTML page that loads your SvelteKit app and navigates to the correct route. For example [Surge](https://surge.sh/help/adding-a-200-page-for-client-side-routing), a static web host, lets you add a `200.html` file that will handle any requests that don't otherwise match. We can create that file like so: + +```js +// svelte.config.cjs +const adapter = require('@sveltejs/adapter-static'); + +module.exports = { + kit: { + adapter: adapter({ + fallback: '200.html' + }) + } +}; +``` + +When operating in SPA mode, only pages that have the [`prerender`](https://kit.svelte.dev/docs#ssr-and-javascript-prerender) option set will be prerendered. + +## License + +[MIT](LICENSE) diff --git a/packages/adapter-static/index.cjs b/packages/adapter-static/index.cjs index 5e51c495b5cb..17ca222dc919 100644 --- a/packages/adapter-static/index.cjs +++ b/packages/adapter-static/index.cjs @@ -1,4 +1,4 @@ -module.exports = function ({ pages = 'build', assets = 'build', fallback = null } = {}) { +module.exports = function ({ pages = 'build', assets = pages, fallback = null } = {}) { /** @type {import('@sveltejs/kit').Adapter} */ const adapter = { name: '@sveltejs/adapter-static', From 15bfb6ceee1cbc17015dbf3b2ba22419867afe22 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 22 Apr 2021 23:13:23 -0400 Subject: [PATCH 09/17] formatting --- .../adapter-static/test/apps/spa/package.json | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/adapter-static/test/apps/spa/package.json b/packages/adapter-static/test/apps/spa/package.json index 057797abf170..b910650e8c48 100644 --- a/packages/adapter-static/test/apps/spa/package.json +++ b/packages/adapter-static/test/apps/spa/package.json @@ -1,16 +1,16 @@ { - "name": "~TODO~", - "version": "0.0.1", - "scripts": { - "dev": "svelte-kit dev", - "build": "svelte-kit build", - "start": "svelte-kit start" - }, - "devDependencies": { - "@sveltejs/adapter-node": "next", - "@sveltejs/kit": "next", - "svelte": "^3.29.0", - "vite": "^2.1.0" - }, - "type": "module" -} \ No newline at end of file + "name": "~TODO~", + "version": "0.0.1", + "scripts": { + "dev": "svelte-kit dev", + "build": "svelte-kit build", + "start": "svelte-kit start" + }, + "devDependencies": { + "@sveltejs/adapter-node": "next", + "@sveltejs/kit": "next", + "svelte": "^3.29.0", + "vite": "^2.1.0" + }, + "type": "module" +} From f2da777ea3d5578e0c4956cb6a3c34d3ecf1fa66 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 22 Apr 2021 23:14:34 -0400 Subject: [PATCH 10/17] gah --- packages/adapter-static/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/adapter-static/tsconfig.json b/packages/adapter-static/tsconfig.json index 98f56a3c9581..5e88bbd94a98 100644 --- a/packages/adapter-static/tsconfig.json +++ b/packages/adapter-static/tsconfig.json @@ -10,5 +10,5 @@ "allowSyntheticDefaultImports": true, "skipLibCheck": true }, - "include": ["index.cjs", "test"] + "include": ["index.cjs", "test/*.js"] } From 8ffc48bff0197fec0a7d6bd39a10460e2e3a57e9 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 22 Apr 2021 23:18:33 -0400 Subject: [PATCH 11/17] missing full stop --- packages/adapter-static/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/adapter-static/README.md b/packages/adapter-static/README.md index 70be46f36e28..b8c64ae0f404 100644 --- a/packages/adapter-static/README.md +++ b/packages/adapter-static/README.md @@ -28,7 +28,7 @@ The directory to write prerendered pages to. It defaults to `build`. ### assets -The directory to write static assets (the contents of `static`, plus client-side JS and CSS generated by SvelteKit) to. Ordinarily this should be the same as `pages`, and it will default to whatever the value of `pages` is, but in rare circumstances you might need to output pages and assets to separate locations +The directory to write static assets (the contents of `static`, plus client-side JS and CSS generated by SvelteKit) to. Ordinarily this should be the same as `pages`, and it will default to whatever the value of `pages` is, but in rare circumstances you might need to output pages and assets to separate locations. ### fallback From f75661de090ad64a37b98bc38a09ef30ca0af156 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Apr 2021 07:09:09 -0400 Subject: [PATCH 12/17] remove ESM export for now, no benefit to it --- packages/adapter-static/index.js | 2 -- packages/adapter-static/package.json | 2 -- 2 files changed, 4 deletions(-) delete mode 100644 packages/adapter-static/index.js diff --git a/packages/adapter-static/index.js b/packages/adapter-static/index.js deleted file mode 100644 index 1167b35ece79..000000000000 --- a/packages/adapter-static/index.js +++ /dev/null @@ -1,2 +0,0 @@ -import commonjs from './index.cjs'; -export default commonjs.default; diff --git a/packages/adapter-static/package.json b/packages/adapter-static/package.json index 806a8173436e..a551dfe29244 100644 --- a/packages/adapter-static/package.json +++ b/packages/adapter-static/package.json @@ -17,9 +17,7 @@ }, "type": "module", "main": "index.cjs", - "module": "index.js", "exports": { - "import": "./index.js", "require": "./index.cjs" } } From 1da21e53e6bea8e5c8e856aebe7bc24296fe7855 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Apr 2021 07:11:44 -0400 Subject: [PATCH 13/17] windows --- packages/adapter-static/package.json | 2 +- packages/adapter-static/test/utils.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/adapter-static/package.json b/packages/adapter-static/package.json index a551dfe29244..ab7cbbd3b04a 100644 --- a/packages/adapter-static/package.json +++ b/packages/adapter-static/package.json @@ -6,7 +6,7 @@ "check": "tsc", "format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore", "check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore", - "test": "rm -rf test/apps/*/build && uvu test test.js" + "test": "uvu test test.js" }, "devDependencies": { "@sveltejs/kit": "workspace:*", diff --git a/packages/adapter-static/test/utils.js b/packages/adapter-static/test/utils.js index 35e8583852a6..fd233e4de28f 100644 --- a/packages/adapter-static/test/utils.js +++ b/packages/adapter-static/test/utils.js @@ -1,4 +1,5 @@ import child_process from 'child_process'; +import fs from 'fs'; import http from 'http'; import { fileURLToPath } from 'url'; import * as ports from 'port-authority'; @@ -29,6 +30,8 @@ export function run(app, callback) { try { const cwd = fileURLToPath(new URL(`apps/${app}`, import.meta.url)); + rimraf(`${cwd}/build`); + await spawn('npm run build', { cwd, stdio: 'inherit' @@ -91,3 +94,8 @@ function create_server(port, handler) { }); }); } + +/** @param {string} path */ +function rimraf(path) { + (fs.rmSync || fs.rmdirSync)(path, { recursive: true, force: true }); +} From 254896908f862b2311aed5b0483ae4d5d5bd36f5 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Apr 2021 07:12:09 -0400 Subject: [PATCH 14/17] lockfile shenanigans --- pnpm-lock.yaml | 65 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 6 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 84ca11c71e16..8139fb8874f7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -190,7 +190,7 @@ importers: '@sveltejs/adapter-cloudflare-workers': link:../../../adapter-cloudflare-workers '@sveltejs/adapter-netlify': link:../../../adapter-netlify '@sveltejs/adapter-vercel': link:../../../adapter-vercel - '@sveltejs/kit': link:../../../kit + '@sveltejs/kit': 1.0.0-next.86_svelte@3.37.0+vite@2.1.5 svelte: 3.37.0 svelte-preprocess: 4.7.1_svelte@3.37.0+typescript@4.2.4 typescript: 4.2.4 @@ -637,6 +637,16 @@ packages: rollup: 2.42.3 dev: true + /@rollup/pluginutils/4.1.0: + resolution: {integrity: sha512-TrBhfJkFxA+ER+ew2U2/fHbebhLT/l/2pRk0hfj9KusXUuRXd2v0R58AfaZK9VXDQ4TogOSEmICVrQAA3zFnHQ==} + engines: {node: '>= 8.0.0'} + peerDependencies: + rollup: ^1.20.0||^2.0.0 + dependencies: + estree-walker: 2.0.2 + picomatch: 2.2.3 + dev: true + /@rollup/pluginutils/4.1.0_rollup@2.41.1: resolution: {integrity: sha512-TrBhfJkFxA+ER+ew2U2/fHbebhLT/l/2pRk0hfj9KusXUuRXd2v0R58AfaZK9VXDQ4TogOSEmICVrQAA3zFnHQ==} engines: {node: '>= 8.0.0'} @@ -648,6 +658,24 @@ packages: rollup: 2.41.1 dev: false + /@sveltejs/kit/1.0.0-next.86_svelte@3.37.0+vite@2.1.5: + resolution: {integrity: sha512-a55qWGoYNIUi4H00asl8kM55D8ZZuu0we9ijKy36Whuz071uqeg3ysLjib/LGpSjcpVZbj3akcUwCog6lU4chQ==} + engines: {node: '>= 12.17.0'} + hasBin: true + peerDependencies: + svelte: ^3.32.1 + vite: ^2.1.0 + dependencies: + '@sveltejs/vite-plugin-svelte': 1.0.0-next.7_svelte@3.37.0+vite@2.1.5 + cheap-watch: 1.0.3 + sade: 1.7.4 + svelte: 3.37.0 + vite: 2.1.5 + transitivePeerDependencies: + - rollup + - supports-color + dev: true + /@sveltejs/vite-plugin-svelte/1.0.0-next.7_93fef3a16c1b45ac67b14a795df10192: resolution: {integrity: sha512-ENvKYY36jrvFP7h1G87k5uOoEh5UM1m8n40J2duqV/R3wHnxfW81SCR1aXo+5CVU8Prm3/jtS4TWs8CUTqO1fw==} engines: {node: '>=12.0.0'} @@ -670,6 +698,28 @@ packages: - supports-color dev: false + /@sveltejs/vite-plugin-svelte/1.0.0-next.7_svelte@3.37.0+vite@2.1.5: + resolution: {integrity: sha512-ENvKYY36jrvFP7h1G87k5uOoEh5UM1m8n40J2duqV/R3wHnxfW81SCR1aXo+5CVU8Prm3/jtS4TWs8CUTqO1fw==} + engines: {node: '>=12.0.0'} + peerDependencies: + svelte: ^3.37.0 + vite: ^2.1.5 + dependencies: + '@rollup/pluginutils': 4.1.0 + chalk: 4.1.0 + debug: 4.3.2 + hash-sum: 2.0.0 + require-relative: 0.8.7 + slash: 3.0.0 + source-map: 0.7.3 + svelte: 3.37.0 + svelte-hmr: 0.14.0_svelte@3.37.0 + vite: 2.1.5 + transitivePeerDependencies: + - rollup + - supports-color + dev: true + /@types/amphtml-validator/1.0.1: resolution: {integrity: sha512-DWE7fy6KtC+Uw0KV/HAmjuH2GB/o8yskXlvmVWR7mOVsLDybp+XrwkzEeRFU9wGjWKeRMBNGsx+5DRq7sUsAwA==} dependencies: @@ -1188,7 +1238,6 @@ packages: /cheap-watch/1.0.3: resolution: {integrity: sha512-xC5CruMhLzjPwJ5ecUxGu1uGmwJQykUhqd2QrCrYbwvsFYdRyviu6jG9+pccwDXJR/OpmOTOJ9yLFunVgQu9wg==} engines: {node: '>=8'} - dev: false /ci-info/2.0.0: resolution: {integrity: sha512-5tK7EtrZ0N+OLFMthtqOj4fI2Jeb88C4CAZPu25LDVUgXJ0A3Js4PMGqrn0JU1W0Mh1/Z8wZzYPxqUrXeBboCQ==} @@ -1377,7 +1426,6 @@ packages: optional: true dependencies: ms: 2.1.2 - dev: false /decamelize-keys/1.1.0: resolution: {integrity: sha1-0XGoeTMlKAfrPLYdwcFEXQeN8tk=} @@ -2070,7 +2118,6 @@ packages: /hash-sum/2.0.0: resolution: {integrity: sha512-WdZTbAByD+pHfl/g9QSsBIIwy8IT+EsPiKDs0KNX+zSHhdDLFKdZu0BQHljvO+0QI/BasbMSUa8wYNCZTvhslg==} - dev: false /hosted-git-info/2.8.8: resolution: {integrity: sha512-f/wzC2QaWBs7t9IYqB4T3sR1xviIViXJRJTWBlx2Gf3g0Xi5vI7Yy4koXQ1c9OYDGHN9sBy1DQ2AB8fqZBWhUg==} @@ -2825,7 +2872,6 @@ packages: /picomatch/2.2.3: resolution: {integrity: sha512-KpELjfwcCDUb9PeigTs2mBJzXUPzAuP2oPcA989He8Rte0+YUAjw1JVedDhuTKPkHjSYzMN3npC9luThGYEKdg==} engines: {node: '>=8.6'} - dev: false /pify/2.3.0: resolution: {integrity: sha1-7RQaasBDqEnqWISY59yosVMw6Qw=} @@ -3085,7 +3131,6 @@ packages: /require-relative/0.8.7: resolution: {integrity: sha1-eZlTn8ngR6N5KPoZb44VY9q9Nt4=} - dev: false /resolve-from/4.0.0: resolution: {integrity: sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==} @@ -3419,6 +3464,14 @@ packages: svelte: 3.35.0 dev: false + /svelte-hmr/0.14.0_svelte@3.37.0: + resolution: {integrity: sha512-Rc4w11U+U30m/cHqOJ/xioFSEAY5fd5muiQC7FL6XJuJAuB2OIJoEZl3KEJR2uO1/f4Bw0PdrugtbxcngSsOtQ==} + peerDependencies: + svelte: '>=3.19.0' + dependencies: + svelte: 3.37.0 + dev: true + /svelte-preprocess/4.7.0_typescript@4.2.4: resolution: {integrity: sha512-iNrY4YGqi0LD2e6oT9YbdSzOKntxk8gmzfqso1z/lUJOZh4o6fyIqkirmiZ8/dDJFqtIE1spVgDFWgkfhLEYlw==} engines: {node: '>= 9.11.2'} From 40b175399e2af2b28058e9e783d027c78714edff Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Apr 2021 07:24:01 -0400 Subject: [PATCH 15/17] argh windows --- packages/adapter-static/test/utils.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/adapter-static/test/utils.js b/packages/adapter-static/test/utils.js index fd233e4de28f..9a61005466f5 100644 --- a/packages/adapter-static/test/utils.js +++ b/packages/adapter-static/test/utils.js @@ -29,10 +29,11 @@ export function run(app, callback) { suite.before(async (context) => { try { const cwd = fileURLToPath(new URL(`apps/${app}`, import.meta.url)); + const cli_path = fileURLToPath(new URL('../../kit/svelte-kit.js', import.meta.url)); rimraf(`${cwd}/build`); - await spawn('npm run build', { + await spawn(`${cli_path} build`, { cwd, stdio: 'inherit' }); From c5f0f4674256c7741f452a7d4b1c5c0322accc04 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Apr 2021 07:28:24 -0400 Subject: [PATCH 16/17] ugh WHAT NOW windows --- packages/adapter-static/test/utils.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/adapter-static/test/utils.js b/packages/adapter-static/test/utils.js index 9a61005466f5..f9bc6162413c 100644 --- a/packages/adapter-static/test/utils.js +++ b/packages/adapter-static/test/utils.js @@ -31,6 +31,8 @@ export function run(app, callback) { const cwd = fileURLToPath(new URL(`apps/${app}`, import.meta.url)); const cli_path = fileURLToPath(new URL('../../kit/svelte-kit.js', import.meta.url)); + console.log(cli_path, fs.existsSync(cli_path)); + rimraf(`${cwd}/build`); await spawn(`${cli_path} build`, { From 275ca73fadcc999c4ddad5b98b3e32213ad9a608 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Apr 2021 07:35:53 -0400 Subject: [PATCH 17/17] try this, you dumb timewaster --- packages/adapter-static/test/utils.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/adapter-static/test/utils.js b/packages/adapter-static/test/utils.js index f9bc6162413c..e96b43418bb2 100644 --- a/packages/adapter-static/test/utils.js +++ b/packages/adapter-static/test/utils.js @@ -29,13 +29,11 @@ export function run(app, callback) { suite.before(async (context) => { try { const cwd = fileURLToPath(new URL(`apps/${app}`, import.meta.url)); - const cli_path = fileURLToPath(new URL('../../kit/svelte-kit.js', import.meta.url)); - - console.log(cli_path, fs.existsSync(cli_path)); + const cli_path = fileURLToPath(new URL('../../kit/src/cli.js', import.meta.url)); rimraf(`${cwd}/build`); - await spawn(`${cli_path} build`, { + await spawn(`${process.execPath} ${cli_path} build`, { cwd, stdio: 'inherit' });