diff --git a/.gitignore b/.gitignore index 4165750..1d27d65 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules .DS_Store Thumbs.db +loconfig.*.json diff --git a/build/tasks/concats.js b/build/tasks/concats.js index 00eccc1..af26a43 100644 --- a/build/tasks/concats.js +++ b/build/tasks/concats.js @@ -1,4 +1,4 @@ -import loconfig from '../../loconfig.json'; +import loconfig from '../utils/config.js'; import glob from '../utils/glob.js'; import message from '../utils/message.js'; import notification from '../utils/notification.js'; diff --git a/build/tasks/scripts.js b/build/tasks/scripts.js index 66102a3..e730385 100644 --- a/build/tasks/scripts.js +++ b/build/tasks/scripts.js @@ -1,4 +1,4 @@ -import loconfig from '../../loconfig.json'; +import loconfig from '../utils/config.js'; import message from '../utils/message.js'; import notification from '../utils/notification.js'; import resolve from '../utils/template.js'; diff --git a/build/tasks/styles.js b/build/tasks/styles.js index 16e5a8b..f358ff8 100644 --- a/build/tasks/styles.js +++ b/build/tasks/styles.js @@ -1,4 +1,4 @@ -import loconfig from '../../loconfig.json'; +import loconfig from '../utils/config.js'; import message from '../utils/message.js'; import notification from '../utils/notification.js'; import postcss, { pluginsMap as postcssPluginsMap } from '../utils/postcss.js'; diff --git a/build/tasks/svgs.js b/build/tasks/svgs.js index 6fd6b2f..7672384 100644 --- a/build/tasks/svgs.js +++ b/build/tasks/svgs.js @@ -1,4 +1,4 @@ -import loconfig from '../../loconfig.json'; +import loconfig from '../utils/config.js'; import message from '../utils/message.js'; import notification from '../utils/notification.js'; import resolve from '../utils/template.js'; diff --git a/build/utils/config.js b/build/utils/config.js new file mode 100644 index 0000000..8fa0f86 --- /dev/null +++ b/build/utils/config.js @@ -0,0 +1,64 @@ +/** + * @file Provides simple user configuration options. + */ + +import loconfig from '../../loconfig.json'; + +let usrconfig; + +try { + usrconfig = await import('../../loconfig.local.json'); + usrconfig = usrconfig.default; + + merge(loconfig, usrconfig); +} catch (err) { + // do nothing +} + +export default loconfig; + +/** + * Creates a new object with all nested object properties + * merged into it recursively. + * + * @param {object} target - The target object. + * @param {object[]} ...sources - The source object(s). + * @throws {TypeError} If the target and source are the same. + * @return {object} Returns the `target` object. + */ +export function merge(target, ...sources) { + for (const source of sources) { + if (target === source) { + throw new TypeError( + 'Cannot merge, target and source are the same' + ); + } + + for (const key in source) { + if (source[key] != null) { + if (isObjectLike(source[key]) && isObjectLike(target[key])) { + merge(target[key], source[key]); + continue; + } else if (Array.isArray(source[key]) && Array.isArray(target[key])) { + target[key] = target[key].concat(source[key]); + continue; + } + } + + target[key] = source[key]; + } + } + + return target; +} + +/** + * Determines whether the passed value is an `Object`. + * + * @param {*} value - The value to be checked. + * @return {boolean} Returns `true` if the value is an `Object`, + * otherwise `false`. + */ +function isObjectLike(value) { + return (value != null && typeof value === 'object'); +} diff --git a/build/utils/template.js b/build/utils/template.js index fa0d2a8..a42bfd1 100644 --- a/build/utils/template.js +++ b/build/utils/template.js @@ -2,7 +2,7 @@ * @file Provides simple template tags. */ -import loconfig from '../../loconfig.json'; +import loconfig from './config.js'; const templateData = flatten({ paths: loconfig.paths diff --git a/build/watch.js b/build/watch.js index d6d4f6d..384c756 100644 --- a/build/watch.js +++ b/build/watch.js @@ -1,8 +1,8 @@ -import loconfig from '../loconfig.json'; import concatFiles, { developmentConcatFilesArgs } from './tasks/concats.js'; import compileScripts, { developmentScriptsArgs } from './tasks/scripts.js'; import compileStyles, { developmentStylesArgs } from './tasks/styles.js' ; import compileSVGs, { developmentSVGsArgs } from './tasks/svgs.js'; +import loconfig, { merge } from './utils/config.js'; import message from './utils/message.js'; import notification from './utils/notification.js'; import resolve from './utils/template.js'; @@ -124,9 +124,7 @@ function createServerOptions({ }; } - Object.assign(config, resolve(options)); - - return config; + return merge(config, resolve(options)); } /**