1 | var _ = require('lodash');
|
2 | var generators = require('./generators');
|
3 | var getLoader = generators.getLoader;
|
4 | var getPlugin = generators.getPlugin;
|
5 | var getOutput = generators.getOutput;
|
6 | var getResolver = generators.getResolver;
|
7 | var getEntry = generators.getEntry;
|
8 | var getPostCSS = generators.getPostCSS;
|
9 | var getExternals = generators.getExternals;
|
10 |
|
11 |
|
12 | function makeBuild(shortBuild) {
|
13 | return {
|
14 | buildName: shortBuild.name,
|
15 | watch: shortBuild.watch,
|
16 | webpackConfig: parseWebpackConfig(shortBuild.webpack, shortBuild.name),
|
17 | };
|
18 | }
|
19 |
|
20 | function 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 |
|
63 | function concatInPlace(dest, source) {
|
64 | source.forEach(function(thing) { dest.push(thing) });
|
65 | }
|
66 |
|
67 | function loadNameOrUseSource(thing, load) {
|
68 | if (typeof thing === 'string') {
|
69 | return load(thing);
|
70 | }
|
71 |
|
72 | return thing;
|
73 | }
|
74 |
|
75 | function mapLoadNameOrUseSource(array, loader) {
|
76 | return array.map(loader);
|
77 | }
|
78 |
|
79 | module.exports = {
|
80 | makeBuild,
|
81 | parseWebpackConfig,
|
82 | loadNameOrUseSource,
|
83 | };
|