Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] allow SvelteKit to be used without bundling #7950

Merged
merged 15 commits into from
Dec 9, 2022
6 changes: 6 additions & 0 deletions .changeset/slow-files-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sveltejs/environment': patch
'@sveltejs/kit': patch
---

[feat] @sveltejs/environment
19 changes: 19 additions & 0 deletions packages/environment/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @sveltejs/adapter-node
benmccann marked this conversation as resolved.
Show resolved Hide resolved

Package which uses export conditions to return environment information in a way that works with major bundlers and runtimes.

## Usage

Install with `npm install @sveltejs/environment`, then import as needed:

```js
// svelte.config.js
import { DEV, SSR } from '@sveltejs/environment';
```
## Changelog

[The Changelog for this package is available on GitHub](https://github.com/sveltejs/kit/blob/master/packages/environment/CHANGELOG.md).

## License

[MIT](LICENSE)
2 changes: 2 additions & 0 deletions packages/environment/dev-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const SSR = false;
export const DEV = true;
2 changes: 2 additions & 0 deletions packages/environment/dev-ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const SSR = true;
export const DEV = true;
25 changes: 25 additions & 0 deletions packages/environment/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@sveltejs/environment",
"version": "1.0.0-next.1",
"repository": {
"type": "git",
"url": "https://github.com/sveltejs/kit",
"directory": "packages/environment"
},
"license": "MIT",
"homepage": "https://github.com/sveltejs/kit/blob/master/packages/environment/CHANGELOG.md",
"type": "module",
"exports": {
".": {
"browser": {
"development": "./dev-browser.js",
"default": "./prod-browser.js"
},
"default": {
"development": "./dev-ssr.js",
"default": "./prod-ssr.js"
}
},
"./package.json": "./package.json"
benmccann marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 2 additions & 0 deletions packages/environment/prod-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const SSR = false;
export const DEV = false;
2 changes: 2 additions & 0 deletions packages/environment/prod-ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const SSR = true;
export const DEV = false;
1 change: 1 addition & 0 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"homepage": "https://kit.svelte.dev",
"type": "module",
"dependencies": {
"@sveltejs/environment": "workspace:*",
"@sveltejs/vite-plugin-svelte": "^1.3.1",
"@types/cookie": "^0.5.1",
"cookie": "^0.5.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/kit/src/runtime/app/environment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { SSR } from '@sveltejs/environment';

/**
* @type {import('$app/environment').browser}
*/
export const browser = !import.meta.env.SSR;
export const browser = !SSR;

/**
* @type {import('$app/environment').dev}
Expand Down
5 changes: 2 additions & 3 deletions packages/kit/src/runtime/app/forms.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as devalue from 'devalue';
import { client } from '../client/singletons.js';
import { invalidateAll } from './navigation.js';
import { SSR } from '@sveltejs/environment';

/**
* @param {string} name
Expand All @@ -11,10 +12,8 @@ function guard(name) {
};
}

const ssr = import.meta.env.SSR;

/** @type {import('$app/forms').applyAction} */
export const applyAction = ssr ? guard('applyAction') : client.apply_action;
export const applyAction = SSR ? guard('applyAction') : client.apply_action;

/** @type {import('$app/forms').deserialize} */
export function deserialize(result) {
Expand Down
19 changes: 9 additions & 10 deletions packages/kit/src/runtime/app/navigation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SSR } from '@sveltejs/environment';
import { client } from '../client/singletons.js';

/**
Expand All @@ -9,18 +10,16 @@ function guard(name) {
};
}

const ssr = import.meta.env.SSR;

export const disableScrollHandling = ssr
export const disableScrollHandling = SSR
? guard('disableScrollHandling')
: client.disable_scroll_handling;
export const goto = ssr ? guard('goto') : client.goto;
export const invalidate = ssr ? guard('invalidate') : client.invalidate;
export const invalidateAll = ssr ? guard('invalidateAll') : client.invalidateAll;
export const preloadData = ssr ? guard('preloadData') : client.preload_data;
export const preloadCode = ssr ? guard('preloadCode') : client.preload_code;
export const beforeNavigate = ssr ? () => {} : client.before_navigate;
export const afterNavigate = ssr ? () => {} : client.after_navigate;
export const goto = SSR ? guard('goto') : client.goto;
export const invalidate = SSR ? guard('invalidate') : client.invalidate;
export const invalidateAll = SSR ? guard('invalidateAll') : client.invalidateAll;
export const preloadData = SSR ? guard('preloadData') : client.preload_data;
export const preloadCode = SSR ? guard('preloadCode') : client.preload_code;
export const beforeNavigate = SSR ? () => {} : client.before_navigate;
export const afterNavigate = SSR ? () => {} : client.after_navigate;

// TODO remove for 1.0 release
/** @param {any} _args */
Expand Down
5 changes: 3 additions & 2 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DEV } from '@sveltejs/environment';
import { onMount, tick } from 'svelte';
import {
make_trackable,
Expand Down Expand Up @@ -668,7 +669,7 @@ export function create_client({ target, base }) {
}
});

if (import.meta.env.DEV) {
if (DEV) {
try {
lock_fetch();
data = (await node.shared.load.call(null, load_input)) ?? null;
Expand Down Expand Up @@ -1293,7 +1294,7 @@ export function create_client({ target, base }) {
},

disable_scroll_handling: () => {
if (import.meta.env.DEV && started && !updating) {
if (DEV && started && !updating) {
throw new Error('Can only disable scroll handling during navigation');
}

Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/runtime/client/fetcher.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DEV } from '@sveltejs/environment';
import { hash } from '../hash.js';

let loading = 0;
Expand All @@ -12,7 +13,7 @@ export function unlock_fetch() {
loading -= 1;
}

if (import.meta.env.DEV) {
if (DEV) {
let can_inspect_stack_trace = false;

const check_stack_trace = async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/runtime/client/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DEV, SSR } from '@sveltejs/environment';
import { writable } from 'svelte/store';
import { assets } from '../paths.js';
import { version } from '../env.js';
Expand Down Expand Up @@ -209,7 +210,7 @@ export function create_updated_store() {
let timeout;

async function check() {
if (import.meta.env.DEV || import.meta.env.SSR) return false;
if (DEV || SSR) return false;

clearTimeout(timeout);

Expand Down
5 changes: 5 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.