From 21078d66af6a50c7c81998d94de6959ae478887c Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 18 Jul 2022 17:20:05 -0400 Subject: [PATCH 1/9] failing test for #5573 --- packages/kit/src/vite/build/build_server.js | 2 +- packages/kit/src/vite/utils.js | 2 +- packages/kit/test/apps/options/package.json | 2 +- .../test/apps/options/source/pages/mode/index.js | 7 +++++++ .../apps/options/source/pages/mode/index.svelte | 14 ++++++++++++++ packages/kit/test/apps/options/test/test.js | 9 +++++++++ 6 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 packages/kit/test/apps/options/source/pages/mode/index.js create mode 100644 packages/kit/test/apps/options/source/pages/mode/index.svelte diff --git a/packages/kit/src/vite/build/build_server.js b/packages/kit/src/vite/build/build_server.js index 63be2f5b1bc3..b52853d2e697 100644 --- a/packages/kit/src/vite/build/build_server.js +++ b/packages/kit/src/vite/build/build_server.js @@ -111,7 +111,7 @@ export class Server { /** * @param {{ * cwd: string; - * config: import('types').ValidatedConfig + * config: import('types').ValidatedConfig; * vite_config_env: import('vite').ConfigEnv * manifest_data: import('types').ManifestData * build_dir: string; diff --git a/packages/kit/src/vite/utils.js b/packages/kit/src/vite/utils.js index aa00c3856982..1a7eafaf92f0 100644 --- a/packages/kit/src/vite/utils.js +++ b/packages/kit/src/vite/utils.js @@ -12,7 +12,7 @@ export async function get_vite_config(config_env) { if (!config) { throw new Error('Could not load Vite config'); } - return config; + return { ...config, mode: config_env.mode }; } /** diff --git a/packages/kit/test/apps/options/package.json b/packages/kit/test/apps/options/package.json index bb2d3caf9740..ccb66c4d310c 100644 --- a/packages/kit/test/apps/options/package.json +++ b/packages/kit/test/apps/options/package.json @@ -4,7 +4,7 @@ "version": "0.0.1", "scripts": { "dev": "vite dev", - "build": "vite build", + "build": "vite build --mode custom", "preview": "vite preview", "prepare": "node ../../cli.js sync", "check": "tsc && svelte-check", diff --git a/packages/kit/test/apps/options/source/pages/mode/index.js b/packages/kit/test/apps/options/source/pages/mode/index.js new file mode 100644 index 000000000000..8ef254f8e6ff --- /dev/null +++ b/packages/kit/test/apps/options/source/pages/mode/index.js @@ -0,0 +1,7 @@ +export function GET() { + return { + body: { + mode_from_endpoint: import.meta.env.MODE + } + }; +} diff --git a/packages/kit/test/apps/options/source/pages/mode/index.svelte b/packages/kit/test/apps/options/source/pages/mode/index.svelte new file mode 100644 index 000000000000..c492c762cf51 --- /dev/null +++ b/packages/kit/test/apps/options/source/pages/mode/index.svelte @@ -0,0 +1,14 @@ + + +

+ {mode_from_endpoint} === {mode_from_import} === {import.meta.env.MODE} === {dev + ? 'development' + : 'custom'} +

diff --git a/packages/kit/test/apps/options/test/test.js b/packages/kit/test/apps/options/test/test.js index c7af4695c15b..600e7fa796e6 100644 --- a/packages/kit/test/apps/options/test/test.js +++ b/packages/kit/test/apps/options/test/test.js @@ -203,3 +203,12 @@ test.describe('serviceWorker', () => { expect(await page.content()).not.toMatch('navigator.serviceWorker'); }); }); + +test.describe('Vite options', () => { + test('Respects --mode', async ({ page }) => { + await page.goto('/path-base/mode'); + + const mode = process.env.DEV ? 'development' : 'custom'; + expect(await page.textContent('h2')).toBe(`${mode} === ${mode} === ${mode} === ${mode}`); + }); +}); From 8cd0093aa03e81208a6ef4ef8beb20ef48f8d8f1 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 18 Jul 2022 17:20:56 -0400 Subject: [PATCH 2/9] remove some logging --- packages/kit/test/apps/options/source/pages/mode/index.svelte | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/kit/test/apps/options/source/pages/mode/index.svelte b/packages/kit/test/apps/options/source/pages/mode/index.svelte index c492c762cf51..590cb76d092b 100644 --- a/packages/kit/test/apps/options/source/pages/mode/index.svelte +++ b/packages/kit/test/apps/options/source/pages/mode/index.svelte @@ -3,8 +3,6 @@ /** @type {string} */ export let mode_from_endpoint; - - console.error({ dev, mode_from_import, mode_from_endpoint, DEV: import.meta.env.DEV });

From 160e6686fdbb8324ce9aa4144eb7b56dbbd0338c Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 18 Jul 2022 17:40:18 -0400 Subject: [PATCH 3/9] add app.d.ts --- packages/kit/test/apps/options/source/app.d.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 packages/kit/test/apps/options/source/app.d.ts diff --git a/packages/kit/test/apps/options/source/app.d.ts b/packages/kit/test/apps/options/source/app.d.ts new file mode 100644 index 000000000000..d8c081b14d05 --- /dev/null +++ b/packages/kit/test/apps/options/source/app.d.ts @@ -0,0 +1,11 @@ +/// + +declare namespace App { + interface Locals {} + + interface Platform {} + + interface Session {} + + interface Stuff {} +} From 3bac10577da247dab6aa41624b7aae93a37c5d37 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 18 Jul 2022 18:07:27 -0400 Subject: [PATCH 4/9] use define to control dev/prod env values --- packages/kit/src/runtime/app/env.js | 4 ++-- packages/kit/src/vite/build/utils.js | 3 ++- packages/kit/src/vite/index.js | 1 + packages/kit/types/internal.d.ts | 1 + 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/kit/src/runtime/app/env.js b/packages/kit/src/runtime/app/env.js index 72c051286906..e8b594f6850e 100644 --- a/packages/kit/src/runtime/app/env.js +++ b/packages/kit/src/runtime/app/env.js @@ -11,12 +11,12 @@ export const server = !!import.meta.env.SSR; /** * @type {import('$app/env').dev} */ -export const dev = !!import.meta.env.DEV; +export const dev = !__SVELTEKIT_BUILD__; /** * @type {import('$app/env').prod} */ -export const prod = !import.meta.env.DEV; +export const prod = __SVELTEKIT_BUILD__; /** * @type {import('$app/env').mode} diff --git a/packages/kit/src/vite/build/utils.js b/packages/kit/src/vite/build/utils.js index 68e7d26d1249..07e1d8de549a 100644 --- a/packages/kit/src/vite/build/utils.js +++ b/packages/kit/src/vite/build/utils.js @@ -109,7 +109,8 @@ export const get_default_config = function ({ config, input, ssr, outDir }) { __SVELTEKIT_ADAPTER_NAME__: JSON.stringify(config.kit.adapter?.name), __SVELTEKIT_APP_VERSION__: JSON.stringify(config.kit.version.name), __SVELTEKIT_APP_VERSION_FILE__: JSON.stringify(`${config.kit.appDir}/version.json`), - __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: JSON.stringify(config.kit.version.pollInterval) + __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: JSON.stringify(config.kit.version.pollInterval), + __SVELTEKIT_BUILD__: 'true' }, // prevent Vite copying the contents of `config.kit.files.assets`, // if it happens to be 'public' instead of 'static' diff --git a/packages/kit/src/vite/index.js b/packages/kit/src/vite/index.js index cd12cc85601a..55adb12a1a4b 100644 --- a/packages/kit/src/vite/index.js +++ b/packages/kit/src/vite/index.js @@ -206,6 +206,7 @@ function kit() { } }, define: { + __SVELTEKIT_BUILD__: 'false', __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: '0' }, resolve: { diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts index 2c7fc82c66a4..e2d1aef733e8 100644 --- a/packages/kit/types/internal.d.ts +++ b/packages/kit/types/internal.d.ts @@ -323,4 +323,5 @@ declare global { const __SVELTEKIT_APP_VERSION__: string; const __SVELTEKIT_APP_VERSION_FILE__: string; const __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: number; + const __SVELTEKIT_BUILD__: boolean; } From 3bd68ed96dd188ef618eaefba2d2f6f4812cfb49 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 18 Jul 2022 18:09:07 -0400 Subject: [PATCH 5/9] tidy up --- packages/kit/src/vite/build/build_server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/kit/src/vite/build/build_server.js b/packages/kit/src/vite/build/build_server.js index 650b06b5c246..daa12c0e4cd3 100644 --- a/packages/kit/src/vite/build/build_server.js +++ b/packages/kit/src/vite/build/build_server.js @@ -112,8 +112,8 @@ export class Server { * @param {{ * cwd: string; * config: import('types').ValidatedConfig; - * vite_config_env: import('vite').ConfigEnv - * manifest_data: import('types').ManifestData + * vite_config_env: import('vite').ConfigEnv; + * manifest_data: import('types').ManifestData; * build_dir: string; * output_dir: string; * service_worker_entry_file: string | null; From 44786e02b279f8733041eba8e554dc60fc4ed9b3 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 18 Jul 2022 18:11:44 -0400 Subject: [PATCH 6/9] jeez --- .eslintrc.json | 6 ------ packages/kit/src/runtime/app/env.js | 2 ++ 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index a3a8f1176507..2ea366beb18d 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -12,11 +12,5 @@ "error", { "object": "test", "property": "only", "message": "Do not check in test.only tests." } ] - }, - "globals": { - "__SVELTEKIT_ADAPTER_NAME__": true, - "__SVELTEKIT_APP_VERSION__": true, - "__SVELTEKIT_APP_VERSION_FILE__": true, - "__SVELTEKIT_APP_VERSION_POLL_INTERVAL__": true } } diff --git a/packages/kit/src/runtime/app/env.js b/packages/kit/src/runtime/app/env.js index e8b594f6850e..80099f2c20a3 100644 --- a/packages/kit/src/runtime/app/env.js +++ b/packages/kit/src/runtime/app/env.js @@ -1,3 +1,5 @@ +/* global __SVELTEKIT_BUILD__ */ + /** * @type {import('$app/env').browser} */ From 300a02d95ee064a63570330d795d4ead25fd3aa0 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 18 Jul 2022 18:15:18 -0400 Subject: [PATCH 7/9] come on --- packages/kit/test/apps/options/tsconfig.json | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/kit/test/apps/options/tsconfig.json b/packages/kit/test/apps/options/tsconfig.json index f26647b03599..4a9e2bf146ae 100644 --- a/packages/kit/test/apps/options/tsconfig.json +++ b/packages/kit/test/apps/options/tsconfig.json @@ -4,6 +4,7 @@ "checkJs": true, "noEmit": true, "paths": { + "@sveltejs/kit": ["../../../types"], "$lib": ["source/components"], "$lib/*": ["source/components/*"], "types": ["../../../types/internal"] From c2bf6382a529ccc588066b51330151b6cba57b31 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 18 Jul 2022 18:40:19 -0400 Subject: [PATCH 8/9] changeset --- .changeset/modern-lies-deny.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/modern-lies-deny.md diff --git a/.changeset/modern-lies-deny.md b/.changeset/modern-lies-deny.md new file mode 100644 index 000000000000..3a67ca9f4410 --- /dev/null +++ b/.changeset/modern-lies-deny.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Respect custom Vite mode in SSR build From aa2faf3a1dab27ae6648ec2145cb58a604f15432 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 19 Jul 2022 18:00:47 -0400 Subject: [PATCH 9/9] remove redundant env (#5614) * remove redundant env * changeset * add meatier description * huh, cant use those in comments, they get replaced dangerously --- .changeset/thick-chicken-applaud.md | 5 +++++ packages/kit/src/runtime/app/env.js | 19 +---------------- packages/kit/src/vite/build/utils.js | 2 +- packages/kit/src/vite/index.js | 2 +- .../options/source/pages/mode/index.svelte | 6 ++---- packages/kit/test/apps/options/test/test.js | 2 +- packages/kit/types/ambient.d.ts | 21 ++----------------- packages/kit/types/internal.d.ts | 2 +- 8 files changed, 14 insertions(+), 45 deletions(-) create mode 100644 .changeset/thick-chicken-applaud.md diff --git a/.changeset/thick-chicken-applaud.md b/.changeset/thick-chicken-applaud.md new file mode 100644 index 000000000000..9aa565589cec --- /dev/null +++ b/.changeset/thick-chicken-applaud.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +[breaking] remove mode, prod and server from \$app/env diff --git a/packages/kit/src/runtime/app/env.js b/packages/kit/src/runtime/app/env.js index 80099f2c20a3..36ead7671807 100644 --- a/packages/kit/src/runtime/app/env.js +++ b/packages/kit/src/runtime/app/env.js @@ -1,28 +1,11 @@ -/* global __SVELTEKIT_BUILD__ */ - /** * @type {import('$app/env').browser} */ export const browser = !import.meta.env.SSR; -/** - * @type {import('$app/env').server} - */ -export const server = !!import.meta.env.SSR; - /** * @type {import('$app/env').dev} */ -export const dev = !__SVELTEKIT_BUILD__; - -/** - * @type {import('$app/env').prod} - */ -export const prod = __SVELTEKIT_BUILD__; - -/** - * @type {import('$app/env').mode} - */ -export const mode = import.meta.env.MODE; +export const dev = __SVELTEKIT_DEV__; export { prerendering } from '../env.js'; diff --git a/packages/kit/src/vite/build/utils.js b/packages/kit/src/vite/build/utils.js index 07e1d8de549a..363ce2f05440 100644 --- a/packages/kit/src/vite/build/utils.js +++ b/packages/kit/src/vite/build/utils.js @@ -110,7 +110,7 @@ export const get_default_config = function ({ config, input, ssr, outDir }) { __SVELTEKIT_APP_VERSION__: JSON.stringify(config.kit.version.name), __SVELTEKIT_APP_VERSION_FILE__: JSON.stringify(`${config.kit.appDir}/version.json`), __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: JSON.stringify(config.kit.version.pollInterval), - __SVELTEKIT_BUILD__: 'true' + __SVELTEKIT_DEV__: 'false' }, // prevent Vite copying the contents of `config.kit.files.assets`, // if it happens to be 'public' instead of 'static' diff --git a/packages/kit/src/vite/index.js b/packages/kit/src/vite/index.js index 55adb12a1a4b..8891e3c2cd24 100644 --- a/packages/kit/src/vite/index.js +++ b/packages/kit/src/vite/index.js @@ -206,7 +206,7 @@ function kit() { } }, define: { - __SVELTEKIT_BUILD__: 'false', + __SVELTEKIT_DEV__: 'true', __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: '0' }, resolve: { diff --git a/packages/kit/test/apps/options/source/pages/mode/index.svelte b/packages/kit/test/apps/options/source/pages/mode/index.svelte index 590cb76d092b..5b7950bd097b 100644 --- a/packages/kit/test/apps/options/source/pages/mode/index.svelte +++ b/packages/kit/test/apps/options/source/pages/mode/index.svelte @@ -1,12 +1,10 @@

- {mode_from_endpoint} === {mode_from_import} === {import.meta.env.MODE} === {dev - ? 'development' - : 'custom'} + {mode_from_endpoint} === {import.meta.env.MODE} === {dev ? 'development' : 'custom'}

diff --git a/packages/kit/test/apps/options/test/test.js b/packages/kit/test/apps/options/test/test.js index 600e7fa796e6..bf94a72c9215 100644 --- a/packages/kit/test/apps/options/test/test.js +++ b/packages/kit/test/apps/options/test/test.js @@ -209,6 +209,6 @@ test.describe('Vite options', () => { await page.goto('/path-base/mode'); const mode = process.env.DEV ? 'development' : 'custom'; - expect(await page.textContent('h2')).toBe(`${mode} === ${mode} === ${mode} === ${mode}`); + expect(await page.textContent('h2')).toBe(`${mode} === ${mode} === ${mode}`); }); }); diff --git a/packages/kit/types/ambient.d.ts b/packages/kit/types/ambient.d.ts index cbd1b54027d7..0662f091dacd 100644 --- a/packages/kit/types/ambient.d.ts +++ b/packages/kit/types/ambient.d.ts @@ -65,7 +65,7 @@ declare namespace App { /** * ```ts - * import { browser, dev, mode, prerendering, prod, server } from '$app/env'; + * import { browser, dev, prerendering } from '$app/env'; * ``` */ declare module '$app/env' { @@ -75,31 +75,14 @@ declare module '$app/env' { export const browser: boolean; /** - * `true` in development mode, `false` in production. + * Whether the dev server is running. This is not guaranteed to correspond to `NODE_ENV` or `MODE`. */ export const dev: boolean; - /** - * The Vite.js mode the app is running in. Configure in [`vite.config.js`](https://vitejs.dev/config/shared-options.html#mode). - * Vite.js loads the dotenv file associated with the provided mode, `.env.[mode]` or `.env.[mode].local`. - * By default, `vite dev` runs with `mode=development` and `vite build` runs with `mode=production`. - */ - export const mode: string; - /** * `true` when prerendering, `false` otherwise. */ export const prerendering: boolean; - - /** - * `true` in production mode, `false` in development. - */ - export const prod: boolean; - - /** - * `true` if the app is running on the server. - */ - export const server: boolean; } /** diff --git a/packages/kit/types/internal.d.ts b/packages/kit/types/internal.d.ts index e2d1aef733e8..f7a88de58f37 100644 --- a/packages/kit/types/internal.d.ts +++ b/packages/kit/types/internal.d.ts @@ -323,5 +323,5 @@ declare global { const __SVELTEKIT_APP_VERSION__: string; const __SVELTEKIT_APP_VERSION_FILE__: string; const __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: number; - const __SVELTEKIT_BUILD__: boolean; + const __SVELTEKIT_DEV__: boolean; }