Skip to content

Commit 7051a0c

Browse files
author
benholloway
committed
add extend() for operator extensability, improve parsing of deep options from environment variables, add stats related options, bump minor version
1 parent 84aab1e commit 7051a0c

File tree

13 files changed

+240
-110
lines changed

13 files changed

+240
-110
lines changed

config/add/browser-sync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var glob = require('glob'),
66
BrowserSyncPlugin = require('browser-sync-webpack-plugin');
77

88
/**
9-
* Add browser-sync feature.
9+
* Add browser-sync server for Webpack `--watch`.
1010
* @this {Config} A webpack-configurator instance
1111
* @param {string} directory Base directory for the server (should point to your compiled application)
1212
* @param {number} port Server port

config/add/common.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ var webpack = require('webpack'),
99
OmitTildePlugin = require('omit-tilde-webpack-plugin');
1010

1111
/**
12-
* Add common features.
12+
* Add configuration common to all modes.
1313
* @this {Config} A webpack-configurator instance
1414
* @param {string} loaderRoot The base path in which to locate loaders
15-
* @param {{appDir:string, globals:object} globals A hash of options
15+
* @param {{appDir:string, globals:object, stats:string}} options A hash of options
1616
* @returns {Config} The given webpack-configurator instance
1717
*/
1818
function common(loaderRoot, options) {
@@ -46,7 +46,8 @@ function common(loaderRoot, options) {
4646
},
4747
node : {
4848
fs: 'empty'
49-
}
49+
},
50+
stats : options.stats
5051
})
5152

5253
// before compile

config/add/composition.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ var IndexHTMLPlugin = require('indexhtml-webpack-plugin'),
66
GulpInjectPlugin = require('gulp-inject-webpack-plugin');
77

88
/**
9-
* Detect all compositions in the given base directory.
9+
* Add an application for compilation.
1010
* @this {Config} A webpack-configurator instance
11-
* @param {{directory:string, htmlFiles:Array, indexFiles:Array}} item A composition item
11+
* @param {{namespace:Array.<string>, directory:string, htmlFiles:Array, indexFiles:Array}} item A composition item
1212
* @returns {Config} The given webpack-configurator instance
1313
*/
1414
function composition(item) {
1515
/* jshint validthis:true */
1616
return this
1717
.merge({
18+
name : ['app'].concat(composition.namespace).join(':'),
1819
entry: {
1920
'index-html': item.htmlFiles,
2021
index : item.indexFiles

config/app.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,23 @@
22

33
var path = require('path');
44

5-
var createConfigurator = require('../lib/create-configurator'),
6-
listCompositions = require('../lib/list-compositions');
5+
var listCompositions = require('../lib/list-compositions');
76

87
/**
98
* Create a list of webpack configurators, one for each application detected.
9+
* @param {function} configuratorFactory A factory for the webpack-configurator
1010
* @param {{appDir:string, buildDir:string, globals:object, unminified:boolean, port:number}} options An options hash
1111
* @returns {Array.<Config>} A list of configurators, one for each application detected
1212
*/
13-
function app(options) {
13+
function app(configuratorFactory, options) {
1414

1515
// there may be any number of compositions in subdirectories
1616
return listCompositions(options.appDir)
1717
.map(eachComposition);
1818

1919
function eachComposition(composition, i) {
2020
var buildDir = path.join(options.buildDir, composition.directory),
21-
config = createConfigurator({
22-
addBrowserSync : require('./add/browser-sync'),
23-
addClean : require('./add/clean'),
24-
addCommon : require('./add/common'),
25-
addComposition : require('./add/composition'),
26-
addConditionals: require('./add/conditionals'),
27-
addMinification: require('./add/minification')
28-
})
21+
config = configuratorFactory()
2922
.addClean(buildDir)
3023
.addCommon(path.resolve(__dirname, '..', 'node_modules'), options)
3124
.addComposition(composition)
@@ -36,7 +29,6 @@ function app(options) {
3629
})
3730
.addMinification(!options.unminified)
3831
.merge({
39-
name : ['app'].concat(composition.namespace).join('::'),
4032
output: {
4133
path: path.resolve(buildDir)
4234
}

config/release.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,18 @@
22

33
var path = require('path');
44

5-
var createConfigurator = require('../lib/create-configurator'),
6-
listCompositions = require('../lib/list-compositions');
5+
var listCompositions = require('../lib/list-compositions');
76

87
/**
98
* Create a single webpack configurator for release.
9+
* @param {function} configuratorFactory A factory for the webpack-configurator
1010
* @param {{appDir:string, releaseDir:string, globals:object, unminified:boolean, port:number}} options An options hash
1111
* @returns {Config} A webpack configurator
1212
*/
13-
function release(options) {
13+
function release(configuratorFactory, options) {
1414
var composition = listCompositions(options.appDir)[0];
1515
if (composition) {
16-
return createConfigurator({
17-
addBrowserSync : require('./add/browser-sync'),
18-
addClean : require('./add/clean'),
19-
addCommon : require('./add/common'),
20-
addComposition : require('./add/composition'),
21-
addConditionals : require('./add/conditionals'),
22-
addExternalChunkManifest: require('./add/external-chunk-manifest'),
23-
addMinification : require('./add/minification')
24-
})
16+
return configuratorFactory()
2517
.addBrowserSync(options.releaseDir, options.port)
2618
.addClean(options.releaseDir)
2719
.addComposition(composition)

config/test.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@
22

33
var path = require('path');
44

5-
var createConfigurator = require('../lib/create-configurator');
6-
75
/**
86
* Create a single webpack configurator for test.
7+
* @param {function} configuratorFactory A factory for the webpack-configurator
98
* @param {{appDir:string, testDir:string, globals:object, testGlob:string}} options An options hash
109
* @returns {Config} A webpack configurator
1110
*/
12-
function test(options) {
11+
function test(configuratorFactory, options) {
1312
var testEntry = path.resolve(options.appDir, 'test.js');
1413

15-
return createConfigurator({
16-
addClean : require('./add/clean'),
17-
addCommon : require('./add/common'),
18-
addConditionals : require('./add/conditionals'),
19-
addTestSuiteGeneration: require('./add/test-suite-generation')
20-
})
14+
return configuratorFactory()
2115
.addClean(options.testDir)
2216
.addCommon(path.resolve(__dirname, '..', 'node_modules'), options)
2317
.addConditionals({

index.js

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,27 @@ var path = require('path'),
55

66
var defaults = require('lodash.defaults');
77

8-
var defaultOptions = require('./lib/default-options'),
9-
parseOptions = require('./lib/parse-options');
8+
var defaultOptions = require('./lib/default-options'),
9+
parseOptions = require('./lib/parse-options'),
10+
getConfiguratorFactory = require('./lib/get-configurator-factory');
11+
12+
var DEFAULT_OPERATORS = {
13+
addBrowserSync : require('./config/add/browser-sync'),
14+
addClean : require('./config/add/clean'),
15+
addCommon : require('./config/add/common'),
16+
addComposition : require('./config/add/composition'),
17+
addConditionals : require('./config/add/conditionals'),
18+
addExternalChunkManifest: require('./config/add/external-chunk-manifest'),
19+
addMinification : require('./config/add/minification'),
20+
addTestSuiteGeneration : require('./config/add/test-suite-generation')
21+
};
1022

1123
/**
1224
* Create a set of accessors that yield webpack configurator(s).
1325
* @param {...object} [options] Any number of options hashes to be merged
1426
* @returns {{app:function, test:function, release:function, resolve:function}} A new instance
1527
*/
16-
function configFactory(options) {
28+
function create(options) {
1729

1830
// legacy support
1931
// where angularity.json is present it should define the port
@@ -27,40 +39,55 @@ function configFactory(options) {
2739
defaults({port: angularityPort}, defaultOptions()) // merged defaults
2840
);
2941

30-
// create and return the instance
31-
var instance = {
32-
get app() {
33-
return require('./config/app')(opt);
34-
},
35-
get test() {
36-
return require('./config/test')(opt);
37-
},
38-
get release() {
39-
return require('./config/release')(opt);
40-
},
41-
resolve: resolve
42-
};
43-
return instance;
42+
// default is the default operator set
43+
return extend(DEFAULT_OPERATORS);
4444

4545
/**
46-
* Call the given function with the instance (as this) and resolve() any webpack configurators that it returns.
47-
* @param {function(instance:object}:Array.<Config>|Config} fn A method to call with the instance as this
48-
* @returns {Array.<object>|object} A webpack configuration or Array thereof
46+
* Extend the configurator with the given operators.
47+
* @param {object} oldOperators A hash of the existing operators from the parent instance
48+
* @param {object} newOperators A hash of operator overrides
49+
* @returns {{extend:function, resolve:function, app:Array.<Configurator>, test:Configurator, release:Configurator}}
4950
*/
50-
function resolve(fn) {
51-
if (typeof fn !== 'function') {
52-
throw new Error('The argument given to resolve() must be a function');
53-
}
54-
else {
55-
return [].concat(fn.call(instance))
56-
.filter(Boolean)
57-
.map(resolveElement);
58-
}
51+
function extend(oldOperators, newOperators) {
52+
var operators = defaults({}, newOperators, oldOperators),
53+
configuratorFactory = getConfiguratorFactory(operators);
54+
55+
// create and return the instance
56+
var instance = {
57+
extend : extend.bind(null, operators),
58+
resolve: resolve,
59+
get app() {
60+
return require('./config/app')(configuratorFactory, opt);
61+
},
62+
get test() {
63+
return require('./config/test')(configuratorFactory, opt);
64+
},
65+
get release() {
66+
return require('./config/release')(configuratorFactory, opt);
67+
}
68+
};
69+
return instance;
70+
71+
/**
72+
* Call the given function with the instance (as this) and resolve() any webpack configurators that it returns.
73+
* @param {function(instance:object):Config|Array.<Config>} fn A method to call with the instance as this
74+
* @returns {Array.<object>|object} A webpack configuration or Array thereof
75+
*/
76+
function resolve(fn) {
77+
if (typeof fn !== 'function') {
78+
throw new Error('The argument given to resolve() must be a function');
79+
}
80+
else {
81+
return [].concat(fn.call(instance))
82+
.filter(Boolean)
83+
.map(resolveElement);
84+
}
5985

60-
function resolveElement(configurator) {
61-
return configurator.resolve();
86+
function resolveElement(configurator) {
87+
return configurator.resolve();
88+
}
6289
}
6390
}
6491
}
6592

66-
module.exports = configFactory;
93+
module.exports = create;

lib/create-configurator.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

lib/default-options.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,22 @@ function defaultOptions() {
1010
port : 55555,
1111
unminified: false,
1212
publicPath: undefined,
13-
globals : {}
13+
globals : {},
14+
stats : {
15+
hash : true,
16+
version : true,
17+
timings : true,
18+
assets : true,
19+
chunks : true,
20+
modules : true,
21+
reasons : true,
22+
children : true,
23+
source : true,
24+
errors : true,
25+
errorDetails: true,
26+
warnings : true,
27+
publicPath : true
28+
}
1429
};
1530
}
1631

lib/get-configurator-factory.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
var Config = require('webpack-configurator');
4+
5+
/**
6+
* Create a factory for webpack-configurator with additional methods as given.
7+
* @param {object} operators A hash of methods to add to webpack-configurator
8+
* @returns {Config} A webpack-configurator instance with the additional operators
9+
*/
10+
function getConfiguratorFactory(operators) {
11+
return function configuratorFactory() {
12+
var instance = new Config();
13+
for (var key in operators) {
14+
if (typeof operators[key] === 'function') {
15+
instance[key] = operators[key].bind(instance);
16+
} else {
17+
throw new Error('The operator named "' + key + '" must be function.')
18+
}
19+
}
20+
return instance;
21+
}
22+
}
23+
24+
module.exports = getConfiguratorFactory;

0 commit comments

Comments
 (0)