From c6219c0c9d05dc5073a1e8ab21ec1bd57d51804a Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 25 Jul 2022 12:02:25 -0400 Subject: [PATCH] support custom Vite config locations - closes #5702 --- .changeset/ninety-glasses-exercise.md | 5 +++++ packages/kit/src/vite/build/build_server.js | 6 +++--- .../kit/src/vite/build/build_service_worker.js | 6 +++--- packages/kit/src/vite/index.js | 1 + packages/kit/src/vite/utils.js | 15 +++++++++++---- packages/kit/test/apps/options/package.json | 6 +++--- .../{vite.config.js => vite.custom.config.js} | 0 7 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 .changeset/ninety-glasses-exercise.md rename packages/kit/test/apps/options/{vite.config.js => vite.custom.config.js} (100%) diff --git a/.changeset/ninety-glasses-exercise.md b/.changeset/ninety-glasses-exercise.md new file mode 100644 index 000000000000..de3aeae3d9f6 --- /dev/null +++ b/.changeset/ninety-glasses-exercise.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +Support custom Vite config locations diff --git a/packages/kit/src/vite/build/build_server.js b/packages/kit/src/vite/build/build_server.js index db7908b0e20a..1b0e245b5872 100644 --- a/packages/kit/src/vite/build/build_server.js +++ b/packages/kit/src/vite/build/build_server.js @@ -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; @@ -124,6 +125,7 @@ export async function build_server(options, client) { const { cwd, config, + vite_config, vite_config_env, manifest_data, build_dir, @@ -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); diff --git a/packages/kit/src/vite/build/build_service_worker.js b/packages/kit/src/vite/build/build_service_worker.js index 7b358c8af87b..19a7134829c2 100644 --- a/packages/kit/src/vite/build/build_service_worker.js +++ b/packages/kit/src/vite/build/build_service_worker.js @@ -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; @@ -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 ) { @@ -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: { diff --git a/packages/kit/src/vite/index.js b/packages/kit/src/vite/index.js index c2d8355bee1f..34fa0858f403 100644 --- a/packages/kit/src/vite/index.js +++ b/packages/kit/src/vite/index.js @@ -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, diff --git a/packages/kit/src/vite/utils.js b/packages/kit/src/vite/utils.js index 1a7eafaf92f0..d92024c960c7 100644 --- a/packages/kit/src/vite/utils.js +++ b/packages/kit/src/vite/utils.js @@ -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} */ -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 }; } /** diff --git a/packages/kit/test/apps/options/package.json b/packages/kit/test/apps/options/package.json index ccb66c4d310c..662c1ac6a470 100644 --- a/packages/kit/test/apps/options/package.json +++ b/packages/kit/test/apps/options/package.json @@ -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", diff --git a/packages/kit/test/apps/options/vite.config.js b/packages/kit/test/apps/options/vite.custom.config.js similarity index 100% rename from packages/kit/test/apps/options/vite.config.js rename to packages/kit/test/apps/options/vite.custom.config.js