Skip to content

Commit 131304e

Browse files
author
benholloway
committed
improved usability with resolve() method, added names to config, bump minor version
1 parent 793dde2 commit 131304e

File tree

5 files changed

+94
-67
lines changed

5 files changed

+94
-67
lines changed

config/app.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ var path = require('path');
55
var createConfigurator = require('../lib/create-configurator'),
66
listCompositions = require('../lib/list-compositions');
77

8-
// TODO doctag
8+
/**
9+
* Create a list of webpack configurators, one for each application detected.
10+
* @param {{appDir:string, buildDir:string, globals:object, unminified:boolean, port:number}} options An options hash
11+
* @returns {Array.<Config>} A list of configurators, one for each application detected
12+
*/
913
function app(options) {
1014

1115
// there may be any number of compositions in subdirectories
@@ -32,6 +36,7 @@ function app(options) {
3236
})
3337
.addMinification(!options.unminified)
3438
.merge({
39+
name : ['app', composition.namespace].join('::'),
3540
output: {
3641
path: path.resolve(buildDir)
3742
}

config/release.js

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,41 @@ var path = require('path');
55
var createConfigurator = require('../lib/create-configurator'),
66
listCompositions = require('../lib/list-compositions');
77

8-
// TODO doctag
8+
/**
9+
* Create a single webpack configurator for release.
10+
* @param {{appDir:string, releaseDir:string, globals:object, unminified:boolean, port:number}} options An options hash
11+
* @returns {Config} A webpack configurator
12+
*/
913
function release(options) {
1014
var composition = listCompositions(options.appDir)[0];
1115

12-
return [
13-
createConfigurator({
14-
addBrowserSync : require('./add/browser-sync'),
15-
addClean : require('./add/clean'),
16-
addCommon : require('./add/common'),
17-
addComposition : require('./add/composition'),
18-
addConditionals : require('./add/conditionals'),
19-
addExternalChunkManifest: require('./add/external-chunk-manifest'),
20-
addMinification : require('./add/minification')
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+
})
25+
.addBrowserSync(options.releaseDir, options.port)
26+
.addClean(options.releaseDir)
27+
.addComposition(composition)
28+
.addCommon(path.resolve(__dirname, '..', 'node_modules'), options.globals)
29+
.addConditionals({
30+
TEST : false,
31+
DEBUG : false,
32+
RELEASE: true
2133
})
22-
.addBrowserSync(options.releaseDir, options.port)
23-
.addClean(options.releaseDir)
24-
.addComposition(composition)
25-
.addCommon(path.resolve(__dirname, '..', 'node_modules'), options.globals)
26-
.addConditionals({
27-
TEST : false,
28-
DEBUG : false,
29-
RELEASE: true
30-
})
31-
.addExternalChunkManifest()
32-
.addMinification(!options.unminified)
33-
.merge({
34-
output: {
35-
path : path.resolve(options.releaseDir),
36-
publicPath: options.publicPath
37-
}
38-
})
39-
];
34+
.addExternalChunkManifest()
35+
.addMinification(!options.unminified)
36+
.merge({
37+
name : 'release',
38+
output: {
39+
path : path.resolve(options.releaseDir),
40+
publicPath: options.publicPath
41+
}
42+
});
4043
}
4144

4245
module.exports = release;

config/test.js

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,35 @@ var path = require('path');
44

55
var createConfigurator = require('../lib/create-configurator');
66

7-
// TODO doctag
7+
/**
8+
* Create a single webpack configurator for test.
9+
* @param {{appDir:string, testDir:string, globals:object}} options An options hash
10+
* @returns {Config} A webpack configurator
11+
*/
812
function test(options) {
913
var testEntry = path.resolve(options.appDir, 'test.js');
1014

11-
return [
12-
createConfigurator({
13-
addCommon : require('./add/common'),
14-
addConditionals : require('./add/conditionals'),
15-
addTestSuiteGeneration: require('./add/test-suite-generation')
15+
return createConfigurator({
16+
addCommon : require('./add/common'),
17+
addConditionals : require('./add/conditionals'),
18+
addTestSuiteGeneration: require('./add/test-suite-generation')
19+
})
20+
.addCommon(path.resolve(__dirname, '..', 'node_modules'), options.globals)
21+
.addConditionals({
22+
TEST : true,
23+
DEBUG : true,
24+
RELEASE: false
1625
})
17-
.addCommon(path.resolve(__dirname, '..', 'node_modules'), options.globals)
18-
.addConditionals({
19-
TEST : true,
20-
DEBUG : true,
21-
RELEASE: false
22-
})
23-
.addTestSuiteGeneration(testEntry, '**/*.spec.js')
24-
.merge({
25-
entry : {
26-
test: testEntry
27-
},
28-
output: {
29-
path: path.resolve(options.testDir)
30-
}
31-
})
32-
];
26+
.addTestSuiteGeneration(testEntry, '**/*.spec.js')
27+
.merge({
28+
name : 'test',
29+
entry : {
30+
test: testEntry
31+
},
32+
output: {
33+
path: path.resolve(options.testDir)
34+
}
35+
});
3336
}
3437

3538
module.exports = test;

index.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ var defaultOptions = require('./lib/default-options'),
99
parseOptions = require('./lib/parse-options');
1010

1111
/**
12-
* Create a webpack configuration
12+
* Create a set of methods that yield webpack configurator(s).
1313
* @param {...object} [options] Any number of options hashes to be merged
14-
* @returns {object} Webpack configuration
14+
* @returns {{app:function, test:function, release:function, resolve:function}} A new instance
1515
*/
1616
function configFactory(options) {
1717

@@ -23,22 +23,38 @@ function configFactory(options) {
2323
// options set
2424
var args = Array.prototype.slice.call(arguments),
2525
opt = parseOptions(
26-
defaults.apply(null, [{}].concat(args)), // merged options in
27-
defaults({port: angularityPort}, defaultOptions()) // merged defaults
26+
defaults.apply(null, [{}].concat(args)), // merged options in
27+
defaults({port: angularityPort}, defaultOptions()) // merged defaults
2828
);
2929

30-
// api
31-
return {
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-
}
30+
// create and return the instance
31+
var instance = {
32+
app : require('./config/app')(opt),
33+
test : require('./config/test')(opt),
34+
release: require('./config/release')(opt),
35+
resolve: resolve
4136
};
37+
return instance;
38+
39+
/**
40+
* Call the given function with the instance (as this) and resolve() any webpack configurators that it returns.
41+
* @param {function(instance:object}:Array.<Config>|Config} fn A method to call with the instance as this
42+
* @returns {Array.<object>|object} A webpack configuration or Array thereof
43+
*/
44+
function resolve(fn) {
45+
if (typeof fn !== 'function') {
46+
throw new Error('The argument given to resolve() must be a function');
47+
}
48+
else {
49+
return [].concat(fn.call(instance))
50+
.filter(Boolean)
51+
.map(resolveElement);
52+
}
53+
54+
function resolveElement(configurator) {
55+
return configurator.resolve();
56+
}
57+
}
4258
}
4359

4460
module.exports = configFactory;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-angularity-solution",
3-
"version": "0.6.0",
3+
"version": "0.7.0",
44
"description": "Requisite configuration and modules to build Angularity projects with Webpack",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)