Skip to content

Commit c1d05a9

Browse files
committed
eliminate client dependencies from server code
1 parent 9ecbe64 commit c1d05a9

File tree

18 files changed

+105
-97
lines changed

18 files changed

+105
-97
lines changed

packages/svelte/src/index-server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { current_component } from './internal/server/context.js';
2-
import { noop } from './internal/common.js';
2+
import { noop } from './internal/shared/utils.js';
33

44
/** @param {() => void} fn */
55
export function onDestroy(fn) {
@@ -13,7 +13,7 @@ export {
1313
noop as onMount,
1414
noop as flushSync,
1515
run as untrack
16-
} from './internal/common.js';
16+
} from './internal/shared/utils.js';
1717

1818
export function createEventDispatcher() {
1919
return noop;

packages/svelte/src/internal/client/dom/blocks/await.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { is_promise } from '../../../common.js';
1+
import { is_promise } from '../../../shared/utils.js';
22
import {
33
current_component_context,
44
flush_sync,

packages/svelte/src/internal/client/dom/elements/transitions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { noop } from '../../../common.js';
1+
import { noop } from '../../../shared/utils.js';
22
import { effect } from '../../reactivity/effects.js';
33
import { current_effect, untrack } from '../../runtime.js';
44
import { raf } from '../../timing.js';

packages/svelte/src/internal/client/dom/legacy/lifecycle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { run, run_all } from '../../../common.js';
1+
import { run, run_all } from '../../../shared/utils.js';
22
import { user_pre_effect, user_effect } from '../../reactivity/effects.js';
33
import {
44
current_component_context,

packages/svelte/src/internal/client/dom/task.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { run_all } from '../../common.js';
1+
import { run_all } from '../../shared/utils.js';
22

33
let is_task_queued = false;
44
let is_raf_queued = false;

packages/svelte/src/internal/client/index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,10 @@ export {
120120
hasContext
121121
} from './runtime.js';
122122
export {
123-
add_snippet_symbol,
124-
validate_component,
125123
validate_dynamic_component,
126-
validate_dynamic_element_tag,
127124
validate_each_keys,
128125
validate_prop_bindings,
129-
validate_snippet,
130-
validate_store,
131-
validate_void_dynamic_element
126+
validate_store
132127
} from './validate.js';
133128
export { raf } from './timing.js';
134129
export { proxy, unstate } from './proxy.js';
@@ -140,4 +135,11 @@ export {
140135
$window as window,
141136
$document as document
142137
} from './dom/operations.js';
143-
export { noop } from '../common.js';
138+
export { noop } from '../shared/utils.js';
139+
export {
140+
add_snippet_symbol,
141+
validate_component,
142+
validate_dynamic_element_tag,
143+
validate_snippet,
144+
validate_void_dynamic_element
145+
} from '../shared/validate.js';

packages/svelte/src/internal/client/reactivity/effects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
ROOT_EFFECT
2828
} from '../constants.js';
2929
import { set } from './sources.js';
30-
import { noop } from '../../common.js';
30+
import { noop } from '../../shared/utils.js';
3131
import { remove } from '../dom/reconciler.js';
3232

3333
/**

packages/svelte/src/internal/client/reactivity/store.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { subscribe_to_store } from '../../../store/utils.js';
2-
import { noop } from '../../common.js';
2+
import { noop } from '../../shared/utils.js';
33
import { UNINITIALIZED } from '../../../constants.js';
44
import { get, untrack } from '../runtime.js';
55
import { effect } from './effects.js';
@@ -10,7 +10,7 @@ import { mutable_source, set } from './sources.js';
1010
* signal that will be updated when the store is. The store references container is needed to
1111
* track reassignments to stores and to track the correct component context.
1212
* @template V
13-
* @param {import('#client').Store<V> | null | undefined} store
13+
* @param {import('#shared').Store<V> | null | undefined} store
1414
* @param {string} store_name
1515
* @param {import('#client').StoreReferencesContainer} stores
1616
* @returns {V}
@@ -46,7 +46,7 @@ export function store_get(store, store_name, stores) {
4646
* Unsubscribe from a store if it's not the same as the one in the store references container.
4747
* We need this in addition to `store_get` because someone could unsubscribe from a store but
4848
* then never subscribe to the new one (if any), causing the subscription to stay open wrongfully.
49-
* @param {import('#client').Store<any> | null | undefined} store
49+
* @param {import('#shared').Store<any> | null | undefined} store
5050
* @param {string} store_name
5151
* @param {import('#client').StoreReferencesContainer} stores
5252
*/
@@ -65,7 +65,7 @@ export function store_unsub(store, store_name, stores) {
6565

6666
/**
6767
* @template V
68-
* @param {import('#client').Store<V> | null | undefined} store
68+
* @param {import('#shared').Store<V> | null | undefined} store
6969
* @param {import('#client').Source<V>} source
7070
*/
7171
function connect_store_to_signal(store, source) {
@@ -80,7 +80,7 @@ function connect_store_to_signal(store, source) {
8080
/**
8181
* Sets the new value of a store and returns that value.
8282
* @template V
83-
* @param {import('#client').Store<V>} store
83+
* @param {import('#shared').Store<V>} store
8484
* @param {V} value
8585
* @returns {V}
8686
*/
@@ -116,7 +116,7 @@ export function unsubscribe_on_destroy(stores) {
116116

117117
/**
118118
* Updates a store with a new value.
119-
* @param {import('#client').Store<V>} store the store to update
119+
* @param {import('#shared').Store<V>} store the store to update
120120
* @param {any} expression the expression that mutates the store
121121
* @param {V} new_value the new store value
122122
* @template V
@@ -127,7 +127,7 @@ export function mutate_store(store, expression, new_value) {
127127
}
128128

129129
/**
130-
* @param {import('#client').Store<number>} store
130+
* @param {import('#shared').Store<number>} store
131131
* @param {number} store_value
132132
* @param {1 | -1} [d]
133133
* @returns {number}
@@ -138,7 +138,7 @@ export function update_store(store, store_value, d = 1) {
138138
}
139139

140140
/**
141-
* @param {import('#client').Store<number>} store
141+
* @param {import('#shared').Store<number>} store
142142
* @param {number} store_value
143143
* @param {1 | -1} [d]
144144
* @returns {number}

packages/svelte/src/internal/client/timing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { noop } from '../common.js';
1+
import { noop } from '../shared/utils.js';
22

33
const is_client = typeof window !== 'undefined';
44

packages/svelte/src/internal/client/types.d.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1+
import type { Store } from '#shared';
12
import { STATE_SYMBOL } from './constants.js';
23
import type { Effect, Source, Value } from './reactivity/types.js';
34

45
type EventCallback = (event: Event) => boolean;
56
export type EventCallbackMap = Record<string, EventCallback | EventCallback[]>;
67

7-
export type Store<V> = {
8-
subscribe: (run: (value: V) => void) => () => void;
9-
set(value: V): void;
10-
};
11-
128
// For all the core internal objects, we use single-character property strings.
139
// This not only reduces code-size and parsing, but it also improves the performance
1410
// when the JS VM JITs the code.
@@ -43,8 +39,6 @@ export type ComponentContext = {
4339
/** onMount callbacks */
4440
m: Array<() => any>;
4541
};
46-
// TODO move this to a separate server component context object
47-
ondestroy: null | Array<() => void>;
4842
};
4943

5044
export type Equals = (this: Value, value: unknown) => boolean;

packages/svelte/src/internal/client/validate.js

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,6 @@ export function validate_dynamic_component(component_fn) {
4242
}
4343
}
4444

45-
/**
46-
* @param {() => string} tag_fn
47-
* @returns {void}
48-
*/
49-
export function validate_void_dynamic_element(tag_fn) {
50-
const tag = tag_fn();
51-
if (tag && is_void(tag)) {
52-
// eslint-disable-next-line no-console
53-
console.warn(`<svelte:element this="${tag}"> is self-closing and cannot have content.`);
54-
}
55-
}
56-
57-
/** @param {() => unknown} tag_fn */
58-
export function validate_dynamic_element_tag(tag_fn) {
59-
const tag = tag_fn();
60-
const is_string = typeof tag === 'string';
61-
if (tag && !is_string) {
62-
throw new Error('<svelte:element> expects "this" attribute to be a string.');
63-
}
64-
}
65-
6645
/**
6746
* @param {() => any} collection
6847
* @param {(item: any, index: number) => string} key_fn
@@ -103,41 +82,6 @@ export function loop_guard(timeout) {
10382
};
10483
}
10584

106-
const snippet_symbol = Symbol.for('svelte.snippet');
107-
108-
/**
109-
* @param {any} fn
110-
*/
111-
export function add_snippet_symbol(fn) {
112-
fn[snippet_symbol] = true;
113-
return fn;
114-
}
115-
116-
/**
117-
* Validate that the function handed to `{@render ...}` is a snippet function, and not some other kind of function.
118-
* @param {any} snippet_fn
119-
*/
120-
export function validate_snippet(snippet_fn) {
121-
if (snippet_fn && snippet_fn[snippet_symbol] !== true) {
122-
throw new Error(
123-
'The argument to `{@render ...}` must be a snippet function, not a component or some other kind of function. ' +
124-
'If you want to dynamically render one snippet or another, use `$derived` and pass its result to `{@render ...}`.'
125-
);
126-
}
127-
return snippet_fn;
128-
}
129-
130-
/**
131-
* Validate that the function behind `<Component />` isn't a snippet.
132-
* @param {any} component_fn
133-
*/
134-
export function validate_component(component_fn) {
135-
if (component_fn?.[snippet_symbol] === true) {
136-
throw new Error('A snippet must be rendered with `{@render ...}`');
137-
}
138-
return component_fn;
139-
}
140-
14185
/**
14286
* @param {Record<string, any>} $$props
14387
* @param {string[]} bindable

packages/svelte/src/internal/server/index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { is_promise, noop } from '../common.js';
1+
import { is_promise, noop } from '../shared/utils.js';
22
import { subscribe_to_store } from '../../store/utils.js';
33
import {
44
DOMBooleanAttributes,
@@ -10,8 +10,6 @@ import { DEV } from 'esm-env';
1010
import { UNINITIALIZED } from '../../constants.js';
1111
import { current_component, pop, push } from './context.js';
1212

13-
export * from '../client/validate.js';
14-
1513
/**
1614
* @typedef {{
1715
* tag: string;
@@ -419,7 +417,7 @@ export function merge_styles(style_attribute, style_directive) {
419417
* @template V
420418
* @param {Record<string, [any, any, any]>} store_values
421419
* @param {string} store_name
422-
* @param {import('../client/types.js').Store<V> | null | undefined} store
420+
* @param {import('#shared').Store<V> | null | undefined} store
423421
* @returns {V}
424422
*/
425423
export function store_get(store_values, store_name, store) {
@@ -456,7 +454,7 @@ export function validate_store(store, name) {
456454
/**
457455
* Sets the new value of a store and returns that value.
458456
* @template V
459-
* @param {import('../client/types.js').Store<V>} store
457+
* @param {import('#shared').Store<V>} store
460458
* @param {V} value
461459
* @returns {V}
462460
*/
@@ -470,7 +468,7 @@ export function store_set(store, value) {
470468
* @template V
471469
* @param {Record<string, [any, any, any]>} store_values
472470
* @param {string} store_name
473-
* @param {import('../client/types.js').Store<V>} store
471+
* @param {import('#shared').Store<V>} store
474472
* @param {any} expression
475473
*/
476474
export function mutate_store(store_values, store_name, store, expression) {
@@ -481,7 +479,7 @@ export function mutate_store(store_values, store_name, store, expression) {
481479
/**
482480
* @param {Record<string, [any, any, any]>} store_values
483481
* @param {string} store_name
484-
* @param {import('../client/types.js').Store<number>} store
482+
* @param {import('#shared').Store<number>} store
485483
* @param {1 | -1} [d]
486484
* @returns {number}
487485
*/
@@ -494,7 +492,7 @@ export function update_store(store_values, store_name, store, d = 1) {
494492
/**
495493
* @param {Record<string, [any, any, any]>} store_values
496494
* @param {string} store_name
497-
* @param {import('../client/types.js').Store<number>} store
495+
* @param {import('#shared').Store<number>} store
498496
* @param {1 | -1} [d]
499497
* @returns {number}
500498
*/
@@ -658,3 +656,11 @@ export function once(get_value) {
658656
}
659657

660658
export { push, pop } from './context.js';
659+
660+
export {
661+
add_snippet_symbol,
662+
validate_component,
663+
validate_dynamic_element_tag,
664+
validate_snippet,
665+
validate_void_dynamic_element
666+
} from '../shared/validate.js';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export type Store<V> = {
2+
subscribe: (run: (value: V) => void) => () => void;
3+
set(value: V): void;
4+
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { is_void } from '../../compiler/phases/1-parse/utils/names.js';
2+
3+
const snippet_symbol = Symbol.for('svelte.snippet');
4+
5+
/**
6+
* @param {any} fn
7+
*/
8+
export function add_snippet_symbol(fn) {
9+
fn[snippet_symbol] = true;
10+
return fn;
11+
}
12+
13+
/**
14+
* Validate that the function handed to `{@render ...}` is a snippet function, and not some other kind of function.
15+
* @param {any} snippet_fn
16+
*/
17+
export function validate_snippet(snippet_fn) {
18+
if (snippet_fn && snippet_fn[snippet_symbol] !== true) {
19+
throw new Error(
20+
'The argument to `{@render ...}` must be a snippet function, not a component or some other kind of function. ' +
21+
'If you want to dynamically render one snippet or another, use `$derived` and pass its result to `{@render ...}`.'
22+
);
23+
}
24+
return snippet_fn;
25+
}
26+
27+
/**
28+
* Validate that the function behind `<Component />` isn't a snippet.
29+
* @param {any} component_fn
30+
*/
31+
export function validate_component(component_fn) {
32+
if (component_fn?.[snippet_symbol] === true) {
33+
throw new Error('A snippet must be rendered with `{@render ...}`');
34+
}
35+
return component_fn;
36+
}
37+
38+
/**
39+
* @param {() => string} tag_fn
40+
* @returns {void}
41+
*/
42+
export function validate_void_dynamic_element(tag_fn) {
43+
const tag = tag_fn();
44+
if (tag && is_void(tag)) {
45+
// eslint-disable-next-line no-console
46+
console.warn(`<svelte:element this="${tag}"> is self-closing and cannot have content.`);
47+
}
48+
}
49+
50+
/** @param {() => unknown} tag_fn */
51+
export function validate_dynamic_element_tag(tag_fn) {
52+
const tag = tag_fn();
53+
const is_string = typeof tag === 'string';
54+
if (tag && !is_string) {
55+
throw new Error('<svelte:element> expects "this" attribute to be a string.');
56+
}
57+
}

packages/svelte/src/store/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { noop, run_all } from '../internal/common.js';
1+
import { noop, run_all } from '../internal/shared/utils.js';
22
import { subscribe_to_store } from './utils.js';
33

44
/**

packages/svelte/src/store/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { noop } from '../internal/common.js';
1+
import { noop } from '../internal/shared/utils.js';
22

33
/**
44
* @template T

0 commit comments

Comments
 (0)