feat: add merging logic

This commit is contained in:
Igor Randjelovic
2020-11-22 13:19:32 +01:00
parent 573d936976
commit 39949182ea

View File

@ -1,9 +1,11 @@
import { merge } from 'webpack-merge';
import Config from 'webpack-chain'; import Config from 'webpack-chain';
import webpack from 'webpack'; import webpack from 'webpack';
import { highlight } from 'cli-highlight'; import { highlight } from 'cli-highlight';
import { configs } from './configuration'; import { configs } from './configuration';
import { determineProjectFlavor } from './helpers/flavor'; import { determineProjectFlavor } from './helpers/flavor';
import { applyExternalConfigs } from './helpers/externalConfigs'; import { applyExternalConfigs } from './helpers/externalConfigs';
import { error, info } from './helpers/log';
export type Platform = 'android' | 'ios' | string; export type Platform = 'android' | 'ios' | string;
@ -28,6 +30,7 @@ export interface IWebpackEnv {
let webpackChains: any[] = []; let webpackChains: any[] = [];
let webpackMerges: any[] = []; let webpackMerges: any[] = [];
let explicitUseConfig = false; let explicitUseConfig = false;
let hasInitialized = false;
/** /**
* @internal * @internal
@ -38,6 +41,7 @@ export let env: IWebpackEnv = {};
export const defaultConfigs = configs; export const defaultConfigs = configs;
export function init(_env: IWebpackEnv) { export function init(_env: IWebpackEnv) {
hasInitialized = true;
if (_env) { if (_env) {
env = _env; env = _env;
} }
@ -82,18 +86,32 @@ export function resolveChainableConfig() {
}); });
if (env.verbose) { if (env.verbose) {
console.log('Resolved chainable config:'); info('Resolved chainable config (before merges):');
console.log(highlight(config.toString(), { language: 'js' })); info(highlight(config.toString(), { language: 'js' }));
} }
return config; return config;
} }
export function resolveConfig(chainableConfig = resolveChainableConfig()) { export function resolveConfig(chainableConfig = resolveChainableConfig()) {
// todo: warn if no base config if (!hasInitialized) {
throw error('resolveConfig() must be called after init()');
}
// todo: apply merges from webpackMerges let config = chainableConfig.toConfig();
// this applies webpack merges
webpackMerges.forEach((mergeFn) => {
if (typeof mergeFn === 'function') {
// mergeFn is a function with optional return value
const res = mergeFn(config, env);
if (res) config = merge(config, res);
} else if (mergeFn) {
// mergeFn is a literal value (object)
config = merge(config, mergeFn);
}
});
// return a config usable by webpack // return a config usable by webpack
return chainableConfig.toConfig(); return config;
} }