Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mdreizin committed Dec 11, 2015
2 parents eed07e7 + 61dd213 commit e2df5f7
Show file tree
Hide file tree
Showing 56 changed files with 2,436 additions and 977 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ module.exports = new WebpackConfig().extend({

var WebpackConfig = require('webpack-config');

// Please use `process.env.WEBPACK_ENV || process.env.NODE_ENV` to set `[env]` variable or override it via `WebpackConfig.environment.set({ env: 'dev' })`.
WebpackConfig.environment.setAll({
env: function() {
return process.env.WEBPACK_ENV || process.env.NODE_ENV;
}
});

module.exports = new WebpackConfig().extend('./conf/webpack.[env].config.js');

```
10 changes: 5 additions & 5 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
version: '{build}'
build: off
deploy: off
cache:
- node_modules
- '%APPDATA%\npm-cache'
#cache:
# - node_modules
# - '%APPDATA%\npm-cache'
branches:
only:
- master
Expand All @@ -22,9 +22,9 @@ platform:
- x64
install:
- ps: Install-Product node $env:nodejs_version
# - npm dedupe
# - npm prune
- npm install
- npm dedupe
- npm prune
test_script:
- node --version
- npm --version
Expand Down
647 changes: 527 additions & 120 deletions docs/API.md

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion docs/samples/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@

var WebpackConfig = require('webpack-config');

// Please use `process.env.WEBPACK_ENV || process.env.NODE_ENV` to set `[env]` variable or override it via `WebpackConfig.environment.set({ env: 'dev' })`.
WebpackConfig.environment.setAll({
env: function() {
return process.env.WEBPACK_ENV || process.env.NODE_ENV;
}
});

module.exports = new WebpackConfig().extend('./conf/webpack.[env].config.js');
41 changes: 13 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,48 @@
'use strict';

var Config = require('./lib/config'),
ConfigEnvironment = require('./lib/configEnvironment'),
ConfigNameResolver = require('./lib/configNameResolver'),
ConfigPathResolver = require('./lib/configPathResolver'),
ConfigFactory = require('./lib/configFactory'),
ConfigLoader = require('./lib/configLoader'),
ConfigFinder = require('./lib/configFinder'),
ConfigVisitor = require('./lib/configVisitor');

var configEnvironment = new ConfigEnvironment(),
configFactory = new ConfigFactory(),
configNameResolver = new ConfigNameResolver(configEnvironment),
configPathResolver = new ConfigPathResolver(configNameResolver),
configLoader = new ConfigLoader(configFactory, configPathResolver),
configVisitor = new ConfigVisitor(configLoader, configPathResolver),
configFinder = new ConfigFinder(configLoader, configPathResolver);
DefaultConfigEnvironment = require('./lib/defaultConfigEnvironment'),
DefaultConfigNameResolver = require('./lib/defaultConfigNameResolver'),
DefaultConfigPathResolver = require('./lib/defaultConfigPathResolver'),
DefaultConfigFactory = require('./lib/defaultConfigFactory'),
DefaultConfigLoader = require('./lib/defaultConfigLoader'),
DefaultConfigFinder = require('./lib/defaultConfigFinder');

/**
* @property {ConfigEnvironment}
* @static
*/
Config.environment = configEnvironment;
Config.environment = DefaultConfigEnvironment.INSTANCE;

/**
* @property {ConfigNameResolver}
* @static
*/
Config.nameResolver = configNameResolver;
Config.nameResolver = DefaultConfigNameResolver.INSTANCE;

/**
* @property {ConfigFinder}
* @property {ConfigFactory}
* @static
*/
Config.factory = configFactory;
Config.factory = DefaultConfigFactory.INSTANCE;

/**
* @property {ConfigLoader}
* @static
*/
Config.loader = configLoader;
Config.loader = DefaultConfigLoader.INSTANCE;

/**
* @property {ConfigFinder}
* @static
*/
Config.finder = configFinder;

/**
* @property {ConfigVisitor}
* @static
*/
Config.visitor = configVisitor;
Config.finder = DefaultConfigFinder.INSTANCE;

/**
* @property {ConfigPathResolver}
* @static
*/
Config.pathResolver = configPathResolver;
Config.pathResolver = DefaultConfigPathResolver.INSTANCE;

/**
* @const {String} - `webpack.config.js`
Expand Down
14 changes: 14 additions & 0 deletions jasmine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var Jasmine = require('jasmine'),
SpecReporter = require('jasmine-spec-reporter');

var JASMINE_CONFIG_PATH = './jasmine.json',
jasmine = new Jasmine();

jasmine.configureDefaultReporter({
print: function() {}
});
jasmine.addReporter(new SpecReporter());
jasmine.loadConfigFile(JASMINE_CONFIG_PATH);
jasmine.execute();
6 changes: 6 additions & 0 deletions jasmine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"spec_dir": "test",
"spec_files": [
"**/*[sS]pec.js"
]
}
92 changes: 92 additions & 0 deletions lib/closestConfigFinderStrategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict';

var _ = require('lodash'),
util = require('util'),
fs = require('fs'),
path = require('path'),
ConfigFinderStrategy = require('./configFinderStrategy'),
DefaultConfigLoader = require('./defaultConfigLoader'),
DefaultConfigPathResolver = require('./defaultConfigPathResolver');

/**
* @alias ClosestConfigFinderStrategy
* @class
* @constructor
* @implements {ConfigFinderStrategy}
* @param {ConfigLoader} loader
* @param {ConfigPathResolver} pathResolver
*/
function ClosestConfigFinderStrategy(loader, pathResolver) {
this.loader = loader;
this.pathResolver = pathResolver;
}

util.inherits(ClosestConfigFinderStrategy, ConfigFinderStrategy);

/**
* @private
* @param {String} filename
* @param {String[]} visited
* @returns {Config|Config[]}
*/
ClosestConfigFinderStrategy.prototype.findClosestConfig = function(filename, visited) {
if (_.includes(visited, filename)) {
return null;
}

filename = this.resolvePath(filename);

visited.push(filename);

if (fs.existsSync(filename)) {
return this.loadConfig(filename);
}

var dirname = path.dirname(filename),
paths = dirname.split(path.sep),
basename = path.basename(filename);

dirname = paths.slice(0, paths.length - 1).join(path.sep);
filename = path.join(dirname, basename);

return this.findClosestConfig(filename, visited);
};

/**
* @private
* @param {String} filename
* @returns {Config|Config[]}
*/
ClosestConfigFinderStrategy.prototype.loadConfig = function(filename) {
return this.loader.loadConfig(filename);
};

/**
* @private
* @param {String} filename
* @returns {String}
*/
ClosestConfigFinderStrategy.prototype.resolvePath = function(filename) {
return this.pathResolver.resolvePath(filename);
};

/**
* @override
*/
ClosestConfigFinderStrategy.prototype.findConfig = function(filename) {
return this.findClosestConfig(filename, []);
};

/**
* @private
* @constant
* @type {ClosestConfigFinderStrategy}
*/
var INSTANCE = new ClosestConfigFinderStrategy(DefaultConfigLoader.INSTANCE, DefaultConfigPathResolver.INSTANCE);

/**
* @module webpack-config/lib/closestConfigFinderStrategy
* @returns {ClosestConfigFinderStrategy}
*/
module.exports = ClosestConfigFinderStrategy;
module.exports.INSTANCE = INSTANCE;
81 changes: 25 additions & 56 deletions lib/configEnvironment.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,49 @@
'use strict';

var _ = require('lodash');

/**
* @private
* @constant
* @type {Object}
* @alias ConfigEnvironment
* @interface
*/
var DEFAULT_VARIABLES = {
/**
* Returns `process.env.WEBPACK_ENV || process.env.NODE_ENV`
* @function
* @returns {String}
*/
env: function() {
return this.WEBPACK_ENV || this.NODE_ENV;
}
};
function ConfigEnvironment() {}

/**
* Adds `process.env` to `variables` by default
* @class
* @alias ConfigEnvironment
* @param {...Object} arguments
* @constructor
* Adds a new element with a specified `key` and `value`
* @abstract
* @param {*} key
* @param {*} value
* @returns {ConfigEnvironment}
*/
function ConfigEnvironment() {
var args = _.flatten(_.toArray(arguments), true);

this.reset();
this.set(args);
}
ConfigEnvironment.prototype.set = function(key, value) {}; // eslint-disable-line

/**
* Adds custom `variables`
* @param {...Object} arguments
* Adds a new key/value pairs
* @abstract
* @param {Object} map
* @returns {ConfigEnvironment}
*/
ConfigEnvironment.prototype.set = function() {
var args = _.flatten(_.toArray(arguments), true);

args.forEach(function(variables) {
this.variables = _.merge(this.variables, variables);
}, this);
};
ConfigEnvironment.prototype.setAll = function(map) {}; // eslint-disable-line

/**
* Gets keys
* Returns array that contains the `keys`
* @abstract
* @returns {String[]}
*/
ConfigEnvironment.prototype.keys = function() {
return _.keys(this.variables);
};
ConfigEnvironment.prototype.keys = function() {};

/**
* Gets value from `variables` or `process.env` as fallback
* @param {String} key
* Returns the element associated with the specified `key` or `defaultValue` if `key` can't be found
* @abstract
* @param {*} key
* @param {*=} defaultValue
* @returns {*}
*/
ConfigEnvironment.prototype.get = function(key) {
var value = _.result(this.variables, key);

if (_.isUndefined(value)) {
value = _.result(process.env, key);
}

return value;
};
ConfigEnvironment.prototype.get = function(key, defaultValue) {}; // eslint-disable-line

/**
* Resets `variables` to default state
* Removes all key/value pairs
* @abstract
*/
ConfigEnvironment.prototype.reset = function() {
this.variables = {};

this.set(process.env, DEFAULT_VARIABLES);
};
ConfigEnvironment.prototype.clear = function() {};

/**
* @module webpack-config/lib/configEnvironment
Expand Down
25 changes: 14 additions & 11 deletions lib/configExtendMixin.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict';

var _ = require('lodash'),
ConfigVisitor = require('./configVisitor');

/**
* Extend options
* @alias ExtendOptions
* @typedef {(String[]|Object<String,Function>|Object<String,Boolean>)}
*/
ConfigLoader = require('./configLoader'),
ConfigPathResolver = require('./configPathResolver'),
ConfigTransformInvoker = require('./configTransformInvoker'),
ConfigTransformBuilder = require('./configTransformBuilder');

/**
* @mixin
Expand All @@ -17,16 +14,22 @@ var ConfigExtendMixin = {
/**
* Extends config
* @function
* @param {...ExtendOptions} arguments
* @param {...(String[]|Object<String,Function>|Object<String,Function[]>|Object<String,Boolean>)} arguments
* @returns {Config}
*/
extend: function() {
var configVisitor = this.constructor.visitor;
var pathResolver = this.constructor.pathResolver,
loader = this.constructor.loader,
canExtend = pathResolver instanceof ConfigPathResolver && loader instanceof ConfigLoader;

if (configVisitor instanceof ConfigVisitor) {
if (canExtend) {
var args = _.flatten(_.toArray(arguments), true);

configVisitor.visit(args, this.merge, this);
var map = new ConfigTransformBuilder(pathResolver).setAll(args).build();

var invoker = new ConfigTransformInvoker(loader);

invoker.forEach(map, this.merge, this);
}

return this;
Expand Down
Loading

0 comments on commit e2df5f7

Please sign in to comment.