Skip to content

Commit 8cf71f0

Browse files
authored
chore: remove __SVELTEKIT_DEV__ global (#14308)
* remove __SVELTEKIT_DEV__ global * changeset * group third party import statements * separate prod only tests * format * Apply suggestion from @eltigerchino * explicitly import process * adjust test to work in dev
1 parent 4299b76 commit 8cf71f0

File tree

16 files changed

+588
-590
lines changed

16 files changed

+588
-590
lines changed

.changeset/true-moles-tap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
chore: consolidate dev checks to use `esm-env` instead of a `__SVELTEKIT_DEV__` global

packages/kit/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@
8080
"test:cross-platform:build": "pnpm test:unit && pnpm -r --workspace-concurrency 1 --filter=\"./test/**\" test:cross-platform:build",
8181
"test:server-side-route-resolution:dev": "pnpm -r --workspace-concurrency 1 --filter=\"./test/**\" test:server-side-route-resolution:dev",
8282
"test:server-side-route-resolution:build": "pnpm test:unit && pnpm -r --workspace-concurrency 1 --filter=\"./test/**\" test:server-side-route-resolution:build",
83-
"test:unit": "vitest --config kit.vitest.config.js run",
83+
"test:unit:dev": "vitest --config kit.vitest.config.js run",
84+
"test:unit:prod": "NODE_ENV=production vitest --config kit.vitest.config.js run csp.spec.js cookie.spec.js",
85+
"test:unit": "pnpm test:unit:dev && pnpm test:unit:prod",
8486
"prepublishOnly": "pnpm generate:types",
8587
"generate:version": "node scripts/generate-version.js",
8688
"generate:types": "node scripts/generate-dts.js"

packages/kit/src/exports/vite/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ async function kit({ svelte_config }) {
335335
__SVELTEKIT_ADAPTER_NAME__: s(kit.adapter?.name),
336336
__SVELTEKIT_APP_VERSION_FILE__: s(`${kit.appDir}/version.json`),
337337
__SVELTEKIT_APP_VERSION_POLL_INTERVAL__: s(kit.version.pollInterval),
338-
__SVELTEKIT_DEV__: 'false',
339338
__SVELTEKIT_EMBEDDED__: s(kit.embedded),
340339
__SVELTEKIT_EXPERIMENTAL__REMOTE_FUNCTIONS__: s(kit.experimental.remoteFunctions),
341340
__SVELTEKIT_CLIENT_ROUTING__: s(kit.router.resolution === 'client'),
@@ -351,7 +350,6 @@ async function kit({ svelte_config }) {
351350
} else {
352351
new_config.define = {
353352
__SVELTEKIT_APP_VERSION_POLL_INTERVAL__: '0',
354-
__SVELTEKIT_DEV__: 'true',
355353
__SVELTEKIT_EMBEDDED__: s(kit.embedded),
356354
__SVELTEKIT_EXPERIMENTAL__REMOTE_FUNCTIONS__: s(kit.experimental.remoteFunctions),
357355
__SVELTEKIT_CLIENT_ROUTING__: s(kit.router.resolution === 'client'),

packages/kit/src/runtime/app/state/server.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DEV } from 'esm-env';
12
import { getContext } from 'svelte';
23

34
function context() {
@@ -16,31 +17,30 @@ function context_dev(name) {
1617
}
1718
}
1819

19-
// TODO we're using DEV in some places and __SVELTEKIT_DEV__ in others - why? Can we consolidate?
2020
export const page = {
2121
get data() {
22-
return (__SVELTEKIT_DEV__ ? context_dev('page.data') : context()).page.data;
22+
return (DEV ? context_dev('page.data') : context()).page.data;
2323
},
2424
get error() {
25-
return (__SVELTEKIT_DEV__ ? context_dev('page.error') : context()).page.error;
25+
return (DEV ? context_dev('page.error') : context()).page.error;
2626
},
2727
get form() {
28-
return (__SVELTEKIT_DEV__ ? context_dev('page.form') : context()).page.form;
28+
return (DEV ? context_dev('page.form') : context()).page.form;
2929
},
3030
get params() {
31-
return (__SVELTEKIT_DEV__ ? context_dev('page.params') : context()).page.params;
31+
return (DEV ? context_dev('page.params') : context()).page.params;
3232
},
3333
get route() {
34-
return (__SVELTEKIT_DEV__ ? context_dev('page.route') : context()).page.route;
34+
return (DEV ? context_dev('page.route') : context()).page.route;
3535
},
3636
get state() {
37-
return (__SVELTEKIT_DEV__ ? context_dev('page.state') : context()).page.state;
37+
return (DEV ? context_dev('page.state') : context()).page.state;
3838
},
3939
get status() {
40-
return (__SVELTEKIT_DEV__ ? context_dev('page.status') : context()).page.status;
40+
return (DEV ? context_dev('page.status') : context()).page.status;
4141
},
4242
get url() {
43-
return (__SVELTEKIT_DEV__ ? context_dev('page.url') : context()).page.url;
43+
return (DEV ? context_dev('page.url') : context()).page.url;
4444
}
4545
};
4646

packages/kit/src/runtime/app/stores.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getContext } from 'svelte';
2-
import { BROWSER } from 'esm-env';
2+
import { BROWSER, DEV } from 'esm-env';
33
import { stores as browser_stores } from '../client/client.js';
44

55
/**
@@ -35,7 +35,7 @@ export const getStores = () => {
3535
*/
3636
export const page = {
3737
subscribe(fn) {
38-
const store = __SVELTEKIT_DEV__ ? get_store('page') : getStores().page;
38+
const store = DEV ? get_store('page') : getStores().page;
3939
return store.subscribe(fn);
4040
}
4141
};
@@ -52,7 +52,7 @@ export const page = {
5252
*/
5353
export const navigating = {
5454
subscribe(fn) {
55-
const store = __SVELTEKIT_DEV__ ? get_store('navigating') : getStores().navigating;
55+
const store = DEV ? get_store('navigating') : getStores().navigating;
5656
return store.subscribe(fn);
5757
}
5858
};
@@ -67,7 +67,7 @@ export const navigating = {
6767
*/
6868
export const updated = {
6969
subscribe(fn) {
70-
const store = __SVELTEKIT_DEV__ ? get_store('updated') : getStores().updated;
70+
const store = DEV ? get_store('updated') : getStores().updated;
7171

7272
if (BROWSER) {
7373
updated.check = store.check;

packages/kit/src/runtime/server/cookie.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { parse, serialize } from 'cookie';
2+
import { DEV } from 'esm-env';
23
import { normalize_path, resolve } from '../../utils/url.js';
34
import { add_data_suffix } from '../pathname.js';
45
import { text_encoder } from '../utils.js';
@@ -96,7 +97,7 @@ export function get_cookies(request, url) {
9697
// in development, if the cookie was set during this session with `cookies.set`,
9798
// but at a different path, warn the user. (ignore cookies from request headers,
9899
// since we don't know which path they were set at)
99-
if (__SVELTEKIT_DEV__ && !cookie) {
100+
if (DEV && !cookie) {
100101
const paths = Array.from(cookie_paths[name] ?? []).filter((path) => {
101102
// we only care about paths that are _more_ specific than the current path
102103
return path_matches(path, url.pathname) && path !== url.pathname;
@@ -252,7 +253,7 @@ export function get_cookies(request, url) {
252253
const cookie = { name, value, options: { ...options, path } };
253254
new_cookies.set(cookie_key, cookie);
254255

255-
if (__SVELTEKIT_DEV__) {
256+
if (DEV) {
256257
const serialized = serialize(name, value, cookie.options);
257258
if (text_encoder.encode(serialized).byteLength > MAX_COOKIE_SIZE) {
258259
throw new Error(`Cookie "${name}" is too large, and will be discarded by the browser`);

0 commit comments

Comments
 (0)