Skip to content

Commit

Permalink
fix loading of conditional Vite configs (#5376)
Browse files Browse the repository at this point in the history
* fix loading of conditional Vite configs (#5372)

* add changeset

* use loadConfigFromFile to load Vite config

* Update .changeset/poor-knives-yawn.md

Co-authored-by: Rich Harris <hello@rich-harris.dev>
Co-authored-by: Rich Harris <richard.a.harris@gmail.com>
  • Loading branch information
3 people authored Jul 6, 2022
1 parent 730bd3e commit a703893
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-knives-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] support conditional Vite configs
13 changes: 11 additions & 2 deletions packages/kit/src/vite/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class Server {
* @param {{
* cwd: string;
* config: import('types').ValidatedConfig
* vite_config_env: import('vite').ConfigEnv
* manifest_data: import('types').ManifestData
* build_dir: string;
* output_dir: string;
Expand All @@ -115,7 +116,15 @@ export class Server {
* @param {{ vite_manifest: import('vite').Manifest, assets: import('rollup').OutputAsset[] }} client
*/
export async function build_server(options, client) {
const { cwd, config, manifest_data, build_dir, output_dir, service_worker_entry_file } = options;
const {
cwd,
config,
vite_config_env,
manifest_data,
build_dir,
output_dir,
service_worker_entry_file
} = options;

let hooks_file = resolve_entry(config.kit.files.hooks);
if (!hooks_file || !fs.existsSync(hooks_file)) {
Expand Down Expand Up @@ -174,7 +183,7 @@ export async function build_server(options, client) {
})
);

const vite_config = await get_vite_config();
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` }),
Expand Down
5 changes: 3 additions & 2 deletions packages/kit/src/vite/build/build_service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { assets_base, remove_svelte_kit } from './utils.js';
/**
* @param {{
* config: import('types').ValidatedConfig;
* vite_config_env: import('vite').ConfigEnv;
* manifest_data: import('types').ManifestData;
* output_dir: string;
* service_worker_entry_file: string | null;
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, manifest_data, output_dir, service_worker_entry_file },
{ config, vite_config_env, manifest_data, output_dir, service_worker_entry_file },
prerendered,
client_manifest
) {
Expand Down Expand Up @@ -65,7 +66,7 @@ export async function build_service_worker(
.trim()
);

const vite_config = await get_vite_config();
const vite_config = await get_vite_config(vite_config_env);
const merged_config = merge_vite_configs(vite_config, {
base: assets_base(config.kit),
build: {
Expand Down
9 changes: 7 additions & 2 deletions packages/kit/src/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ function kit() {
/** @type {import('vite').UserConfig} */
let vite_config;

/** @type {import('vite').ConfigEnv} */
let vite_config_env;

/** @type {import('types').ManifestData} */
let manifest_data;

Expand Down Expand Up @@ -117,10 +120,11 @@ function kit() {
return {
name: 'vite-plugin-svelte-kit',

async config(config, { command }) {
async config(config, config_env) {
vite_config = config;
vite_config_env = config_env;
svelte_config = await load_config();
is_build = command === 'build';
is_build = config_env.command === 'build';

paths = {
build_dir: `${svelte_config.kit.outDir}/build`,
Expand Down Expand Up @@ -239,6 +243,7 @@ function kit() {
const options = {
cwd,
config: svelte_config,
vite_config_env,
build_dir: paths.build_dir, // TODO just pass `paths`
manifest_data,
output_dir: paths.output_dir,
Expand Down
15 changes: 7 additions & 8 deletions packages/kit/src/vite/utils.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import fs from 'fs';
import path from 'path';
import { pathToFileURL } from 'url';
import { loadConfigFromFile } from 'vite';
import { get_runtime_path } from '../core/utils.js';

/**
* @param {import('vite').ConfigEnv} config_env
* @return {Promise<import('vite').UserConfig>}
*/
export async function get_vite_config() {
for (const file of ['vite.config.js', 'vite.config.mjs', 'vite.config.cjs']) {
if (fs.existsSync(file)) {
const config = await import(pathToFileURL(file).toString());
return config.default || config;
}
export async function get_vite_config(config_env) {
const config = (await loadConfigFromFile(config_env))?.config;
if (!config) {
throw new Error('Could not load Vite config');
}
throw new Error('Could not find vite.config.js');
return config;
}

/**
Expand Down

0 comments on commit a703893

Please sign in to comment.