Repository: sveltejs/kit
Pull request #4932 (2022-05-15 14:00:59)

set default options for dev command
Set default options for the `dev` command, similar to the `preview` command.

This shouldn't break anything, but could be helpful:
1. Ensure port is 3000, as Vite 3 changes to 5173. 
2. Set `--host` to `localhost` as Vite defaults to `127.0.0.1` in dev. 

Re no2, while Vite still forces `127.0.0.1` when `localhost` is specified (ref https://github.com/vitejs/vite/issues/7075), this will be changed in Vite 3 and the behaviour should be consistent with `preview` then.

Also ref: https://github.com/sveltejs/kit/pull/4729

### Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- [ ] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- [x] This message body should clearly illustrate what problems it solves.
- [ ] Ideally, include a test that fails without this PR but passes with it.

### Tests
- [x] Run the tests with `pnpm test` and lint the project with `pnpm lint` and `pnpm check`

### Changesets
- [x] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running `pnpm changeset` and following the prompts. All changesets should be `patch` until SvelteKit 1.0


changeset-bot[bot]: ###  🦋  Changeset detected

Latest commit: d453dfe5d81192e2e3996976db063d313282768c

**The changes in this PR will be included in the next version bump.**

<details><summary>This PR includes changesets to release 1 package</summary>

  | Name          | Type  |
| ------------- | ----- |
| @sveltejs/kit | Patch |

</details>

Not sure what this means? [Click here  to learn what changesets are](https://github.com/changesets/changesets/blob/master/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add another changeset to this PR](https://github.com/sveltejs/kit/new/dev-command-options?filename=.changeset/ninety-bananas-hope.md&value=---%0A%22%40sveltejs%2Fkit%22%3A%20patch%0A---%0A%0Aset%20default%20options%20for%20dev%20command%0A)



Patch:
diff --git a/.changeset/wise-wombats-speak.md b/.changeset/wise-wombats-speak.md
new file mode 100644
index 000000000..4c401c2c2
--- /dev/null
+++ b/.changeset/wise-wombats-speak.md
@@ -0,0 +1,5 @@
+---
+'@sveltejs/kit': patch
+---
+
+only skip files that were already written when prerendering
diff --git a/.changeset/yellow-eagles-leave.md b/.changeset/yellow-eagles-leave.md
new file mode 100644
index 000000000..0dfa6a9bf
--- /dev/null
+++ b/.changeset/yellow-eagles-leave.md
@@ -0,0 +1,5 @@
+---
+'@sveltejs/kit': patch
+---
+
+Set default options for dev command
diff --git a/packages/kit/src/cli.js b/packages/kit/src/cli.js
index 138059520..686a4fab3 100755
--- a/packages/kit/src/cli.js
+++ b/packages/kit/src/cli.js
@@ -47,10 +47,10 @@ const prog = sade('svelte-kit').version('__VERSION__');
 prog
 	.command('dev')
 	.describe('Start a development server')
-	.option('-p, --port', 'Port')
-	.option('-o, --open', 'Open a browser tab')
-	.option('--host', 'Host (only use this on trusted networks)')
-	.option('--https', 'Use self-signed HTTPS certificate')
+	.option('-p, --port', 'Port', 3000)
+	.option('-o, --open', 'Open a browser tab', false)
+	.option('--host', 'Host (only use this on trusted networks)', 'localhost')
+	.option('--https', 'Use self-signed HTTPS certificate', false)
 	.option('-H', 'no longer supported, use --https instead') // TODO remove for 1.0
 	.action(async ({ port, host, https, open, H }) => {
 		try {
diff --git a/packages/kit/src/core/build/prerender/prerender.js b/packages/kit/src/core/build/prerender/prerender.js
index da8f26beb..8b231689d 100644
--- a/packages/kit/src/core/build/prerender/prerender.js
+++ b/packages/kit/src/core/build/prerender/prerender.js
@@ -195,7 +195,6 @@ export async function prerender({ config, entries, files, log }) {
 		const dest = `${config.kit.outDir}/output/prerendered/${category}/${file}`;
 
 		if (written.has(file)) return;
-		written.add(file);
 
 		if (response_type === REDIRECT) {
 			const location = response.headers.get('location');
@@ -216,6 +215,8 @@ export async function prerender({ config, entries, files, log }) {
 						`<meta http-equiv="refresh" content=${escape_html_attr(`0;url=${location}`)}>`
 					);
 
+					written.add(file);
+
 					if (!prerendered.redirects.has(decoded)) {
 						prerendered.redirects.set(decoded, {
 							status: response.status,
@@ -237,6 +238,7 @@ export async function prerender({ config, entries, files, log }) {
 
 			log.info(`${response.status} ${decoded}`);
 			writeFileSync(dest, body);
+			written.add(file);
 
 			if (is_html) {
 				prerendered.pages.set(decoded, {
diff --git a/packages/kit/src/core/dev/index.js b/packages/kit/src/core/dev/index.js
index 14e35c234..5defc0e5c 100644
--- a/packages/kit/src/core/dev/index.js
+++ b/packages/kit/src/core/dev/index.js
@@ -11,7 +11,7 @@ import * as sync from '../sync/sync.js';
  * @typedef {{
  *   cwd: string,
  *   port: number,
- *   host?: string,
+ *   host: string,
  *   https: boolean,
  *   config: import('types').ValidatedConfig
  * }} Options
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index ce337032f..54beb9539 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -424,6 +424,20 @@ importers:
       typescript: 4.6.2
       uvu: 0.5.2
 
+  packages/kit/test/prerendering/paths-base:
+    specifiers:
+      '@sveltejs/kit': workspace:*
+      svelte: ^3.43.0
+      svelte-check: ^2.5.0
+      typescript: ~4.6.2
+      uvu: ^0.5.2
+    devDependencies:
+      '@sveltejs/kit': link:../../..
+      svelte: 3.48.0
+      svelte-check: 2.5.0_svelte@3.48.0
+      typescript: 4.6.2
+      uvu: 0.5.2
+
   packages/kit/test/prerendering/trailing-slash:
     specifiers:
       '@sveltejs/kit': workspace:*
@@ -5526,6 +5540,34 @@ packages:
       - sugarss
     dev: true
 
+  /svelte-check/2.5.0_svelte@3.48.0:
+    resolution: {integrity: sha512-/laO032JQ1TpmYHgaaLndHNPeWW3VSL3liMB97sA9fYTEzlIy3jIo4Rgr7EzjNvieiNWG73C5voOAeNHlrwbpg==}
+    hasBin: true
+    peerDependencies:
+      svelte: ^3.24.0
+    dependencies:
+      chokidar: 3.5.3
+      fast-glob: 3.2.7
+      import-fresh: 3.3.0
+      picocolors: 1.0.0
+      sade: 1.7.4
+      source-map: 0.7.3
+      svelte: 3.48.0
+      svelte-preprocess: 4.9.8_c64miirrcucob5yjzfkddrmila
+      typescript: 4.6.2
+    transitivePeerDependencies:
+      - '@babel/core'
+      - coffeescript
+      - less
+      - node-sass
+      - postcss
+      - postcss-load-config
+      - pug
+      - sass
+      - stylus
+      - sugarss
+    dev: true
+
   /svelte-hmr/0.14.7_svelte@3.44.2:
     resolution: {integrity: sha512-pDrzgcWSoMaK6AJkBWkmgIsecW0GChxYZSZieIYfCP0v2oPyx2CYU/zm7TBIcjLVUPP714WxmViE9Thht4etog==}
     peerDependencies:
@@ -5534,6 +5576,57 @@ packages:
       svelte: 3.44.2
     dev: false
 
+  /svelte-preprocess/4.9.8_c64miirrcucob5yjzfkddrmila:
+    resolution: {integrity: sha512-EQS/oRZzMtYdAprppZxY3HcysKh11w54MgA63ybtL+TAZ4hVqYOnhw41JVJjWN9dhPnNjjLzvbZ2tMhTsla1Og==}
+    engines: {node: '>= 9.11.2'}
+    requiresBuild: true
+    peerDependencies:
+      '@babel/core': ^7.10.2
+      coffeescript: ^2.5.1
+      less: ^3.11.3
+      node-sass: '*'
+      postcss: ^7 || ^8
+      postcss-load-config: ^2.1.0 || ^3.0.0
+      pug: ^3.0.0
+      sass: ^1.26.8
+      stylus: ^0.54.7
+      sugarss: ^2.0.0
+      svelte: ^3.23.0
+      typescript: ^3.9.5 || ^4.0.0
+    peerDependenciesMeta:
+      '@babel/core':
+        optional: true
+      coffeescript:
+        optional: true
+      less:
+        optional: true
+      node-sass:
+        optional: true
+      postcss:
+        optional: true
+      postcss-load-config:
+        optional: true
+      pug:
+        optional: true
+      sass:
+        optional: true
+      stylus:
+        optional: true
+      sugarss:
+        optional: true
+      typescript:
+        optional: true
+    dependencies:
+      '@types/pug': 2.0.5
+      '@types/sass': 1.43.1
+      detect-indent: 6.1.0
+      magic-string: 0.25.7
+      sorcery: 0.10.0
+      strip-indent: 3.0.0
+      svelte: 3.48.0
+      typescript: 4.6.2
+    dev: true
+
   /svelte-preprocess/4.9.8_svelte@3.44.2:
     resolution: {integrity: sha512-EQS/oRZzMtYdAprppZxY3HcysKh11w54MgA63ybtL+TAZ4hVqYOnhw41JVJjWN9dhPnNjjLzvbZ2tMhTsla1Og==}
     engines: {node: '>= 9.11.2'}


Test patch:
diff --git a/packages/kit/test/prerendering/paths-base/package.json b/packages/kit/test/prerendering/paths-base/package.json
new file mode 100644
index 000000000..7f1fac8a5
--- /dev/null
+++ b/packages/kit/test/prerendering/paths-base/package.json
@@ -0,0 +1,20 @@
+{
+	"name": "prerendering-test-paths-base",
+	"private": true,
+	"version": "0.0.1",
+	"scripts": {
+		"dev": "node ../../cli.js dev",
+		"build": "node ../../cli.js build --verbose",
+		"preview": "node ../../cli.js preview",
+		"check": "tsc && svelte-check",
+		"test": "npm run build && uvu test"
+	},
+	"devDependencies": {
+		"@sveltejs/kit": "workspace:*",
+		"svelte": "^3.43.0",
+		"svelte-check": "^2.5.0",
+		"typescript": "~4.6.2",
+		"uvu": "^0.5.2"
+	},
+	"type": "module"
+}
diff --git a/packages/kit/test/prerendering/paths-base/src/app.html b/packages/kit/test/prerendering/paths-base/src/app.html
new file mode 100644
index 000000000..11ff812a1
--- /dev/null
+++ b/packages/kit/test/prerendering/paths-base/src/app.html
@@ -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>
+		%svelte.body%
+	</body>
+</html>
diff --git a/packages/kit/test/prerendering/paths-base/src/hooks.js b/packages/kit/test/prerendering/paths-base/src/hooks.js
new file mode 100644
index 000000000..e69de29bb
diff --git a/packages/kit/test/prerendering/paths-base/src/routes/dynamic/[slug].svelte b/packages/kit/test/prerendering/paths-base/src/routes/dynamic/[slug].svelte
new file mode 100644
index 000000000..91ad718b6
--- /dev/null
+++ b/packages/kit/test/prerendering/paths-base/src/routes/dynamic/[slug].svelte
@@ -0,0 +1,5 @@
+<script>
+	import { page } from '$app/stores';
+</script>
+
+<h1>{$page.params.slug}</h1>
\ No newline at end of file
diff --git a/packages/kit/test/prerendering/paths-base/src/routes/index.svelte b/packages/kit/test/prerendering/paths-base/src/routes/index.svelte
new file mode 100644
index 000000000..ba61d6448
--- /dev/null
+++ b/packages/kit/test/prerendering/paths-base/src/routes/index.svelte
@@ -0,0 +1,10 @@
+<script context="module">
+	import { base } from '$app/paths';
+
+	export function load() {
+		return {
+			status: 301,
+			redirect: `${base}/dynamic/foo`
+		};
+	}
+</script>
\ No newline at end of file
diff --git a/packages/kit/test/prerendering/paths-base/src/routes/nested/index.svelte b/packages/kit/test/prerendering/paths-base/src/routes/nested/index.svelte
new file mode 100644
index 000000000..db7a72875
--- /dev/null
+++ b/packages/kit/test/prerendering/paths-base/src/routes/nested/index.svelte
@@ -0,0 +1,1 @@
+<h1>nested hello</h1>
diff --git a/packages/kit/test/prerendering/paths-base/svelte.config.js b/packages/kit/test/prerendering/paths-base/svelte.config.js
new file mode 100644
index 000000000..416a7be5e
--- /dev/null
+++ b/packages/kit/test/prerendering/paths-base/svelte.config.js
@@ -0,0 +1,31 @@
+import path from 'path';
+import adapter from '../../../../adapter-static/index.js';
+
+/** @type {import('@sveltejs/kit').Config} */
+const config = {
+	kit: {
+		adapter: adapter(),
+
+		paths: {
+			base: '/path-base',
+		},
+
+		prerender: {
+			default: true
+		},
+
+		vite: {
+			build: {
+				minify: false
+			},
+			clearScreen: false,
+			server: {
+				fs: {
+					allow: [path.resolve('../../../src')]
+				}
+			}
+		}
+	}
+};
+
+export default config;
diff --git a/packages/kit/test/prerendering/paths-base/test/test.js b/packages/kit/test/prerendering/paths-base/test/test.js
new file mode 100644
index 000000000..19ad70489
--- /dev/null
+++ b/packages/kit/test/prerendering/paths-base/test/test.js
@@ -0,0 +1,21 @@
+import fs from 'fs';
+import { fileURLToPath } from 'url';
+import { test } from 'uvu';
+import * as assert from 'uvu/assert';
+
+const build = fileURLToPath(new URL('../build', import.meta.url));
+
+/** @param {string} file */
+const read = (file) => fs.readFileSync(`${build}/${file}`, 'utf-8');
+
+test('prerenders /path-base', () => {
+	const content = read('index.html');
+	assert.equal(content, '<meta http-equiv="refresh" content="0;url=/path-base/dynamic/foo">');
+});
+
+test('prerenders /path-base/dynamic/foo', () => {
+	const content = read('dynamic/foo.html');
+	assert.ok(content.includes('<h1>foo</h1>'));
+});
+
+test.run();
diff --git a/packages/kit/test/prerendering/paths-base/tsconfig.json b/packages/kit/test/prerendering/paths-base/tsconfig.json
new file mode 100644
index 000000000..86714a091
--- /dev/null
+++ b/packages/kit/test/prerendering/paths-base/tsconfig.json
@@ -0,0 +1,11 @@
+{
+	"compilerOptions": {
+		"noEmit": true,
+		"paths": {
+			"$lib": ["src/lib"],
+			"$lib/*": ["src/lib/*"],
+			"types": ["../../../types/internal"]
+		}
+	},
+	"extends": "./.svelte-kit/tsconfig.json"
+}