UNPKG

webpack-config

Version:

Helps to load, extend and merge webpack configs

367 lines (321 loc) 9.56 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _entries = require('babel-runtime/core-js/object/entries'); var _entries2 = _interopRequireDefault(_entries); var _getIterator2 = require('babel-runtime/core-js/get-iterator'); var _getIterator3 = _interopRequireDefault(_getIterator2); var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray'); var _slicedToArray3 = _interopRequireDefault(_slicedToArray2); var _weakMap = require('babel-runtime/core-js/weak-map'); var _weakMap2 = _interopRequireDefault(_weakMap); var _lodash = require('lodash'); var _ConfigDependency = require('./ConfigDependency'); var _ConfigDependency2 = _interopRequireDefault(_ConfigDependency); var _ConfigCommandInvoker = require('./ConfigCommandInvoker'); var _ConfigCommandInvoker2 = _interopRequireDefault(_ConfigCommandInvoker); var _ConfigCommandNames = require('./ConfigCommandNames'); var commandNames = _interopRequireWildcard(_ConfigCommandNames); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * @private * @type {WeakMap} */ const DEPENDENCY_TREE = new _weakMap2.default(); /** * @private * @type {WeakMap} */ const COMMAND_FACTORY = new _weakMap2.default(); /** * @class */ class Config { /** * @constructor * @param {ConfigCommandFactory} commandFactory */ constructor(commandFactory) { COMMAND_FACTORY.set(this, commandFactory); } /** * @readonly * @type {ConfigCommandFactory} */ get commandFactory() { return COMMAND_FACTORY.get(this); } /** * @example * import Config from 'webpack-config'; * * const config = new Config(); * * config.extend('./test/fixtures/webpack.1.config.js'); * * for (const {node} of config.dependencyTree) { * console.log(node.root.filename); * } * // ./test/fixtures/webpack.1.config.js * // ./test/fixtures/webpack.2.config.js * // ./test/fixtures/webpack.3.config.js * // ./test/fixtures/webpack.5.config.js * // ./test/fixtures/webpack.4.config.js * @description Holds information about [included]{@link Config#extend} configs * @readonly * @type {ConfigDependency} */ get dependencyTree() { if (!DEPENDENCY_TREE.has(this)) { DEPENDENCY_TREE.set(this, new _ConfigDependency2.default(this)); } return DEPENDENCY_TREE.get(this); } /** * @private * @param {ConfigDependency} value */ set dependencyTree(value) { DEPENDENCY_TREE.set(this, value); } /** * import Config from 'webpack-config'; * * export default new Config().defaults({ * debug: true * }, { * profile: false * }); * @example * import Config from 'webpack-config'; * * export default new Config().defaults(() => { * return { * debug: true * }; * }); * @description Adds `values` if they are missing * @param {...ConfigOptions} values * @returns {Config} */ defaults(...values) { return _ConfigCommandInvoker2.default.invoke(commandNames.DEFAULTS, this, ...values); } /** * @example * import Config from 'webpack-config'; * * export default new Config().merge({ * debug: true * }, { * profile: false * }); * @example * import Config from 'webpack-config'; * * export default new Config().merge(() => { * return { * debug: true * }; * }); * @description Merges `values` * @param {...ConfigOptions} values * @returns {Config} */ merge(...values) { return _ConfigCommandInvoker2.default.invoke(commandNames.MERGE, this, ...values); } /** * @example * import Config from 'webpack-config'; * * export default new Config().extend('./test/fixtures/webpack.1.config.js'); * @example * import Config from 'webpack-config'; * * // Loads from `node_modules/react-redux/webpack.config.js` * export default new Config().extend('react-redux/webpack.config.js'); * @example * import Config from 'webpack-config'; * * // Loads from `node_modules/webpack-config-my/webpack.config.js` * export default new Config().extend('my/webpack.config.js'); * @example * import Config from 'webpack-config'; * * export default new Config().extend({ * './test/fixtures/webpack.1.config.js': config => { * delete config.tags; * * return config; * } * }); * @example * import Config from 'webpack-config'; * * export default new Config().extend({ * './test/fixtures/webpack.1.config.js': [config => { * delete config.tags; * * return config; * }, config => { * delete config.profile; * * return config; * }] * }); * @description Helps to extend config using local file or shareable config file which should be hosted under `node_modules` * @param {...ConfigExtendPossibleOptions} values * @returns {Config} */ extend(...values) { return _ConfigCommandInvoker2.default.invoke(commandNames.EXTEND, this, ...values); } /** * @example * import Config from 'webpack-config'; * * const config = new Config(); * * config.merge({ * debug: true * }); * * console.log(config.clone()); * // Config { debug: true } * @description Creates copy of {@link Config} * @returns {Config} */ clone() { const config = new Config(this.commandFactory); config.dependencyTree = new _ConfigDependency2.default(config, this.dependencyTree.children); return config.merge(this.toObject()); } /** * @example * import Config from 'webpack-config'; * * const config = new Config(); * * config.merge({ * debug: true * }); * * console.log(config.toObject()); * // Object { debug: true } * @description Returns plain `Object` representation of {@link Config} * @returns {Object} */ toObject() { const properties = {}; var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = (0, _getIterator3.default)((0, _entries2.default)(this)), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { const _ref = _step.value; var _ref2 = (0, _slicedToArray3.default)(_ref, 2); const key = _ref2[0]; const value = _ref2[1]; if (this.has(key)) { properties[key] = value; } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } return properties; } /** * @example * import Config from 'webpack-config'; * * const config = new Config(); * * config.set('debug', true); * * console.log(config.toObject()); * // Object { debug: true } * @description Sets `value` at `path` * @param {String} path * @param {*} value * @return {Config} */ set(path, value) { (0, _lodash.set)(this, path, value); return this; } /** * @example * import Config from 'webpack-config'; * * const config = new Config(); * * config.set('debug', true); * * console.log(config.get('debug')); * // true * @description Gets `value` at `path` * @param {String} path * @return {*} */ get(path) { return (0, _lodash.get)(this, path); } /** * @example * import Config from 'webpack-config'; * * const config = new Config(); * * config.set('debug', true).remove('debug'); * * console.log(config.get('debug')); * // undefined * @description Removes `value` at `path` * @param {String} path * @return {Config} */ remove(path) { (0, _lodash.unset)(this, path); return this; } /** * @example * import Config from 'webpack-config'; * * const config = new Config(); * * config.set('debug', true); * * console.log(config.has('debug')); * // true * @description Checks if `value` exist at `path` * @param {String} path * @return {Boolean} */ has(path) { return (0, _lodash.has)(this, path); } /** * @returns {Object} */ toJSON() { return this.toObject(); } } exports.default = Config; //# sourceMappingURL=Config.js.map