UNPKG

1.92 kBJavaScriptView Raw
1var _ = require('lodash');
2var generators = require('./generators');
3var getLoader = generators.getLoader;
4var getPlugin = generators.getPlugin;
5var getOutput = generators.getOutput;
6var getResolver = generators.getResolver;
7var getEntry = generators.getEntry;
8var getPostCSS = generators.getPostCSS;
9var getExternals = generators.getExternals;
10
11
12function makeBuild(shortBuild) {
13 return {
14 buildName: shortBuild.name,
15 watch: shortBuild.watch,
16 webpackConfig: parseWebpackConfig(shortBuild.webpack, shortBuild.name),
17 };
18}
19
20function parseWebpackConfig(shortHandConfig, buildName) {
21 var webpackConfig = {};
22
23 Object.keys(shortHandConfig).forEach(function(key) {
24 var obj = shortHandConfig[key];
25
26 if (key === 'loaders') {
27 _.defaults(webpackConfig, { module: { loaders: [] }});
28 concatInPlace(
29 webpackConfig.module.loaders,
30 obj.map(getLoader)
31 );
32 }
33
34 if (key === 'plugins') {
35 webpackConfig.plugins = obj.map(getPlugin);
36 }
37
38 if (key === 'output') {
39 webpackConfig.output = getOutput(obj);
40 }
41
42 if (key === 'resolve') {
43 webpackConfig.resolve = getResolver(obj);
44 }
45
46 if (key === 'entry') {
47 webpackConfig.entry = getEntry(obj, buildName);
48 }
49
50 if (key === 'postcss') {
51 webpackConfig.postcss = obj.map(getPostCSS);
52 }
53
54 if (key === 'externals') {
55 webpackConfig.externals = getExternals(obj);
56 }
57 });
58
59 return _.extend(webpackConfig, _.omit(shortHandConfig,
60 Object.keys(webpackConfig).concat('loaders')));
61}
62
63function concatInPlace(dest, source) {
64 source.forEach(function(thing) { dest.push(thing) });
65}
66
67function loadNameOrUseSource(thing, load) {
68 if (typeof thing === 'string') {
69 return load(thing);
70 }
71
72 return thing;
73}
74
75function mapLoadNameOrUseSource(array, loader) {
76 return array.map(loader);
77}
78
79module.exports = {
80 makeBuild,
81 parseWebpackConfig,
82 loadNameOrUseSource,
83};