elm-pages
Version:
Hybrid Elm framework with full-stack and static routes.
78 lines (72 loc) • 2.15 kB
JavaScript
/** This code is from https://github.com/sveltejs/kit/blob/3b457f67d4d7c59fc63bb3f600a490e4dacc2e62/packages/kit/src/exports/vite/utils.js */
/**
* @param {...import('vite').UserConfig} configs
* @returns {import('vite').UserConfig}
*/
export function merge_vite_configs(...configs) {
return deep_merge(
...configs.map((config) => ({
...config,
resolve: {
...config.resolve,
alias: normalize_alias((config.resolve && config.resolve.alias) || {}),
},
}))
);
}
/**
* Takes zero or more objects and returns a new object that has all the values
* deeply merged together. None of the original objects will be mutated at any
* level, and the returned object will have no references to the original
* objects at any depth. If there's a conflict the last one wins, except for
* arrays which will be combined.
* @param {...Object} objects
* @returns {Record<string, any>} the merged object
*/
function deep_merge(...objects) {
const result = {};
/** @type {string[]} */
objects.forEach((o) => merge_into(result, o));
return result;
}
/**
* normalize kit.vite.resolve.alias as an array
* @param {import('vite').AliasOptions} o
* @returns {import('vite').Alias[]}
*/
function normalize_alias(o) {
if (Array.isArray(o)) return o;
return Object.entries(o).map(([find, replacement]) => ({
find,
replacement,
}));
}
/**
* Merges b into a, recursively, mutating a.
* @param {Record<string, any>} a
* @param {Record<string, any>} b
*/
function merge_into(a, b) {
/**
* Checks for "plain old Javascript object", typically made as an object
* literal. Excludes Arrays and built-in types like Buffer.
* @param {any} x
*/
const is_plain_object = (x) =>
typeof x === "object" && x.constructor === Object;
for (const prop in b) {
if (is_plain_object(b[prop])) {
if (!is_plain_object(a[prop])) {
a[prop] = {};
}
merge_into(a[prop], b[prop]);
} else if (Array.isArray(b[prop])) {
if (!Array.isArray(a[prop])) {
a[prop] = [];
}
a[prop].push(...b[prop]);
} else {
a[prop] = b[prop];
}
}
}