var fse = require('fs-extra');
const util = require('util');

const webpack = require('webpack');

const { getReplacements } = require('./app-info');
const { hasProcessFlag, root } = require('./helpers.js');

const replacements = getReplacements();

const EVENT = process.env.npm_lifecycle_event || '';
const PROD = EVENT.includes('prod');

module.exports = function (config) {
  config.target = 'electron-renderer';
  config.output = {
    path: PROD ? root('build', 'dist') : root('build', 'dev'),
    filename: '[name].js',
    crossOriginLoading: false,
  };

  config.plugins.push(
    new webpack.DefinePlugin(
      Object.assign({}, replacements, {
        __PROCESS_KIND__: JSON.stringify('renderer'),
      })
    )
  );

  var json = util.inspect(config, { showHidden: false, depth: null });
  fse.writeFileSync('webpack.js', json, 'utf8');

  return config;
};
