Skip to content

Commit

Permalink
remove non-browser code from browser builds (#7296)
Browse files Browse the repository at this point in the history
* remove non-browser code from browser builds

* changeset

* tests

* silence type error

* fix

Co-authored-by: Simon H <[email protected]>
  • Loading branch information
Rich-Harris and dummdidumm authored Oct 18, 2022
1 parent 4843de3 commit e934bc8
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-taxis-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Enable removal of non-browser code from client builds
1 change: 1 addition & 0 deletions packages/kit/src/exports/vite/build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export function get_default_build_config({ 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_BROWSER__: ssr ? 'false' : 'true',
__SVELTEKIT_DEV__: 'false'
},
publicDir: ssr ? false : config.kit.files.assets,
Expand Down
3 changes: 3 additions & 0 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const cwd = process.cwd();
export async function dev(vite, vite_config, svelte_config) {
installPolyfills();

// @ts-expect-error
globalThis.__SVELTEKIT_BROWSER__ = false;

sync.init(svelte_config, vite_config.mode);

/** @type {import('types').Respond} */
Expand Down
5 changes: 3 additions & 2 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,9 @@ function kit() {
}
},
define: {
__SVELTEKIT_DEV__: 'true',
__SVELTEKIT_APP_VERSION_POLL_INTERVAL__: '0'
__SVELTEKIT_APP_VERSION_POLL_INTERVAL__: '0',
__SVELTEKIT_BROWSER__: config_env.ssrBuild ? 'false' : 'true',
__SVELTEKIT_DEV__: 'true'
},
publicDir: svelte_config.kit.files.assets,
resolve: {
Expand Down
10 changes: 6 additions & 4 deletions packages/kit/src/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ export function make_trackable(url, callback) {
});
}

// @ts-ignore
tracked[Symbol.for('nodejs.util.inspect.custom')] = (depth, opts, inspect) => {
return inspect(url, opts);
};
if (!__SVELTEKIT_BROWSER__) {
// @ts-ignore
tracked[Symbol.for('nodejs.util.inspect.custom')] = (depth, opts, inspect) => {
return inspect(url, opts);
};
}

disable_hash(tracked);

Expand Down
3 changes: 3 additions & 0 deletions packages/kit/src/utils/url.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import * as assert from 'uvu/assert';
import { describe } from './unit_test.js';
import { resolve, normalize_path, make_trackable, disable_search } from './url.js';

// @ts-expect-error define global required in url.js
globalThis.__SVELTEKIT_BROWSER__ = false;

describe('resolve', (test) => {
test('resolves a root-relative path', () => {
assert.equal(resolve('/a/b/c', '/x/y/z'), '/x/y/z');
Expand Down
1 change: 1 addition & 0 deletions packages/kit/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,5 +375,6 @@ declare global {
const __SVELTEKIT_APP_VERSION__: string;
const __SVELTEKIT_APP_VERSION_FILE__: string;
const __SVELTEKIT_APP_VERSION_POLL_INTERVAL__: number;
const __SVELTEKIT_BROWSER__: boolean;
const __SVELTEKIT_DEV__: boolean;
}

0 comments on commit e934bc8

Please sign in to comment.