Skip to content

Commit f303d82

Browse files
authored
chore: tidy up server exports (#10972)
* tidy up server exports * tidy up server exports * docs are unnecessary here * eliminate client dependencies from server code * lint
1 parent d49e2ae commit f303d82

File tree

27 files changed

+251
-157
lines changed

27 files changed

+251
-157
lines changed

packages/svelte/src/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export const TRANSITION_GLOBAL = 1 << 2;
1919
export const TEMPLATE_FRAGMENT = 1;
2020
export const TEMPLATE_USE_IMPORT_NODE = 1 << 1;
2121

22+
export const UNINITIALIZED = Symbol();
23+
2224
/** List of Element events that will be delegated */
2325
export const DelegatedEvents = [
2426
'beforeinput',

packages/svelte/src/index-server.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
import { current_component_context } from './internal/client/runtime.js';
2-
3-
export {
4-
createEventDispatcher,
5-
flushSync,
6-
getAllContexts,
7-
getContext,
8-
hasContext,
9-
mount,
10-
hydrate,
11-
setContext,
12-
tick,
13-
unmount,
14-
untrack
15-
} from './index-client.js';
16-
17-
/** @returns {void} */
18-
export function onMount() {}
1+
import { current_component } from './internal/server/context.js';
2+
import { noop } from './internal/shared/utils.js';
193

204
/** @param {() => void} fn */
215
export function onDestroy(fn) {
22-
const context = /** @type {import('#client').ComponentContext} */ (current_component_context);
23-
(context.ondestroy ??= []).push(fn);
6+
var context = /** @type {import('#server').Component} */ (current_component);
7+
(context.d ??= []).push(fn);
8+
}
9+
10+
export {
11+
noop as beforeUpdate,
12+
noop as afterUpdate,
13+
noop as onMount,
14+
noop as flushSync,
15+
run as untrack
16+
} from './internal/shared/utils.js';
17+
18+
export function createEventDispatcher() {
19+
return noop;
2420
}
2521

26-
/** @returns {void} */
27-
export function beforeUpdate() {}
22+
export function mount() {
23+
throw new Error('mount(...) is not available on the server');
24+
}
25+
26+
export function hydrate() {
27+
throw new Error('hydrate(...) is not available on the server');
28+
}
2829

29-
/** @returns {void} */
30-
export function afterUpdate() {}
30+
export function unmount() {
31+
throw new Error('unmount(...) is not available on the server');
32+
}
33+
34+
export async function tick() {}
3135

3236
/**
3337
* @template T
@@ -38,3 +42,5 @@ export function unstate(value) {
3842
// There's no signals/proxies on the server, so just return the value
3943
return value;
4044
}
45+
46+
export { getAllContexts, getContext, hasContext, setContext } from './internal/server/context.js';

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ export const DESTROYED = 1 << 12;
1313
export const IS_ELSEIF = 1 << 13;
1414
export const EFFECT_RAN = 1 << 14;
1515

16-
export const UNINITIALIZED = Symbol();
1716
export const STATE_SYMBOL = Symbol('$state');

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/blocks/key.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { UNINITIALIZED } from '../../constants.js';
1+
import { UNINITIALIZED } from '../../../../constants.js';
22
import { block, branch, pause_effect } from '../../reactivity/effects.js';
33
import { safe_not_equal } from '../../reactivity/equality.js';
44

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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { 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,
@@ -9,11 +9,6 @@ import {
99
untrack
1010
} from '../../runtime.js';
1111

12-
/** @param {Function} fn */
13-
function run(fn) {
14-
return fn();
15-
}
16-
1712
/**
1813
* Legacy-mode only: Call `onMount` callbacks and set up `beforeUpdate`/`afterUpdate` effects
1914
*/

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/proxy.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import {
1313
} from './utils.js';
1414
import { add_owner, check_ownership, strip_owner } from './dev/ownership.js';
1515
import { mutable_source, source, set } from './reactivity/sources.js';
16-
import { STATE_SYMBOL, UNINITIALIZED } from './constants.js';
16+
import { STATE_SYMBOL } from './constants.js';
1717
import { updating_derived } from './reactivity/deriveds.js';
18+
import { UNINITIALIZED } from '../../constants.js';
1819

1920
/**
2021
* @template T

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/sources.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
current_effect,
77
current_untracked_writes,
88
current_untracking,
9-
flush_sync,
109
get,
1110
is_batching_effect,
1211
is_runes,
@@ -18,7 +17,8 @@ import {
1817
untrack
1918
} from '../runtime.js';
2019
import { equals, safe_equals } from './equality.js';
21-
import { CLEAN, DERIVED, DIRTY, BRANCH_EFFECT, UNINITIALIZED } from '../constants.js';
20+
import { CLEAN, DERIVED, DIRTY, BRANCH_EFFECT } from '../constants.js';
21+
import { UNINITIALIZED } from '../../../constants.js';
2222

2323
/**
2424
* @template V

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { subscribe_to_store } from '../../../store/utils.js';
2-
import { noop } from '../../common.js';
3-
import { UNINITIALIZED } from '../constants.js';
2+
import { noop } from '../../shared/utils.js';
3+
import { UNINITIALIZED } from '../../../constants.js';
44
import { get, untrack } from '../runtime.js';
55
import { effect } from './effects.js';
66
import { mutable_source, set } from './sources.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/runtime.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,7 @@ export function push(props, runes = false, fn) {
10551055
l1: [],
10561056
l2: source(false),
10571057
// update_callbacks
1058-
u: null,
1059-
ondestroy: null
1058+
u: null
10601059
};
10611060

10621061
if (DEV) {

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

0 commit comments

Comments
 (0)