Skip to content

Commit

Permalink
fix loading of conditional Vite configs (sveltejs#5372)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conduitry committed Jul 6, 2022
1 parent 4842a24 commit 0e5b226
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
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 @@ -114,8 +117,9 @@ 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();

paths = {
Expand All @@ -124,7 +128,7 @@ function kit() {
client_out_dir: `${svelte_config.kit.outDir}/output/client/${svelte_config.kit.appDir}`
};

if (command === 'build') {
if (config_env.command === 'build') {
process.env.VITE_SVELTEKIT_APP_VERSION = svelte_config.kit.version.name;
process.env.VITE_SVELTEKIT_APP_VERSION_FILE = `${svelte_config.kit.appDir}/version.json`;
process.env.VITE_SVELTEKIT_APP_VERSION_POLL_INTERVAL = `${svelte_config.kit.version.pollInterval}`;
Expand Down Expand Up @@ -233,6 +237,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
8 changes: 5 additions & 3 deletions packages/kit/src/vite/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { pathToFileURL } from 'url';
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() {
export async function get_vite_config(config_env) {
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;
const m = await import(pathToFileURL(file).toString());
const config = m.default || m;
return typeof config === 'function' ? config(config_env) : config;
}
}
throw new Error('Could not find vite.config.js');
Expand Down

0 comments on commit 0e5b226

Please sign in to comment.