Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 20 additions & 27 deletions packages/kit/src/core/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,13 @@ export async function load_explicit_env(kit, file, root, mode) {
}

/**
* Creates the `__sveltekit/env` module
* Creates the `<sveltekit:generated>/env/config.js` module
* @param {Record<string, EnvVarConfig<any> | undefined> | null} variables
* @param {Record<string, string>} env
* @param {string | null} entry
* @param {boolean} is_dev
*/
export function create_sveltekit_env(variables, env, entry) {
export function create_sveltekit_env(variables, env, entry, is_dev) {
const imports = entry
? [
`import { variables } from ${JSON.stringify(entry)};`,
Expand Down Expand Up @@ -170,33 +171,25 @@ export function create_sveltekit_env(variables, env, entry) {
}`
];

return blocks.join('\n\n');
}
if (is_dev) {
// In dev, initialise the env immediately. Tools like `vite-node` load modules
// through the Vite config but don't run the SvelteKit dev server, which is what
// normally calls `set_env`. Without this, dynamic env vars imported from
// `$app/env/public` and `$app/env/private` would be `undefined` in such contexts.
/** @type {Record<string, string>} */
const dev_env = {};
for (const name of Object.keys(variables ?? {})) {
if (name in env) dev_env[name] = env[name];
}

/**
* @param {Record<string, EnvVarConfig<any> | undefined> | null} variables
* @param {Record<string, string>} env
*/
export function create_sveltekit_env_dev(variables, env) {
// In dev, initialise the env immediately. Tools like `vite-node` load modules
// through the Vite config but don't run the SvelteKit dev server, which is what
// normally calls `set_env`. Without this, dynamic env vars imported from
// `$app/env/public` and `$app/env/private` would be `undefined` in such contexts.
/** @type {Record<string, string>} */
const dev_env = {};
for (const name of Object.keys(variables ?? {})) {
if (name in env) dev_env[name] = env[name];
blocks.push(`set_env(${devalue.uneval(dev_env)});`);
}

return [
`import { set_env } from './config.js';`,
`set_env(${devalue.uneval(dev_env)});`,
`export * from './config.js';`
].join('\n\n');
return blocks.join('\n\n');
}

/**
* Creates the `__sveltekit/env/private` module
* Creates the `<sveltekit:generated>/env/private/server.js` module
* @param {Record<string, EnvVarConfig<any>> | null} variables
* @param {Record<string, string>} env
*/
Expand All @@ -223,11 +216,11 @@ export function create_sveltekit_env_private(variables, env) {

handle_issues(issues);

return `import { dynamic_private_env as env } from '__sveltekit/env';\n\n${exports.join('')}`;
return `import { dynamic_private_env as env } from '../config.js';\n\n${exports.join('')}`;
}

/**
* Creates the `__sveltekit/env/public/*` modules
* Creates the `<sveltekit:generated>/env/public/*` modules
* @param {Record<string, EnvVarConfig<any>> | null} variables
* @param {Record<string, string>} env
* @param {string} prelude
Expand Down Expand Up @@ -259,7 +252,7 @@ export function create_sveltekit_env_public(variables, env, prelude) {
}

/**
* Creates the `__sveltekit/env/service-worker` module used in production. When an app uses
* Creates the `<sveltekit:generated>/env/service-worker.js` module used in production. When an app uses
* dynamic public env vars, they're loaded at runtime via an import of the prerendered
* `env.js`. If there are none, values are inlined.
* @param {Record<string, EnvVarConfig<any>> | null} variables
Expand Down Expand Up @@ -297,7 +290,7 @@ export function create_sveltekit_env_service_worker(
}

/**
* Creates the `__sveltekit/env/service-worker` module used in development
* Creates the `<sveltekit:generated>/env/service-worker.js` module used in development
* @param {Record<string, EnvVarConfig<any>> | null} variables
* @param {Record<string, string>} env
* @param {string} version
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/postbuild/analyse.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function analyse({

// `set_env` lives in a separate module that imports the user's `src/env` config. We import it
// *after* `set_building()` so that `building`-dependent expressions resolve correctly
/** @type {typeof import('__sveltekit/env')} */
/** @type {typeof import('<sveltekit:generated>/env/config.js')} */
const { set_env } = await import(pathToFileURL(`${server_root}/server/env.js`).href);
set_env(env);

Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/postbuild/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async function prerender({

// `set_env` and `Server` live in modules that import the user's `src/env` config. We import them
// *after* `set_building()` so that `building`-dependent expressions resolve correctly
/** @type {typeof import('__sveltekit/env')} */
/** @type {typeof import('<sveltekit:generated>/env/config.js')} */
const { set_env } = await import(pathToFileURL(`${out}/server/env.js`).href);
set_env(env);

Expand Down
26 changes: 19 additions & 7 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ const enforced_config = {
resolve: {
alias: {
$app: true,
$env: true
$env: true,
'<sveltekit:generated>': true
}
}
};
Expand Down Expand Up @@ -455,6 +456,10 @@ function kit({ svelte_config }) {
{ find: '__SERVER__', replacement: `${generated}/server` },
{ find: '$app', replacement: `${runtime_directory}/app` },
{ find: '$env', replacement: `${runtime_directory}/env` },
{
find: '<sveltekit:generated>',
replacement: `${out_dir}/generated/${is_build ? 'build' : 'dev'}`
},
{
find: '__sveltekit/server',
replacement: `${runtime_directory}/server/internal.js`
Expand Down Expand Up @@ -504,7 +509,7 @@ function kit({ svelte_config }) {
// because they for example use rolldown.build with `platform: 'browser'`
'esm-env',
// This forces `$app/*` modules to be bundled, since they depend on
// virtual modules like `__sveltekit/env` (this isn't a valid bare
// generated modules like `<sveltekit:generated>/env/config.js` (this isn't a valid bare
// import, but it works with vite-node's externalization logic, which
// uses basic concatenation)
'@sveltejs/kit/src/runtime'
Expand Down Expand Up @@ -1063,6 +1068,9 @@ function kit({ svelte_config }) {
const new_config = {
environments: {
serviceWorker: {
define: {
__SVELTEKIT_PAYLOAD__: kit_global
},
build: {
modulePreload: false,
rolldownOptions: {
Expand Down Expand Up @@ -1198,7 +1206,7 @@ function kit({ svelte_config }) {
// build time, so `env.js` is loaded at runtime. In dev, the
// imported module just inlines the current values instead.
return {
code: `import '__sveltekit/env/service-worker';\n${code}`
code: `import '<sveltekit:generated>/env/service-worker.js';\n${code}`
};
}
}
Expand Down Expand Up @@ -1231,7 +1239,7 @@ function kit({ svelte_config }) {
const server_input = {
index: `${runtime_directory}/server/index.js`,
internal: `${out_dir}/generated/server/internal.js`,
env: '__sveltekit/env',
env: '<sveltekit:generated>/env/config.js',
['remote-entry']: `${runtime_directory}/app/server/remote/index.js`
};

Expand Down Expand Up @@ -1765,7 +1773,8 @@ function kit({ svelte_config }) {
has_explicit_dynamic_public_env &&
client_chunks.some(
(chunk) =>
chunk.type === 'chunk' && chunk.modules[`${out_dir}/generated/env/public/client.js`]
chunk.type === 'chunk' &&
chunk.modules[`${out_dir}/generated/build/env/public/client.js`]
);

if (kit.output.bundleStrategy === 'split') {
Expand Down Expand Up @@ -1965,8 +1974,11 @@ function kit({ svelte_config }) {
log.info('Building service worker');

// mirror client settings that we couldn't set per environment in the config hook
builder.environments.serviceWorker.config.define =
builder.environments.client.config.define;
builder.environments.serviceWorker.config.define = {
...builder.environments.client.config.define,
...builder.environments.serviceWorker.config.define
};

builder.environments.serviceWorker.config.resolve.alias = [
...get_config_aliases(kit, vite_config.root)
];
Expand Down
Loading
Loading