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

Commit

Permalink
develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mdreizin authored Apr 28, 2018
1 parent 50ddd26 commit 28b1a2e
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 64 deletions.
7 changes: 1 addition & 6 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@
"presets": [
["env", {
"targets": {
"node": 6
"node": true
}
}]
],
"plugins": [
["babel-plugin-transform-builtin-extend", {
"globals": [
"Array"
]
}],
"transform-runtime"
],
"only": [
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
fast_finish: true
include:
- stage: release
node_js: 9
node_js: 8
deploy:
skip_cleanup: true
provider: script
Expand Down
24 changes: 16 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,38 @@
"name": "webpack-config",
"description": "Helps to load, extend and merge webpack configs",
"main": "dist/index.js",
"files": ["dist/", "src/"],
"files": [
"dist/",
"src/"
],
"scripts": {
"clean": "rm -rf dist coverage docs",
"lint": "eslint --ext js,md ./ --cache",
"build": "babel src --out-dir dist --source-maps",
"test": "babel-node jasmine.js",
"cover":
"NODE_ENV=test nyc --reporter=lcov jasmine JASMINE_CONFIG_PATH=jasmine.json",
"cover": "NODE_ENV=test nyc --reporter=lcov jasmine JASMINE_CONFIG_PATH=jasmine.json",
"postcover": "nyc report",
"codeclimate": "codeclimate-test-reporter < ./coverage/lcov.info",
"jsdoc": "jsdoc ./src -c ./jsdoc.json"
},
"nyc": {
"include": ["src/*.js"],
"require": ["babel-register"],
"include": [
"src/*.js"
],
"require": [
"babel-register"
],
"sourceMap": false,
"instrument": false
},
"repository": {
"type": "git",
"url": "https://github.com/Fitbit/webpack-config.git"
},
"keywords": ["webpack", "webpack-config"],
"keywords": [
"webpack",
"webpack-config"
],
"author": "Marat Dreizin <[email protected]>",
"license": "Apache-2.0",
"bugs": {
Expand All @@ -35,7 +44,6 @@
"babel-cli": "^6.26.0",
"babel-eslint": "^8.2.3",
"babel-plugin-istanbul": "^4.1.6",
"babel-plugin-transform-builtin-extend": "^1.1.2",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1",
"babel-register": "^6.26.0",
Expand All @@ -57,6 +65,6 @@
"yargs-parser": "^10.0.0"
},
"engines": {
"node": ">=6.0.0"
"node": ">=8.0.0"
}
}
40 changes: 31 additions & 9 deletions src/ConfigCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import {
import ConfigStrategyList from './ConfigStrategyList';
import DEFAULT_RESOLVERS from './ConfigCacheResolvers';

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

/**
* @private
* @type {String}
Expand All @@ -25,19 +31,17 @@ const VALUE_RESOLVERS = new WeakMap();
/**
* Please set `WEBPACK_CONFIG_CACHE` environment variable to `false` to make it non persistent or just use {@link ConfigCache#persistent}
* @class
* @extends {Map}
*/
class ConfigCache extends Map {
class ConfigCache {
/**
* @constructor
* @param {ConfigEnvironment} environment
* @param {Function[]} [valueResolvers]
*/
constructor(environment, valueResolvers = DEFAULT_RESOLVERS) {
super();

CACHE.set(this, new Map());
ENVIRONMENT.set(this, environment);
VALUE_RESOLVERS.set(this, ConfigStrategyList.from(valueResolvers));
VALUE_RESOLVERS.set(this, new ConfigStrategyList(valueResolvers));
}

/**
Expand All @@ -55,6 +59,15 @@ class ConfigCache extends Map {
return this.environment.getOrDefault(PERSISTENT_KEY, true) === true;
}

/**
* @private
* @readonly
* @type {Map}
*/
get cache() {
return CACHE.get(this);
}

/**
* @example
* import {
Expand All @@ -79,18 +92,19 @@ class ConfigCache extends Map {
}

/**
* @override
* @param {String} key
* @returns {*}
*/
get(key) {
let value;

if (this.persistent) {
if (!this.has(key)) {
if (!this.cache.has(key)) {
value = require(key);

this.set(key, value);
this.cache.set(key, value);
} else {
value = super.get(key);
value = this.cache.get(key);
}
} else {
delete require.cache[key];
Expand All @@ -100,6 +114,14 @@ class ConfigCache extends Map {

return this.valueResolvers.resolve(value, x => !isUndefined(x));
}
/**
* @param {String} key
* @param {*} value
* @returns {void}
*/
set(key, value) {
return this.cache.set(key, value);
}
}

export default ConfigCache;
5 changes: 2 additions & 3 deletions src/ConfigFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
isObject
} from 'lodash';
import Config from './Config';
import ConfigList from './ConfigList';
import { env } from './ConfigArgv';

/**
Expand Down Expand Up @@ -51,7 +50,7 @@ class ConfigFactory {

/**
* @param {Function|Object|Object[]} value
* @returns {Config|ConfigList}
* @returns {Config|Config[]}
*/
createConfig(value) {
let config;
Expand All @@ -61,7 +60,7 @@ class ConfigFactory {
}

if (Array.isArray(value)) {
config = ConfigList.from(value, x => this.initWith(x));
config = Array.from(value, x => this.initWith(x));
} else if (isObject(value)) {
config = this.initWith(value);
}
Expand Down
7 changes: 0 additions & 7 deletions src/ConfigList.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/ConfigLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ConfigLoader {

/**
* @param {String} filename
* @returns {Config|ConfigList}
* @returns {Config|Config[]}
*/
loadConfig(filename) {
filename = this.pathResolver.resolve(filename);
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigPathResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ConfigPathResolver {
*/
constructor(stringResolver, pathResolvers = DEFAULT_RESOLVERS) {
STRING_RESOLVER.set(this, stringResolver);
PATH_RESOLVERS.set(this, ConfigStrategyList.from(pathResolvers));
PATH_RESOLVERS.set(this, new ConfigStrategyList(pathResolvers));
}

/**
Expand Down
27 changes: 24 additions & 3 deletions src/ConfigStrategyList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,39 @@ import {
isError
} from 'lodash';

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

/**
* @class
* @extends {Array}
*/
class ConfigStrategyList extends Array {
class ConfigStrategyList {
/**
* @constructor
* @param {Function[]} resolvers
*/
constructor(resolvers) {
RESOLVERS.set(this, resolvers);
}

/**
* @readonly
* @type {Function[]}
*/
get resolvers() {
return RESOLVERS.get(this);
}

/**
* @param {*} value
* @param {Function} predicate
* @returns {*}
*/
resolve(value, predicate) {
for (const resolver of this) {
for (const resolver of this.resolvers) {
try {
const resolvedValue = resolver(value),
throwsError = isError(resolvedValue) || resolvedValue instanceof Error;
Expand Down
3 changes: 1 addition & 2 deletions test/ConfigFactory.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Config from '../src/Config';
import ConfigList from '../src/ConfigList';
import ConfigFactory from '../src/ConfigFactory';
import MockConfigContainer from './MockConfigContainer';

Expand Down Expand Up @@ -42,7 +41,7 @@ describe('ConfigFactory', () => {
foo: 'foo1'
}]);

expect(configs).toEqual(jasmine.any(ConfigList));
expect(configs).toEqual(jasmine.any(Array));
expect(configs.length).toEqual(1);
expect(configs[0]).toEqual(jasmine.any(Config));
expect(configs[0].toObject()).toEqual({
Expand Down
14 changes: 0 additions & 14 deletions test/ConfigList.spec.js

This file was deleted.

11 changes: 2 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -647,13 +647,6 @@ babel-plugin-transform-async-to-generator@^6.22.0:
babel-plugin-syntax-async-functions "^6.8.0"
babel-runtime "^6.22.0"

babel-plugin-transform-builtin-extend@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-builtin-extend/-/babel-plugin-transform-builtin-extend-1.1.2.tgz#5e96fecf58b8fa1ed74efcad88475b2af3c9116e"
dependencies:
babel-runtime "^6.2.0"
babel-template "^6.3.0"

babel-plugin-transform-es2015-arrow-functions@^6.22.0:
version "6.22.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-arrow-functions/-/babel-plugin-transform-es2015-arrow-functions-6.22.0.tgz#452692cb711d5f79dc7f85e440ce41b9f244d221"
Expand Down Expand Up @@ -928,7 +921,7 @@ babel-register@^6.26.0:
mkdirp "^0.5.1"
source-map-support "^0.4.15"

babel-runtime@^6.18.0, babel-runtime@^6.2.0, babel-runtime@^6.22.0:
babel-runtime@^6.18.0, babel-runtime@^6.22.0:
version "6.22.0"
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.22.0.tgz#1cf8b4ac67c77a4ddb0db2ae1f74de52ac4ca611"
dependencies:
Expand All @@ -942,7 +935,7 @@ babel-runtime@^6.26.0:
core-js "^2.4.0"
regenerator-runtime "^0.11.0"

babel-template@^6.16.0, babel-template@^6.22.0, babel-template@^6.3.0:
babel-template@^6.16.0, babel-template@^6.22.0:
version "6.22.0"
resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.22.0.tgz#403d110905a4626b317a2a1fcb8f3b73204b2edb"
dependencies:
Expand Down

0 comments on commit 28b1a2e

Please sign in to comment.