Skip to content

Commit

Permalink
merge master -> gh-2603
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed May 19, 2022
2 parents 448e20e + a026020 commit 22a9eab
Show file tree
Hide file tree
Showing 38 changed files with 291 additions and 109 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-spoons-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Pass config to vite-plugin-svelte instead of reloading it
5 changes: 5 additions & 0 deletions .changeset/moody-moose-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix FOUC regression during dev
5 changes: 5 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
"fuzzy-jobs-retire",
"fuzzy-kids-turn",
"fuzzy-news-raise",
"fuzzy-spoons-report",
"gentle-apes-tease",
"gentle-dogs-heal",
"gentle-rules-camp",
Expand Down Expand Up @@ -462,6 +463,7 @@
"modern-toys-appear",
"moody-dingos-happen",
"moody-laws-draw",
"moody-moose-jump",
"moody-queens-sell",
"moody-rabbits-complain",
"moody-roses-search",
Expand Down Expand Up @@ -621,6 +623,7 @@
"rotten-tips-teach",
"rotten-wasps-beam",
"rude-balloons-return",
"rude-geese-roll",
"rude-masks-kneel",
"rude-olives-wink",
"rude-rockets-rescue",
Expand Down Expand Up @@ -716,6 +719,7 @@
"smart-deers-approve",
"smart-humans-eat",
"smart-mugs-chew",
"smart-nails-work",
"smart-papayas-look",
"smart-pears-accept",
"smart-planets-beam",
Expand All @@ -733,6 +737,7 @@
"soft-bikes-bake",
"soft-ears-share",
"soft-houses-pump",
"soft-months-fail",
"soft-news-teach",
"soft-students-cover",
"sour-hounds-punch",
Expand Down
5 changes: 5 additions & 0 deletions .changeset/smart-nails-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

import generated types from `__types/index.d.ts` file
5 changes: 5 additions & 0 deletions .changeset/soft-months-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[breaking] write generated types to `__types` directories
2 changes: 2 additions & 0 deletions documentation/docs/02-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ export function match(param) {

If the pathname doesn't match, SvelteKit will try to match other routes (using the sort order specified below), before eventually returning a 404.

> Matchers run both on the server and in the browser.
#### Sorting

It's possible for multiple routes to match a given path. For example each of these routes would match `/foo-abc`:
Expand Down
4 changes: 3 additions & 1 deletion documentation/docs/04-loading.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ If the page you're loading has an endpoint, the data returned from it is accessi

### Output

If you return a Promise from `load`, SvelteKit will delay rendering until the promise resolves. The return value has several properties, all optional:
If you return a Promise from `load`, SvelteKit will delay rendering until the promise resolves. The return value has several properties listed below, all of which are optional.

> `status`, `error`, `redirect` and `cache` are ignored when rendering error pages.
#### status

Expand Down
8 changes: 4 additions & 4 deletions documentation/docs/15-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Needless to say, this is cumbersome to write out, and less portable (if you were
To solve this problem, SvelteKit generates `.d.ts` files for each of your endpoints and pages:

```ts
/// file: .svelte-kit/types/src/routes/[foo]/[bar]/[baz].d.ts
/// file: .svelte-kit/types/src/routes/[foo]/[bar]/__types/[baz].d.ts
/// link: false
import type { RequestHandler as GenericRequestHandler, Load as GenericLoad } from '@sveltejs/kit';

Expand All @@ -45,7 +45,7 @@ These files can be imported into your endpoints and pages as siblings, thanks to

```js
/// file: src/routes/[foo]/[bar]/[baz].js
// @filename: [baz].d.ts
// @filename: __types/[baz].d.ts
import type { RequestHandler as GenericRequestHandler, Load as GenericLoad } from '@sveltejs/kit';

export type RequestHandler<Body = any> = GenericRequestHandler<
Expand All @@ -56,15 +56,15 @@ export type RequestHandler<Body = any> = GenericRequestHandler<
// @filename: index.js
// @errors: 2355
// ---cut---
/** @type {import('./[baz]').RequestHandler} */
/** @type {import('./__types/[baz]').RequestHandler} */
export async function get({ params }) {
// ...
}
```

```svelte
<script context="module">
/** @type {import('./[baz]').Load} */
/** @type {import('./__types/[baz]').Load} */
export async function load({ params, fetch, session, stuff }) {
// ...
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"turbo": "^1.2.6",
"typescript": "~4.6.2"
},
"packageManager": "[email protected].0",
"packageManager": "[email protected].2",
"engines": {
"pnpm": "^7.0.0"
},
Expand Down
6 changes: 6 additions & 0 deletions packages/create-svelte/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# create-svelte

## 2.0.0-next.137

### Patch Changes

- import generated types from `__types/index.d.ts` file ([#4705](https://github.com/sveltejs/kit/pull/4705))

## 2.0.0-next.136

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/create-svelte/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-svelte",
"version": "2.0.0-next.136",
"version": "2.0.0-next.137",
"repository": {
"type": "git",
"url": "https://github.com/sveltejs/kit",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { api } from './_api';
import type { RequestHandler } from './index';
import type { RequestHandler } from './__types';

/** @type {import('./index').RequestHandler} */
/** @type {import('./__types').RequestHandler} */
export const get: RequestHandler = async ({ locals }) => {
// locals.userid comes from src/hooks.js
const response = await api('get', `todos/${locals.userid}`);
Expand Down
12 changes: 12 additions & 0 deletions packages/kit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @sveltejs/kit

## 1.0.0-next.336

### Patch Changes

- Pass config to vite-plugin-svelte instead of reloading it ([#4760](https://github.com/sveltejs/kit/pull/4760))

* fix FOUC regression during dev ([#4990](https://github.com/sveltejs/kit/pull/4990))

- move `RequestEvent` and `ResolveOptions` as public types ([#4809](https://github.com/sveltejs/kit/pull/4809))

* [breaking] write generated types to `__types` directories ([#4705](https://github.com/sveltejs/kit/pull/4705))

## 1.0.0-next.335

### Patch Changes
Expand Down
5 changes: 3 additions & 2 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sveltejs/kit",
"version": "1.0.0-next.335",
"version": "1.0.0-next.336",
"repository": {
"type": "git",
"url": "https://github.com/sveltejs/kit",
Expand All @@ -10,7 +10,7 @@
"homepage": "https://kit.svelte.dev",
"type": "module",
"dependencies": {
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.32",
"@sveltejs/vite-plugin-svelte": "^1.0.0-next.44",
"chokidar": "^3.5.3",
"sade": "^1.7.4",
"vite": "^2.9.0"
Expand Down Expand Up @@ -44,6 +44,7 @@
"svelte-preprocess": "^4.9.8",
"svelte2tsx": "~0.5.0",
"tiny-glob": "^0.2.9",
"typescript": "^4.6.4",
"uvu": "^0.5.2"
},
"peerDependencies": {
Expand Down
56 changes: 50 additions & 6 deletions packages/kit/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import colors from 'kleur';
import fs from 'fs';
import { relative } from 'path';
import * as ports from 'port-authority';
import chokidar from 'chokidar';
import { load_config } from './core/config/index.js';
import { networkInterfaces, release } from 'os';
import { coalesce_to_error } from './utils/error.js';
Expand Down Expand Up @@ -53,17 +54,20 @@ prog
.option('--https', 'Use self-signed HTTPS certificate')
.option('-H', 'no longer supported, use --https instead') // TODO remove for 1.0
.action(async ({ port, host, https, open, H }) => {
try {
if (H) throw new Error('-H is no longer supported — use --https instead');
let first = true;
let relaunching = false;
let uid = 1;

process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const config = await load_config();
/** @type {() => Promise<void>} */
let close;

/** @param {import('types').ValidatedConfig} config */
async function start(config) {
const { dev } = await import('./core/dev/index.js');

const cwd = process.cwd();

const { address_info, server_config } = await dev({
const { address_info, server_config, close } = await dev({
cwd,
port,
host,
Expand All @@ -75,12 +79,52 @@ prog
port: address_info.port,
host: address_info.address,
https: !!(https || server_config.https),
open: open || !!server_config.open,
open: first && (open || !!server_config.open),
base: config.kit.paths.base,
loose: server_config.fs.strict === false,
allow: server_config.fs.allow,
cwd
});

first = false;

return close;
}

async function relaunch() {
const id = uid;
relaunching = true;

try {
const updated_config = await load_config();
await close();
close = await start(updated_config);

if (id !== uid) relaunch();
} catch (e) {
const error = /** @type {Error} */ (e);

console.error(colors.bold().red(`> ${error.message}`));
if (error.stack) {
console.error(colors.gray(error.stack.split('\n').slice(1).join('\n')));
}
}

relaunching = false;
}

try {
if (H) throw new Error('-H is no longer supported — use --https instead');

process.env.NODE_ENV = process.env.NODE_ENV || 'development';

const config = await load_config();
close = await start(config);

chokidar.watch('svelte.config.js', { ignoreInitial: true }).on('change', () => {
if (relaunching) uid += 1;
else relaunch();
});
} catch (error) {
handle_error(error);
}
Expand Down
6 changes: 4 additions & 2 deletions packages/kit/src/core/build/build_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ export async function build_client({
},
plugins: [
svelte({
extensions: config.extensions,
...config,
emitCss: true,
compilerOptions: {
...config.compilerOptions,
hydratable: !!config.kit.browser.hydrate
}
},
configFile: false
})
],
// prevent Vite copying the contents of `config.kit.files.assets`,
Expand Down
6 changes: 4 additions & 2 deletions packages/kit/src/core/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,12 @@ export async function build_server(
},
plugins: [
svelte({
extensions: config.extensions,
...config,
compilerOptions: {
...config.compilerOptions,
hydratable: !!config.kit.browser.hydrate
}
},
configFile: false
})
],
resolve: {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function load_config({ cwd = process.cwd() } = {}) {
);
}

const config = await import(url.pathToFileURL(config_file).href);
const config = await import(`${url.pathToFileURL(config_file).href}?ts=${Date.now()}`);

const validated = validate_config(config.default);

Expand Down
6 changes: 4 additions & 2 deletions packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ export async function dev({ cwd, port, host, https, config }) {
},
plugins: [
svelte({
extensions: config.extensions,
...config,
emitCss: true,
compilerOptions: {
...config.compilerOptions,
hydratable: !!config.kit.browser.hydrate
}
},
configFile: false
}),
await create_plugin(config, cwd)
],
Expand Down
18 changes: 11 additions & 7 deletions packages/kit/src/core/dev/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,32 +381,36 @@ function remove_html_middlewares(server) {
* @param {import('vite').ModuleNode} node
* @param {Set<import('vite').ModuleNode>} deps
*/
function find_deps(vite, node, deps) {
async function find_deps(vite, node, deps) {
// since `ssrTransformResult.deps` contains URLs instead of `ModuleNode`s, this process is asynchronous.
// instead of using `await`, we resolve all branches in parallel.
/** @type {Promise<void>[]} */
const branches = [];

/** @param {import('vite').ModuleNode} node */
function add(node) {
async function add(node) {
if (!deps.has(node)) {
deps.add(node);
branches.push(find_deps(vite, node, deps));
await find_deps(vite, node, deps);
}
}

/** @param {string} url */
async function add_by_url(url) {
branches.push(vite.moduleGraph.getModuleByUrl(url).then((node) => node && add(node)));
const node = await vite.moduleGraph.getModuleByUrl(url);

if (node) {
await add(node);
}
}

if (node.ssrTransformResult) {
if (node.ssrTransformResult.deps) {
node.ssrTransformResult.deps.forEach(add_by_url);
node.ssrTransformResult.deps.forEach((url) => branches.push(add_by_url(url)));
}
} else {
node.importedModules.forEach(add);
node.importedModules.forEach((node) => branches.push(add(node)));
}

return Promise.all(branches).then(() => {});
await Promise.all(branches);
}
Loading

0 comments on commit 22a9eab

Please sign in to comment.