-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: migrate to vite environment API #13357
Draft
43081j
wants to merge
1
commit into
sveltejs:main
Choose a base branch
from
43081j:good-for-the-environment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ import { URL } from 'node:url'; | |
import { AsyncLocalStorage } from 'node:async_hooks'; | ||
import colors from 'kleur'; | ||
import sirv from 'sirv'; | ||
import { isCSSRequest, loadEnv, buildErrorMessage } from 'vite'; | ||
import { isCSSRequest, loadEnv, buildErrorMessage, isRunnableDevEnvironment } from 'vite'; | ||
import { createReadableStream, getRequest, setResponse } from '../../../exports/node/index.js'; | ||
import { installPolyfills } from '../../../exports/node/polyfills.js'; | ||
import { coalesce_to_error } from '../../../utils/error.js'; | ||
|
@@ -33,6 +33,12 @@ const vite_css_query_regex = /(?:\?|&)(?:raw|url|inline)(?:&|$)/; | |
export async function dev(vite, vite_config, svelte_config) { | ||
installPolyfills(); | ||
|
||
const viteEnv = vite.environments.ssr; | ||
|
||
if (!isRunnableDevEnvironment(viteEnv)) { | ||
throw new Error('Cannot use sveltekit in a non-runnable vite environment'); | ||
} | ||
|
||
const async_local_storage = new AsyncLocalStorage(); | ||
|
||
globalThis.__SVELTEKIT_TRACK__ = (label) => { | ||
|
@@ -64,9 +70,13 @@ export async function dev(vite, vite_config, svelte_config) { | |
let manifest_error = null; | ||
|
||
/** @param {string} url */ | ||
async function loud_ssr_load_module(url) { | ||
async function load_ssr_load_module(url) { | ||
if (!isRunnableDevEnvironment(viteEnv)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. repeated logic because these functions are all hoisted before we've done our type guard on L38 |
||
throw new Error('Cannot load SSR module in non-runnable vite environment'); | ||
} | ||
|
||
try { | ||
return await vite.ssrLoadModule(url, { fixStacktrace: true }); | ||
return await viteEnv.runner.import(url); | ||
} catch (/** @type {any} */ err) { | ||
const msg = buildErrorMessage(err, [colors.red(`Internal server error: ${err.message}`)]); | ||
|
||
|
@@ -93,9 +103,9 @@ export async function dev(vite, vite_config, svelte_config) { | |
async function resolve(id) { | ||
const url = id.startsWith('..') ? to_fs(path.posix.resolve(id)) : `/${id}`; | ||
|
||
const module = await loud_ssr_load_module(url); | ||
const module = await load_ssr_load_module(url); | ||
|
||
const module_node = await vite.moduleGraph.getModuleByUrl(url); | ||
const module_node = await viteEnv.moduleGraph.getModuleByUrl(url); | ||
if (!module_node) throw new Error(`Could not find node for ${url}`); | ||
|
||
return { module, module_node, url }; | ||
|
@@ -150,7 +160,7 @@ export async function dev(vite, vite_config, svelte_config) { | |
/** @type {import('types').SSRNode} */ | ||
const result = {}; | ||
|
||
/** @type {import('vite').ModuleNode[]} */ | ||
/** @type {import('vite').EnvironmentModuleNode[]} */ | ||
const module_nodes = []; | ||
|
||
result.index = index; | ||
|
@@ -190,11 +200,11 @@ export async function dev(vite, vite_config, svelte_config) { | |
// in dev we inline all styles to avoid FOUC. this gets populated lazily so that | ||
// components/stylesheets loaded via import() during `load` are included | ||
result.inline_styles = async () => { | ||
/** @type {Set<import('vite').ModuleNode>} */ | ||
/** @type {Set<import('vite').EnvironmentModuleNode>} */ | ||
const deps = new Set(); | ||
|
||
for (const module_node of module_nodes) { | ||
await find_deps(vite, module_node, deps); | ||
await find_deps(vite, module_node, deps, viteEnv); | ||
} | ||
|
||
/** @type {Record<string, string>} */ | ||
|
@@ -236,7 +246,7 @@ export async function dev(vite, vite_config, svelte_config) { | |
endpoint: endpoint | ||
? async () => { | ||
const url = path.resolve(cwd, endpoint.file); | ||
return await loud_ssr_load_module(url); | ||
return await load_ssr_load_module(url); | ||
} | ||
: null, | ||
endpoint_id: endpoint?.file | ||
|
@@ -569,39 +579,45 @@ function remove_static_middlewares(server) { | |
|
||
/** | ||
* @param {import('vite').ViteDevServer} vite | ||
* @param {import('vite').ModuleNode} node | ||
* @param {Set<import('vite').ModuleNode>} deps | ||
* @param {import('vite').EnvironmentModuleNode} node | ||
* @param {Set<import('vite').EnvironmentModuleNode>} deps | ||
* @param {import('vite').DevEnvironment} viteEnv | ||
*/ | ||
async function find_deps(vite, node, deps) { | ||
async function find_deps(vite, node, deps, viteEnv) { | ||
// TODO (43081j): maybe throw an error? | ||
if (!isRunnableDevEnvironment(viteEnv)) { | ||
return; | ||
} | ||
|
||
// since `ssrTransformResult.deps` contains URLs instead of `ModuleNode`s, this process is asynchronous. | ||
// instead of using `await`, we resolve all branches in parallel. | ||
/** @type {Promise<void>[]} */ | ||
const branches = []; | ||
|
||
/** @param {import('vite').ModuleNode} node */ | ||
/** @param {import('vite').EnvironmentModuleNode} node */ | ||
async function add(node) { | ||
if (!deps.has(node)) { | ||
deps.add(node); | ||
await find_deps(vite, node, deps); | ||
await find_deps(vite, node, deps, viteEnv); | ||
} | ||
} | ||
|
||
/** @param {string} url */ | ||
async function add_by_url(url) { | ||
const node = await vite.moduleGraph.getModuleByUrl(url); | ||
const node = await viteEnv.moduleGraph.getModuleByUrl(url); | ||
|
||
if (node) { | ||
await add(node); | ||
} | ||
} | ||
|
||
if (node.ssrTransformResult) { | ||
if (node.ssrTransformResult.deps) { | ||
node.ssrTransformResult.deps.forEach((url) => branches.push(add_by_url(url))); | ||
if (node.transformResult) { | ||
if (node.transformResult.deps) { | ||
node.transformResult.deps.forEach((url) => branches.push(add_by_url(url))); | ||
} | ||
|
||
if (node.ssrTransformResult.dynamicDeps) { | ||
node.ssrTransformResult.dynamicDeps.forEach((url) => branches.push(add_by_url(url))); | ||
if (node.transformResult.dynamicDeps) { | ||
node.transformResult.dynamicDeps.forEach((url) => branches.push(add_by_url(url))); | ||
} | ||
} else { | ||
node.importedModules.forEach((node) => branches.push(add(node))); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should probably be named sth like
vite_env_ssr
(internally we use snake_case)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is just the default environment vite sets upnevermind i misread! yes you're right