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

Commit

Permalink
feat(constitute): Uses DI container
Browse files Browse the repository at this point in the history
BREAKING CHANGE: All `Config*.INSTANCE` methods were removed and they are available as separate instances (please see https://github.com/mdreizin/webpack-config/wiki/Changelog)
  • Loading branch information
mdreizin authored Aug 4, 2016
1 parent 8cf605d commit 348c33f
Show file tree
Hide file tree
Showing 41 changed files with 552 additions and 415 deletions.
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"Array"
]
}],
"add-module-exports",
"transform-runtime"
],
"ignore": [
Expand Down
4 changes: 4 additions & 0 deletions .gitdown/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
- [x] Supports environment variables under `#extend()` method
- [x] Supports shareable configs

## Changelog

Details changes for each release are documented in the [release notes]({"gitdown": "gitinfo", "name": "url"}/releases) and also in the [wiki page]({"gitdown": "gitinfo", "name": "url"}/wiki/Changelog).

## Shareable Configs

You can publish your configs to `npm` using `webpack-config-` prefix for package name.
Expand Down
7 changes: 2 additions & 5 deletions .gitdown/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import {
Config,
ConfigEnvironment
} from 'webpack-config';
import Config, { environment } from 'webpack-config';

ConfigEnvironment.INSTANCE.setAll({
environment.setAll({
env: () => process.env.NODE_ENV
});

Expand Down
4 changes: 2 additions & 2 deletions .gitdown/webpack.npm.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import WebpackConfig from 'webpack-config';
import Config from 'webpack-config';

export default new WebpackConfig().extend(
export default new Config().extend(
'mdreizin/base',
'mdreizin/css',
'mdreizin/html',
Expand Down
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![NPM version](http://img.shields.io/npm/v/webpack-config.svg?style=flat-square)](https://www.npmjs.org/package/webpack-config)
[![Travis build status](http://img.shields.io/travis/mdreizin/webpack-config/master.svg?style=flat-square)](https://travis-ci.org/mdreizin/webpack-config)
[![AppVeyor build status](https://img.shields.io/appveyor/ci/mdreizin/webpack-config/master.svg?style=flat-square)](https://ci.appveyor.com/project/mdreizin/webpack-config/branch/master)
[![Travis build status](http://img.shields.io/travis/mdreizin/webpack-config/develop.svg?style=flat-square)](https://travis-ci.org/mdreizin/webpack-config)
[![AppVeyor build status](https://img.shields.io/appveyor/ci/mdreizin/webpack-config/develop.svg?style=flat-square)](https://ci.appveyor.com/project/mdreizin/webpack-config/branch/develop)
[![Code Climate GPA](https://img.shields.io/codeclimate/github/mdreizin/webpack-config.svg?style=flat-square)](https://codeclimate.com/github/mdreizin/webpack-config)
[![Code Climate Coverage](https://img.shields.io/codeclimate/coverage/github/mdreizin/webpack-config.svg?style=flat-square)](https://codeclimate.com/github/mdreizin/webpack-config)
[![Dependency Status](https://img.shields.io/david/mdreizin/webpack-config.svg?style=flat-square)](https://david-dm.org/mdreizin/webpack-config)
Expand All @@ -14,16 +14,20 @@
- [x] Supports environment variables under `#extend()` method
- [x] Supports shareable configs

<h2 id="webpack-config-changelog">Changelog</h2>

Details changes for each release are documented in the [release notes](https://github.com/mdreizin/webpack-config/releases) and also in the [wiki page](https://github.com/mdreizin/webpack-config/wiki/Changelog).

<h2 id="webpack-config-shareable-configs">Shareable Configs</h2>

You can publish your configs to `npm` using `webpack-config-` prefix for package name.

When you call `#extend()` method you may omit that prefix:

```javascript
import WebpackConfig from 'webpack-config';
import Config from 'webpack-config';

export default new WebpackConfig().extend(
export default new Config().extend(
'mdreizin/base',
'mdreizin/css',
'mdreizin/html',
Expand All @@ -38,12 +42,9 @@ export default new WebpackConfig().extend(
`./webpack.config.js`

```javascript
import {
Config,
ConfigEnvironment
} from 'webpack-config';
import Config, { environment } from 'webpack-config';

ConfigEnvironment.INSTANCE.setAll({
environment.setAll({
env: () => process.env.NODE_ENV
});

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"devDependencies": {
"babel-cli": "^6.9.0",
"babel-eslint": "^6.1.1",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-istanbul": "^1.0.3",
"babel-plugin-transform-builtin-extend": "^1.1.0",
"babel-plugin-transform-runtime": "^6.9.0",
Expand All @@ -66,6 +67,7 @@
},
"dependencies": {
"babel-runtime": "^6.9.0",
"constitute": "^1.6.2",
"glob": "^7.0.3",
"glob2base": "0.0.12",
"lodash": "^4.13.1",
Expand Down
47 changes: 30 additions & 17 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
get,
has
} from 'lodash';
import ConfigLoader from './ConfigLoader';
import ConfigExtendTransform from './ConfigExtendTransform';
import ConfigDependency from './ConfigDependency';

Expand Down Expand Up @@ -37,6 +36,12 @@ import ConfigDependency from './ConfigDependency';
*/
const DEPENDENCY_TREE = 'DEPENDENCY_TREE';

/**
* @private
* @type {WeakMap}
*/
const LOADER = new WeakMap();

/**
* @private
* @param {Object|Function} value
Expand All @@ -49,6 +54,23 @@ const evalValue = (value, context) => isFunction(value) ? value.call(context, co
* @class
*/
class Config {
/**
* @constructor
* @param {ConfigLoader} loader
*/
constructor(loader) {
LOADER.set(this, loader);
}

/**
* @protected
* @readonly
* @type {ConfigLoader}
*/
get loader() {
return LOADER.get(this);
}

/**
* @example
* import Config from 'webpack-config';
Expand Down Expand Up @@ -98,8 +120,8 @@ class Config {
* @returns {Config}
*/
defaults(...values) {
for (let value of Object.values(values)) {
let properties = evalValue(value, this);
for (const value of Object.values(values)) {
const properties = evalValue(value, this);

defaultsDeep(this, properties);
}
Expand Down Expand Up @@ -129,8 +151,8 @@ class Config {
* @returns {Config}
*/
merge(...values) {
for (let value of Object.values(values)) {
let properties = evalValue(value, this);
for (const value of Object.values(values)) {
const properties = evalValue(value, this);

mergeWith(this, properties, (x, y) => { // eslint-disable-line consistent-return
if (Array.isArray(x)) {
Expand Down Expand Up @@ -189,7 +211,7 @@ class Config {
const map = ConfigExtendTransform.initWith(...values);

for (const [key, value] of map.entries()) {
const config = ConfigLoader.INSTANCE.loadConfig(key);
const config = this.loader.loadConfig(key);

if (config instanceof Config) {
this.dependencyTree.children.push(config.dependencyTree);
Expand All @@ -206,7 +228,7 @@ class Config {
}

if (!(prevConfig instanceof Config)) {
prevConfig = Config.initWith(prevConfig);
prevConfig = new Config(this.loader).merge(prevConfig);
}
});

Expand Down Expand Up @@ -235,7 +257,7 @@ class Config {
* @returns {Config}
*/
clone() {
return Config.initWith(this.toObject());
return new Config(this.loader).merge(this.toObject());
}

/**
Expand Down Expand Up @@ -351,15 +373,6 @@ class Config {
return this.toObject();
}

/**
* Initializes new {@link Config} with specific `values`
* @param {...Object} values
* @returns {Config}
*/
static initWith(...values) {
return new Config().merge(...values);
}

/**
* Returns `webpack.config.js`
* @readonly
Expand Down
58 changes: 39 additions & 19 deletions src/ConfigBuilder.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
isFunction
} from 'lodash';
import Config from './Config';
import ConfigFactory from './ConfigFactory';

/**
* @private
Expand All @@ -16,6 +14,12 @@ const CONFIG = new WeakMap();
*/
const PENDING_CONFIG = new WeakMap();

/**
* @private
* @type {WeakMap}
*/
const FACTORY = new WeakMap();

/**
* @private
* @param {String} path
Expand All @@ -27,46 +31,62 @@ const PENDING_CONFIG = new WeakMap();
const evalHook = (path, hook, current, previous) => isFunction(hook) ? hook(path, current, previous) : hook;

/**
* @private
* @param {WeakMap} map
* @param {*} context
* @returns {Config}
* @class
*/
const getOrSetConfig = (map, context) => {
if (!map.has(context)) {
map.set(context, new Config());
class ConfigBuilder {
/**
* @constructor
* @param {ConfigFactory} factory
*/
constructor(factory) {
FACTORY.set(this, factory);
}

return map.get(context);
};
/**
* @private
* @readonly
* @type {ConfigFactory}
*/
get factory() {
return FACTORY.get(this);
}

/**
* @class
*/
class ConfigBuilder {
/**
* @private
* @readonly
* @type {Config|ConfigList}
*/
get config() {
return getOrSetConfig(CONFIG, this);
return this.getOrSetConfig(CONFIG);
}

/**
* @readonly
* @type {Config}
*/
get pendingConfig() {
return getOrSetConfig(PENDING_CONFIG, this);
return this.getOrSetConfig(PENDING_CONFIG);
}

/**
* @private
* @param {WeakMap} map
* @returns {Config}
*/
getOrSetConfig(map) {
if (!map.has(this)) {
map.set(this, this.factory.createConfig({}));
}

return map.get(this);
}

/**
* @param {Function|Object|Object[]} value
* @returns {ConfigBuilder}
*/
copyOf(value) {
CONFIG.set(this, ConfigFactory.createConfig(value));
CONFIG.set(this, this.factory.createConfig(value));

return this;
}
Expand Down Expand Up @@ -142,7 +162,7 @@ class ConfigBuilder {
value = this.config.clone().merge(this.pendingConfig).toObject();
}

const config = ConfigFactory.createConfig(value);
const config = this.factory.createConfig(value);

PENDING_CONFIG.delete(this);
CONFIG.set(this, config);
Expand Down
17 changes: 4 additions & 13 deletions src/ConfigCache.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
get
} from 'lodash';
import ConfigEnvironment from './ConfigEnvironment';
import ConfigRegistry from './ConfigRegistry';

/**
* @private
Expand Down Expand Up @@ -39,6 +37,7 @@ class ConfigCache extends Map {
}

/**
* @protected
* @readonly
* @type {ConfigEnvironment}
*/
Expand All @@ -56,12 +55,12 @@ class ConfigCache extends Map {
/**
* @example
* import {
* ConfigCache
* cache
* } from 'webpack-config';
*
* ConfigCache.INSTANCE.persistent = false;
* cache.persistent = false;
* @example
* WEBPACK_CONFIG_CACHE=false npm run build
* WEBPACK_CONFIG_CACHE=false ...
* @param {Boolean} value
*/
set persistent(value) {
Expand Down Expand Up @@ -90,14 +89,6 @@ class ConfigCache extends Map {

return get(value, ES_MODULE_KEY, false) ? value.default : value;
}

/**
* @readonly
* @type {ConfigCache}
*/
static get INSTANCE() {
return ConfigRegistry.INSTANCE.getOrSet(this, () => new ConfigCache(ConfigEnvironment.INSTANCE));
}
}

export default ConfigCache;
9 changes: 0 additions & 9 deletions src/ConfigEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
isFunction,
isUndefined
} from 'lodash';
import ConfigRegistry from './ConfigRegistry';

/**
* @class
Expand Down Expand Up @@ -43,14 +42,6 @@ class ConfigEnvironment extends Map {

return isUndefined(value) ? defaultValue : value;
}

/**
* @readonly
* @type {ConfigEnvironment}
*/
static get INSTANCE() {
return ConfigRegistry.INSTANCE.getOrSet(this, () => new ConfigEnvironment(Object.entries(process.env)));
}
}

export default ConfigEnvironment;
Loading

0 comments on commit 348c33f

Please sign in to comment.