Skip to content

Commit 5cc38e1

Browse files
committed
feat: migrate to vite environment API
Moves the vite dev server to use the new environments API. This changes the following: - `vite.moduleGraph` is replaced by `vite.environments.ssr.moduleGraph` - `vite.ssrLoadModule` is replaced by `vite.environments.ssr.runner.import(url)`
1 parent 2ca09ce commit 5cc38e1

File tree

1 file changed

+36
-20
lines changed
  • packages/kit/src/exports/vite/dev

1 file changed

+36
-20
lines changed

packages/kit/src/exports/vite/dev/index.js

+36-20
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { URL } from 'node:url';
55
import { AsyncLocalStorage } from 'node:async_hooks';
66
import colors from 'kleur';
77
import sirv from 'sirv';
8-
import { isCSSRequest, loadEnv, buildErrorMessage } from 'vite';
8+
import { isCSSRequest, loadEnv, buildErrorMessage, isRunnableDevEnvironment } from 'vite';
99
import { createReadableStream, getRequest, setResponse } from '../../../exports/node/index.js';
1010
import { installPolyfills } from '../../../exports/node/polyfills.js';
1111
import { coalesce_to_error } from '../../../utils/error.js';
@@ -33,6 +33,12 @@ const vite_css_query_regex = /(?:\?|&)(?:raw|url|inline)(?:&|$)/;
3333
export async function dev(vite, vite_config, svelte_config) {
3434
installPolyfills();
3535

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+
3642
const async_local_storage = new AsyncLocalStorage();
3743

3844
globalThis.__SVELTEKIT_TRACK__ = (label) => {
@@ -64,9 +70,13 @@ export async function dev(vite, vite_config, svelte_config) {
6470
let manifest_error = null;
6571

6672
/** @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+
6878
try {
69-
return await vite.ssrLoadModule(url, { fixStacktrace: true });
79+
return await viteEnv.runner.import(url);
7080
} catch (/** @type {any} */ err) {
7181
const msg = buildErrorMessage(err, [colors.red(`Internal server error: ${err.message}`)]);
7282

@@ -93,9 +103,9 @@ export async function dev(vite, vite_config, svelte_config) {
93103
async function resolve(id) {
94104
const url = id.startsWith('..') ? to_fs(path.posix.resolve(id)) : `/${id}`;
95105

96-
const module = await loud_ssr_load_module(url);
106+
const module = await load_ssr_load_module(url);
97107

98-
const module_node = await vite.moduleGraph.getModuleByUrl(url);
108+
const module_node = await viteEnv.moduleGraph.getModuleByUrl(url);
99109
if (!module_node) throw new Error(`Could not find node for ${url}`);
100110

101111
return { module, module_node, url };
@@ -150,7 +160,7 @@ export async function dev(vite, vite_config, svelte_config) {
150160
/** @type {import('types').SSRNode} */
151161
const result = {};
152162

153-
/** @type {import('vite').ModuleNode[]} */
163+
/** @type {import('vite').EnvironmentModuleNode[]} */
154164
const module_nodes = [];
155165

156166
result.index = index;
@@ -190,11 +200,11 @@ export async function dev(vite, vite_config, svelte_config) {
190200
// in dev we inline all styles to avoid FOUC. this gets populated lazily so that
191201
// components/stylesheets loaded via import() during `load` are included
192202
result.inline_styles = async () => {
193-
/** @type {Set<import('vite').ModuleNode>} */
203+
/** @type {Set<import('vite').EnvironmentModuleNode>} */
194204
const deps = new Set();
195205

196206
for (const module_node of module_nodes) {
197-
await find_deps(vite, module_node, deps);
207+
await find_deps(vite, module_node, deps, viteEnv);
198208
}
199209

200210
/** @type {Record<string, string>} */
@@ -236,7 +246,7 @@ export async function dev(vite, vite_config, svelte_config) {
236246
endpoint: endpoint
237247
? async () => {
238248
const url = path.resolve(cwd, endpoint.file);
239-
return await loud_ssr_load_module(url);
249+
return await load_ssr_load_module(url);
240250
}
241251
: null,
242252
endpoint_id: endpoint?.file
@@ -569,39 +579,45 @@ function remove_static_middlewares(server) {
569579

570580
/**
571581
* @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
574585
*/
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+
576592
// since `ssrTransformResult.deps` contains URLs instead of `ModuleNode`s, this process is asynchronous.
577593
// instead of using `await`, we resolve all branches in parallel.
578594
/** @type {Promise<void>[]} */
579595
const branches = [];
580596

581-
/** @param {import('vite').ModuleNode} node */
597+
/** @param {import('vite').EnvironmentModuleNode} node */
582598
async function add(node) {
583599
if (!deps.has(node)) {
584600
deps.add(node);
585-
await find_deps(vite, node, deps);
601+
await find_deps(vite, node, deps, viteEnv);
586602
}
587603
}
588604

589605
/** @param {string} url */
590606
async function add_by_url(url) {
591-
const node = await vite.moduleGraph.getModuleByUrl(url);
607+
const node = await viteEnv.moduleGraph.getModuleByUrl(url);
592608

593609
if (node) {
594610
await add(node);
595611
}
596612
}
597613

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)));
601617
}
602618

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)));
605621
}
606622
} else {
607623
node.importedModules.forEach((node) => branches.push(add(node)));

0 commit comments

Comments
 (0)