Skip to content

Commit

Permalink
feat(config): simplify, add fs placeholders
Browse files Browse the repository at this point in the history
quit freezing export, remove getter mechanism, introduce placeholder substitution for fs paths

BREAKING CHANGE: function config values are no longer treated as getters, but rather returned directly
  • Loading branch information
dmbch committed Jan 25, 2018
1 parent 4e5e28a commit f5ccc11
Showing 1 changed file with 24 additions and 35 deletions.
59 changes: 24 additions & 35 deletions packages/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ function resolvePaths(config) {
.forEach(function(key) {
config[key] = (function resolve(item) {
if (typeof item === 'string') {
if (item.indexOf('<') === 0) {
item = item.replace(/^(?:<([^>]+)>)(.*)/, function() {
return path.join(config[arguments[1]], arguments[2]);
});
}
return path.isAbsolute(item) ? item : path.join(root, item);
} else if (Array.isArray(item)) {
return item.map(resolve);
Expand All @@ -46,7 +51,6 @@ function resolvePaths(config) {
function normalizeURLs(config) {
var basePath = config.basePath.replace(/^\/*/, '/').replace(/\/*$/, '');
var assetPath = config.assetPath.replace(/(^\/*|\/*$)/g, '');

return Object.assign(config, {
locations: config.locations
.map(function(location) {
Expand All @@ -60,38 +64,23 @@ function normalizeURLs(config) {
});
}

function freeze(config) {
return Object.freeze(
Object.keys(config).reduce(function(result, key) {
var descriptor = { enumerable: true };
if (typeof config[key] === 'function') {
descriptor.get = config[key];
} else {
descriptor.value = config[key];
}
return Object.defineProperty(result, key, descriptor);
}, {})
);
}

module.exports = freeze(
normalizeURLs(
resolvePaths(
extendConfig({
https: false,
host: '0.0.0.0',
port: 8080,
locations: [],
basePath: '',
assetPath: '',
browsers: '> 1%, last 2 versions, Firefox ESR',
node: 'current',
envVars: { HOPS_MODE: 'dynamic' },
moduleDirs: [],
appDir: '.',
buildDir: 'build',
cacheDir: 'node_modules/.cache/hops',
})
)
)
module.exports = [extendConfig, resolvePaths, normalizeURLs].reduce(
function(res, fn) {
return fn(res);
},
{
https: false,
host: '0.0.0.0',
port: 8080,
locations: [],
basePath: '',
assetPath: '',
browsers: '> 1%, last 2 versions, Firefox ESR',
node: 'current',
envVars: { HOPS_MODE: 'dynamic' },
moduleDirs: [],
appDir: '.',
buildDir: 'build',
cacheDir: 'node_modules/.cache/hops',
}
);

0 comments on commit f5ccc11

Please sign in to comment.