Skip to content

Commit

Permalink
support custom Vite config locations - closes #5702
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jul 25, 2022
1 parent 8cba6ba commit c6219c0
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-glasses-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Support custom Vite config locations
6 changes: 3 additions & 3 deletions packages/kit/src/vite/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export class Server {
* @param {{
* cwd: string;
* config: import('types').ValidatedConfig;
* vite_config: import('vite').ResolvedConfig;
* vite_config_env: import('vite').ConfigEnv;
* manifest_data: import('types').ManifestData;
* build_dir: string;
Expand All @@ -124,6 +125,7 @@ export async function build_server(options, client) {
const {
cwd,
config,
vite_config,
vite_config_env,
manifest_data,
build_dir,
Expand Down Expand Up @@ -188,11 +190,9 @@ export async function build_server(options, client) {
})
);

const vite_config = await get_vite_config(vite_config_env);

const merged_config = merge_vite_configs(
get_default_config({ config, input, ssr: true, outDir: `${output_dir}/server` }),
vite_config
await get_vite_config(vite_config, vite_config_env)
);

remove_svelte_kit(merged_config);
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/vite/build/build_service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { assets_base, remove_svelte_kit } from './utils.js';
/**
* @param {{
* config: import('types').ValidatedConfig;
* vite_config: import('vite').ResolvedConfig;
* vite_config_env: import('vite').ConfigEnv;
* manifest_data: import('types').ManifestData;
* output_dir: string;
Expand All @@ -16,7 +17,7 @@ import { assets_base, remove_svelte_kit } from './utils.js';
* @param {import('vite').Manifest} client_manifest
*/
export async function build_service_worker(
{ config, vite_config_env, manifest_data, output_dir, service_worker_entry_file },
{ config, vite_config, vite_config_env, manifest_data, output_dir, service_worker_entry_file },
prerendered,
client_manifest
) {
Expand Down Expand Up @@ -63,8 +64,7 @@ export async function build_service_worker(
.trim()
);

const vite_config = await get_vite_config(vite_config_env);
const merged_config = merge_vite_configs(vite_config, {
const merged_config = merge_vite_configs(await get_vite_config(vite_config, vite_config_env), {
base: assets_base(config.kit),
build: {
lib: {
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ function kit() {
const options = {
cwd,
config: svelte_config,
vite_config,
vite_config_env,
build_dir: paths.build_dir, // TODO just pass `paths`
manifest_data,
Expand Down
15 changes: 11 additions & 4 deletions packages/kit/src/vite/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@ import { loadConfigFromFile } from 'vite';
import { get_runtime_directory } from '../core/utils.js';

/**
* @param {import('vite').ResolvedConfig} config
* @param {import('vite').ConfigEnv} config_env
* @return {Promise<import('vite').UserConfig>}
*/
export async function get_vite_config(config_env) {
const config = (await loadConfigFromFile(config_env))?.config;
if (!config) {
export async function get_vite_config(config, config_env) {
const loaded = await loadConfigFromFile(
config_env,
config.configFile,
undefined,
config.logLevel
);

if (!loaded) {
throw new Error('Could not load Vite config');
}
return { ...config, mode: config_env.mode };
return { ...loaded.config, mode: config_env.mode };
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/test/apps/options/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"private": true,
"version": "0.0.1",
"scripts": {
"dev": "vite dev",
"build": "vite build --mode custom",
"preview": "vite preview",
"dev": "vite dev -c vite.custom.config.js",
"build": "vite build -c vite.custom.config.js --mode custom",
"preview": "vite preview -c vite.custom.config.js",
"prepare": "node ../../cli.js sync",
"check": "tsc && svelte-check",
"test": "npm run test:dev && npm run test:build",
Expand Down

0 comments on commit c6219c0

Please sign in to comment.