|
| 1 | +// node modules |
| 2 | +const { merge } = require('webpack-merge'); |
| 3 | + |
| 4 | +/** |
| 5 | + * return a webpack settings file |
| 6 | + * @param name string |
| 7 | + * @returns {{}} |
| 8 | + */ |
| 9 | +const getWebpackSettings = (name) => { |
| 10 | + let settings = {}; |
| 11 | + try { |
| 12 | + settings = require('./webpack-settings/' + name + '.settings.js'); |
| 13 | + } catch (e) { |
| 14 | + // that's okay |
| 15 | + } |
| 16 | + |
| 17 | + return settings; |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * return a webpack settings file combined with the 'app' settings |
| 22 | + * @param name string |
| 23 | + * @returns {{}} |
| 24 | + */ |
| 25 | +const getCombinedWebpackSettings = (name) => ({ |
| 26 | + ...getWebpackSettings('app'), |
| 27 | + ...getWebpackSettings(name), |
| 28 | +}); |
| 29 | + |
| 30 | +/** |
| 31 | + * return a legacy webpack config file |
| 32 | + * @param name |
| 33 | + * @returns {{}} |
| 34 | + */ |
| 35 | +const getLegacyWebpackConfig = (name) => require('./webpack-configs/' + name + '.config.js')('legacy', getCombinedWebpackSettings(name)); |
| 36 | + |
| 37 | +/** |
| 38 | + * return a modern webpack config file |
| 39 | + * @param name |
| 40 | + * @returns {{}} |
| 41 | + */ |
| 42 | +const getModernWebpackConfig = (name) => require('./webpack-configs/' + name + '.config.js')('modern', getCombinedWebpackSettings(name)); |
| 43 | + |
| 44 | +/** |
| 45 | + * return an array of webpack configs using the function provided in getWebpackConfig |
| 46 | + * @param names string[] |
| 47 | + * @param getWebpackConfig function |
| 48 | + * @returns {{}} |
| 49 | + */ |
| 50 | +const webpackConfigs = (names, getWebpackConfig) => { |
| 51 | + let config = {}; |
| 52 | + names.forEach((name) => config = merge(config, getWebpackConfig(name))); |
| 53 | + |
| 54 | + return config; |
| 55 | +}; |
| 56 | + |
| 57 | +/** |
| 58 | + * return an array of build webpack configs |
| 59 | + * @param names string |
| 60 | + * @returns {{}} |
| 61 | + */ |
| 62 | +const buildWebpackConfigs = (...names) => webpackConfigs(names, getModernWebpackConfig); |
| 63 | + |
| 64 | +/** |
| 65 | + * return an array of legacy webpack configs |
| 66 | + * @param names string |
| 67 | + * @returns {{}} |
| 68 | + */ |
| 69 | +const legacyWebpackConfigs = (...names) => webpackConfigs(names, getLegacyWebpackConfig); |
| 70 | + |
| 71 | +/** |
| 72 | + * return an array of modern webpack configs |
| 73 | + * @param names string |
| 74 | + * @returns {{}} |
| 75 | + */ |
| 76 | +const modernWebpackConfigs = (...names) => webpackConfigs(names, getModernWebpackConfig); |
| 77 | + |
| 78 | +// module exports |
| 79 | +module.exports = { |
| 80 | + getWebpackSettings, |
| 81 | + getLegacyWebpackConfig, |
| 82 | + getModernWebpackConfig, |
| 83 | + buildWebpackConfigs, |
| 84 | + legacyWebpackConfigs, |
| 85 | + modernWebpackConfigs, |
| 86 | +}; |
0 commit comments