Skip to content

Commit

Permalink
enforce root with improved check
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Jun 30, 2022
1 parent 30f2671 commit 4b3c0dc
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions packages/kit/src/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { preview } from './preview/index.js';

const cwd = process.cwd();

/** @type {Record<string, any>} */
const enforced_config = {
base: true,
build: {
Expand Down Expand Up @@ -49,7 +50,8 @@ const enforced_config = {
$lib: true,
'$service-worker': true
}
}
},
root: true
};

/**
Expand Down Expand Up @@ -85,15 +87,6 @@ function kit() {
name: 'vite-plugin-svelte-kit',

async config(config, { command }) {
const overridden = find_overridden_config(config, enforced_config);

if (overridden.length > 0) {
console.log(
colors.bold().red('The following Vite config options will be overridden by SvelteKit:')
);
console.log(overridden.map((key) => ` - ${key}`).join('\n'));
}

vite_user_config = config;
svelte_config = await load_config();

Expand Down Expand Up @@ -128,16 +121,19 @@ function kit() {
input[name] = resolved;
});

return get_default_config({
const result = get_default_config({
config: svelte_config,
input,
ssr: false,
outDir: `${paths.client_out_dir}/immutable`
});

warn_overridden_config(config, result);
return result;
}

// dev and preview config can be shared
return {
const result = {
base: '/',
build: {
rollupOptions: {
Expand All @@ -153,6 +149,7 @@ function kit() {
resolve: {
alias: get_aliases(svelte_config.kit)
},
root: cwd,
server: {
fs: {
allow: [
Expand All @@ -176,6 +173,8 @@ function kit() {
}
}
};
warn_overridden_config(config, result);
return result;
},

buildStart() {
Expand Down Expand Up @@ -330,17 +329,33 @@ function kit() {

/**
* @param {Record<string, any>} config
* @param {Record<string, any>} enforced_config
* @param {Record<string, any>} resolved_config
* @param {string} [path]
* @param {string[]} [out] used locally to compute the return value
*/
function find_overridden_config(config, enforced_config, path = '', out = []) {
function warn_overridden_config(config, resolved_config, path = '', out = []) {
const overridden = find_overridden_config(config, resolved_config, path, out);
if (overridden.length > 0) {
console.log(
colors.bold().red('The following Vite config options will be overridden by SvelteKit:')
);
console.log(overridden.map((key) => ` - ${key}`).join('\n'));
}
}

/**
* @param {Record<string, any>} config
* @param {Record<string, any>} resolved_config
* @param {string} path
* @param {string[]} out used locally to compute the return value
*/
function find_overridden_config(config, resolved_config, path, out) {
for (const key in enforced_config) {
if (key in config) {
if (enforced_config[key] === true) {
if (enforced_config[key] === true && config[key] !== resolved_config[key]) {
out.push(path + key);
} else {
find_overridden_config(config[key], enforced_config[key], path + key + '.', out);
find_overridden_config(config[key], resolved_config[key], path + key + '.', out);
}
}
}
Expand Down

0 comments on commit 4b3c0dc

Please sign in to comment.