-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
83 lines (68 loc) · 2.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var path = require('path');
var argv = require('yargs').argv;
var webpack = require('webpack');
var extendify = require('extendify');
var colog = require('colog');
var util = require('util');
var _ = require('lodash');
var defaultOptions = {
profiles: '',
profilesFilename: 'profiles.js',
returnProfiles: false,
extend: {
isDeep: true,
arrays: 'concat'
}
};
module.exports = function (webpackConfig, options) {
_.merge(defaultOptions, options);
var cmdOptions = {
profiles: argv.profiles,
profilesFilename: argv.profilesFilename
};
_.merge(defaultOptions, cmdOptions);
var profiles = require(path.join(path.dirname(module.parent.filename), defaultOptions.profilesFilename));
var profilesNames = [];
// if profiles are not undefined split them, else check if default profile exists
if (defaultOptions.profiles) {
profilesNames = defaultOptions.profiles.split(/[\s,]+/);
} else {
Object.keys(profiles).forEach(function (profileName) {
if (profiles[profileName].activeByDefault) {
profilesNames.push(profileName);
}
});
if(profilesNames.length) {
colog.info(util.format('No profiles specified, using default %s', profilesNames.join(', ')));
}
}
profilesNames.forEach(function (profileName) {
var profile = null;
if (profileName in profiles) {
profile = profiles[profileName];
} else {
throw util.format('Profile %s not found, aborting.', profile);
}
colog.info(util.format('Applying profile %s', profileName)) ;
if (profile.vars) {
var define = new webpack.DefinePlugin(profile.vars);
if (webpackConfig.plugins) {
webpackConfig.plugins.unshift(define);
} else {
webpackConfig.plugins = [
define
];
}
}
extendify(
_.assign(defaultOptions.extend, {inPlace: true})
)(webpackConfig, profile.config);
});
if(defaultOptions.returnProfiles) {
return {
config: webpackConfig,
profiles: _.pick(profiles, profilesNames)
};
}
return webpackConfig;
};