module.exports = function(options) {
  const config = options.currentConfig;
  logger.verbose("add-dll-references configurations", JSON.stringify(config, null, 2));

  try {
    const exists = fs.existsSync(Path.resolve(archetype.AppMode.src.client, "dll.config.js"));
    const filenames = glob.sync(Path.resolve("dll", "js", "*-manifest.*.json"));

    if (exists && filenames.length) {
      return {
        plugins: filenames.map(
          filename =>
            new webpack.DllReferencePlugin({
              context: config.context,
              manifest: require(filename) // eslint-disable-line global-require
            })
        )
      };
    }
  } catch (err) {
    logger.error("add-dll-references failed: %s", err);
  }

  return {};
};
/* eslint-disable max-statements */
function generateConfig(options) {
  const composer = new WebpackConfigComposer();
  composer.addProfiles(options.profiles);
  composer.addPartials(partialConfigs.partials);

  let customConfig;
  const customDirs = [process.cwd(), Path.resolve("archetype/config/webpack")];

  const foundDir = customDirs.find(d => {
    customConfig = optionalRequire(Path.join(d, options.configFilename));
    return !!customConfig;
  });
  if (foundDir) {
    const dir = xsh.pathCwd.replace(foundDir);
    logger.info(`Custom webpack config ${options.configFilename} loaded from ${dir}`);
  } else {
    const dirs = customDirs.map(d => xsh.pathCwd.replace(d)).join("; ");
    logger.info(`No custom webpack config ${options.configFilename} found in dirs ${dirs}`);
  }

  const keepCustomProps = options.keepCustomProps;
  const compose = () => composer.compose({ keepCustomProps }, options.profileNames);

  let config;

  if (customConfig) {
    if (_.isFunction(customConfig)) {
      config = customConfig(composer, options, compose);
    } else {
      config = _.merge(compose(), customConfig);
    }
  } else {
    config = compose();
  }

  logger.verbose("Final Webpack config", JSON.stringify(config, null, 2));

  return config;
}