@@ -5,7 +5,7 @@ import { URL } from 'node:url';
5
5
import { AsyncLocalStorage } from 'node:async_hooks' ;
6
6
import colors from 'kleur' ;
7
7
import sirv from 'sirv' ;
8
- import { isCSSRequest , loadEnv , buildErrorMessage } from 'vite' ;
8
+ import { isCSSRequest , loadEnv , buildErrorMessage , isRunnableDevEnvironment } from 'vite' ;
9
9
import { createReadableStream , getRequest , setResponse } from '../../../exports/node/index.js' ;
10
10
import { installPolyfills } from '../../../exports/node/polyfills.js' ;
11
11
import { coalesce_to_error } from '../../../utils/error.js' ;
@@ -33,6 +33,12 @@ const vite_css_query_regex = /(?:\?|&)(?:raw|url|inline)(?:&|$)/;
33
33
export async function dev ( vite , vite_config , svelte_config ) {
34
34
installPolyfills ( ) ;
35
35
36
+ const viteEnv = vite . environments . ssr ;
37
+
38
+ if ( ! isRunnableDevEnvironment ( viteEnv ) ) {
39
+ throw new Error ( 'Cannot use sveltekit in a non-runnable vite environment' ) ;
40
+ }
41
+
36
42
const async_local_storage = new AsyncLocalStorage ( ) ;
37
43
38
44
globalThis . __SVELTEKIT_TRACK__ = ( label ) => {
@@ -64,9 +70,13 @@ export async function dev(vite, vite_config, svelte_config) {
64
70
let manifest_error = null ;
65
71
66
72
/** @param {string } url */
67
- async function loud_ssr_load_module ( url ) {
73
+ async function load_ssr_load_module ( url ) {
74
+ if ( ! isRunnableDevEnvironment ( viteEnv ) ) {
75
+ throw new Error ( 'Cannot load SSR module in non-runnable vite environment' ) ;
76
+ }
77
+
68
78
try {
69
- return await vite . ssrLoadModule ( url , { fixStacktrace : true } ) ;
79
+ return await viteEnv . runner . import ( url ) ;
70
80
} catch ( /** @type {any } */ err ) {
71
81
const msg = buildErrorMessage ( err , [ colors . red ( `Internal server error: ${ err . message } ` ) ] ) ;
72
82
@@ -93,9 +103,9 @@ export async function dev(vite, vite_config, svelte_config) {
93
103
async function resolve ( id ) {
94
104
const url = id . startsWith ( '..' ) ? to_fs ( path . posix . resolve ( id ) ) : `/${ id } ` ;
95
105
96
- const module = await loud_ssr_load_module ( url ) ;
106
+ const module = await load_ssr_load_module ( url ) ;
97
107
98
- const module_node = await vite . moduleGraph . getModuleByUrl ( url ) ;
108
+ const module_node = await viteEnv . moduleGraph . getModuleByUrl ( url ) ;
99
109
if ( ! module_node ) throw new Error ( `Could not find node for ${ url } ` ) ;
100
110
101
111
return { module, module_node, url } ;
@@ -150,7 +160,7 @@ export async function dev(vite, vite_config, svelte_config) {
150
160
/** @type {import('types').SSRNode } */
151
161
const result = { } ;
152
162
153
- /** @type {import('vite').ModuleNode [] } */
163
+ /** @type {import('vite').EnvironmentModuleNode [] } */
154
164
const module_nodes = [ ] ;
155
165
156
166
result . index = index ;
@@ -190,11 +200,11 @@ export async function dev(vite, vite_config, svelte_config) {
190
200
// in dev we inline all styles to avoid FOUC. this gets populated lazily so that
191
201
// components/stylesheets loaded via import() during `load` are included
192
202
result . inline_styles = async ( ) => {
193
- /** @type {Set<import('vite').ModuleNode > } */
203
+ /** @type {Set<import('vite').EnvironmentModuleNode > } */
194
204
const deps = new Set ( ) ;
195
205
196
206
for ( const module_node of module_nodes ) {
197
- await find_deps ( vite , module_node , deps ) ;
207
+ await find_deps ( vite , module_node , deps , viteEnv ) ;
198
208
}
199
209
200
210
/** @type {Record<string, string> } */
@@ -236,7 +246,7 @@ export async function dev(vite, vite_config, svelte_config) {
236
246
endpoint : endpoint
237
247
? async ( ) => {
238
248
const url = path . resolve ( cwd , endpoint . file ) ;
239
- return await loud_ssr_load_module ( url ) ;
249
+ return await load_ssr_load_module ( url ) ;
240
250
}
241
251
: null ,
242
252
endpoint_id : endpoint ?. file
@@ -569,39 +579,45 @@ function remove_static_middlewares(server) {
569
579
570
580
/**
571
581
* @param {import('vite').ViteDevServer } vite
572
- * @param {import('vite').ModuleNode } node
573
- * @param {Set<import('vite').ModuleNode> } deps
582
+ * @param {import('vite').EnvironmentModuleNode } node
583
+ * @param {Set<import('vite').EnvironmentModuleNode> } deps
584
+ * @param {import('vite').DevEnvironment } viteEnv
574
585
*/
575
- async function find_deps ( vite , node , deps ) {
586
+ async function find_deps ( vite , node , deps , viteEnv ) {
587
+ // TODO (43081j): maybe throw an error?
588
+ if ( ! isRunnableDevEnvironment ( viteEnv ) ) {
589
+ return ;
590
+ }
591
+
576
592
// since `ssrTransformResult.deps` contains URLs instead of `ModuleNode`s, this process is asynchronous.
577
593
// instead of using `await`, we resolve all branches in parallel.
578
594
/** @type {Promise<void>[] } */
579
595
const branches = [ ] ;
580
596
581
- /** @param {import('vite').ModuleNode } node */
597
+ /** @param {import('vite').EnvironmentModuleNode } node */
582
598
async function add ( node ) {
583
599
if ( ! deps . has ( node ) ) {
584
600
deps . add ( node ) ;
585
- await find_deps ( vite , node , deps ) ;
601
+ await find_deps ( vite , node , deps , viteEnv ) ;
586
602
}
587
603
}
588
604
589
605
/** @param {string } url */
590
606
async function add_by_url ( url ) {
591
- const node = await vite . moduleGraph . getModuleByUrl ( url ) ;
607
+ const node = await viteEnv . moduleGraph . getModuleByUrl ( url ) ;
592
608
593
609
if ( node ) {
594
610
await add ( node ) ;
595
611
}
596
612
}
597
613
598
- if ( node . ssrTransformResult ) {
599
- if ( node . ssrTransformResult . deps ) {
600
- node . ssrTransformResult . deps . forEach ( ( url ) => branches . push ( add_by_url ( url ) ) ) ;
614
+ if ( node . transformResult ) {
615
+ if ( node . transformResult . deps ) {
616
+ node . transformResult . deps . forEach ( ( url ) => branches . push ( add_by_url ( url ) ) ) ;
601
617
}
602
618
603
- if ( node . ssrTransformResult . dynamicDeps ) {
604
- node . ssrTransformResult . dynamicDeps . forEach ( ( url ) => branches . push ( add_by_url ( url ) ) ) ;
619
+ if ( node . transformResult . dynamicDeps ) {
620
+ node . transformResult . dynamicDeps . forEach ( ( url ) => branches . push ( add_by_url ( url ) ) ) ;
605
621
}
606
622
} else {
607
623
node . importedModules . forEach ( ( node ) => branches . push ( add ( node ) ) ) ;
0 commit comments