Skip to content

Commit 2e6aef2

Browse files
author
benholloway
committed
added filtering of compositions by name option, fix name not appearing in stats
1 parent d7964ba commit 2e6aef2

File tree

5 files changed

+78
-25
lines changed

5 files changed

+78
-25
lines changed

config/app.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@
33
/**
44
* Create a list of webpack configurators, one for each application detected.
55
* @param {function():Config} factory A factory for the webpack-configurator
6-
* @param {{appDir:string, buildDir:string, globals:object, unminified:boolean, port:number}} options An options hash
7-
* @returns {Array.<Config>} A list of ebpack-configurator instances, one for each application detected
6+
* @param {{appDir:string, buildDir:string, names:Array, globals:object, unminified:boolean, port:number}} options
7+
* @returns {Array.<Config>} A list of Webpack-configurator instances, one for each application detected
88
*/
99
function app(factory, options) {
1010

1111
// lazy import packages
1212
var path = require('path'),
13-
listCompositions = require('../lib/list-compositions');
13+
listCompositions = require('../lib/list-compositions'),
14+
appFilter = require('../lib/app-filter');
1415

1516
// there may be any number of compositions in subdirectories
16-
return listCompositions(options.appDir)
17-
.map(eachComposition);
17+
var list = listCompositions(options.appDir, 'app')
18+
.filter(appFilter(options.names));
19+
20+
// ensure at least one composition or webpack will crash with a cryptic error
21+
if (list.length) {
22+
return list.map(eachComposition);
23+
}
24+
else {
25+
throw new Error('There are no compositions included in this build.');
26+
}
1827

1928
function eachComposition(composition, i) {
2029
var buildDir = path.join(options.buildDir, composition.directory),
@@ -28,6 +37,7 @@ function app(factory, options) {
2837
})
2938
.addMinification(!options.unminified)
3039
.merge({
40+
name : composition.namespace.join('.'),
3141
output: {
3242
path: path.resolve(buildDir)
3343
}

config/release.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
'use strict';
22

33
/**
4-
* Create a single webpack configurator for release.
5-
* @param {Config} configurator A webpack-configurator instance
6-
* @param {{appDir:string, releaseDir:string, globals:object, unminified:boolean, port:number}} options An options hash
7-
* @returns {Config} The given webpack-configurator instance
4+
* Create a list of webpack configurators, one for each application detected.
5+
* @param {function():Config} factory A factory for the webpack-configurator
6+
* @param {{appDir:string, releaseDir:string, names:Array, globals:object, unminified:boolean, port:number}} options
7+
* @returns {Array.<Config>} A list of Webpack-configurator instances, one for each application detected
88
*/
9-
function release(configurator, options) {
9+
function release(factory, options) {
1010

1111
// lazy import packages
1212
var path = require('path'),
13-
listCompositions = require('../lib/list-compositions');
13+
listCompositions = require('../lib/list-compositions'),
14+
appFilter = require('../lib/app-filter');
1415

15-
// only the primary application will be released
16-
var composition = listCompositions(options.appDir)[0];
17-
if (composition) {
18-
return configurator
19-
.addBrowserSync(options.releaseDir, options.port)
20-
.addClean(options.releaseDir)
16+
// there may be any number of compositions in subdirectories
17+
var list = listCompositions(options.appDir, 'release')
18+
.filter(appFilter(options.names));
19+
20+
// ensure at least one composition or webpack will crash with a cryptic error
21+
if (list.length) {
22+
return list.map(eachComposition);
23+
}
24+
else {
25+
throw new Error('There are no compositions included in this build.');
26+
}
27+
28+
function eachComposition(composition) {
29+
var releaseDir = path.join(options.releaseDir, composition.directory);
30+
return factory()
31+
.addClean(releaseDir)
2132
.addComposition(composition, options.publicPath)
2233
.addConditionals({
2334
TEST : false,
@@ -27,16 +38,13 @@ function release(configurator, options) {
2738
.addExternalChunkManifest()
2839
.addMinification(!options.unminified)
2940
.merge({
30-
name : 'release',
41+
name : composition.namespace.join('.'),
3142
output: {
32-
path : path.resolve(options.releaseDir),
43+
path : path.resolve(releaseDir),
3344
publicPath: options.publicPath
3445
}
3546
});
3647
}
37-
else {
38-
throw new Error('there are no compositions in the app directory');
39-
}
4048
}
4149

4250
module.exports = release;

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const DEFAULT_OPTIONS = {
1212
testGlob : '**/*.spec.js',
1313
port : 55555,
1414
unminified: false,
15+
names : 'app*, release',
1516
publicPath: undefined,
1617
globals : {},
1718
stats : {
@@ -56,6 +57,6 @@ module.exports = multiConfigurator(DEFAULT_OPTIONS, configuratorFactory(OPERATOR
5657
.append(require('./config/test'))
5758
.append('common')
5859
.define('release')
59-
.append(require('./config/release'))
60+
.generate(require('./config/release'))
6061
.append('common')
6162
.create;

lib/app-filter.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
/**
4+
* Create a filter for apps given a comma separated list.
5+
* @param {string} names A comma separated list of names to include
6+
* @returns {function} A composition filter function
7+
*/
8+
function appFilter(names) {
9+
var list = Array.isArray(names) ? names.map(trim) : String(names).split(',').map(trim);
10+
11+
return function filter(composition) {
12+
var actual = composition.namespace.join('.'),
13+
result = list.some(testName);
14+
console.log(result, list, composition);
15+
return result;
16+
17+
function testName(name) {
18+
if (name.slice(-1) === '*') {
19+
var short = name.slice(0, -1);
20+
return (actual.slice(0, short.length) === short);
21+
}
22+
else {
23+
return (actual === name);
24+
}
25+
}
26+
};
27+
28+
function trim(value) {
29+
return String(value).trim();
30+
}
31+
}
32+
33+
module.exports = appFilter;

lib/list-compositions.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ var glob = require('glob');
77
/**
88
* Detect all compositions in the given base directory.
99
* @param {string} appDir The directory to search for composition roots
10+
* @param {string} [namespacePrefix] Optional prefix for the composition namespace
1011
* @returns {Array.<object>} A list of composition items
1112
*/
12-
function compositions(appDir) {
13+
function compositions(appDir, namespacePrefix) {
1314
var files = glob.sync(appDir + '/**/index.html') || [];
1415
return files
1516
.map(fileToItem)
@@ -26,7 +27,7 @@ function compositions(appDir) {
2627

2728
return {
2829
directory : directory,
29-
namespace : namespace,
30+
namespace : [].concat(namespacePrefix || []).concat(namespace),
3031
htmlFiles : file,
3132
indexFiles: glob.sync(posix + '/index.{js,css,scss}')
3233
};

0 commit comments

Comments
 (0)