"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");

_Object$defineProperty(exports, "__esModule", {
  value: true
});

exports.default = extendWebpackConfig;

var _debug = _interopRequireDefault(require("debug"));

var _fsJetpack = _interopRequireDefault(require("fs-jetpack"));

var _webpackMerge = _interopRequireDefault(require("webpack-merge"));

var _webpack = _interopRequireDefault(require("webpack"));

function extendWebpackConfig(config, APP_PATH, plugins) {
  try {
    const FILENAME = 'webpack.custom.config.js';

    const srcPath = _fsJetpack.default.cwd(APP_PATH || process.cwd());

    if (srcPath.exists(FILENAME)) {
      const customConfig = require(srcPath.path(FILENAME));

      if (customConfig && typeof customConfig === 'function') {
        config = _webpackMerge.default.smart(config, customConfig({
          webpack: _webpack.default,
          merge: _webpackMerge.default,
          plugins
        }));
      } else if (customConfig) {
        config = (0, _webpackMerge.default)(config, customConfig);
      }
    } else {
      (0, _debug.default)('webpack:config')('No custom config found');
    }
  } catch (e) {
    console.log('ERROR in webpack.custom.config', e);
  }

  return config;
}

module.exports = exports.default;