import * as path from 'path';

import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
// import ExtractTextPlugin from 'extract-text-webpack-plugin';
// import Clean from 'clean-webpack-plugin';
import merge from 'webpack-merge';
import pkg from './package.json';

const TARGET = process.env.npm_lifecycle_event;
const ROOT_PATH = __dirname;
const config = {
  paths: {
    dist: path.join(ROOT_PATH, 'dist'),
    src: path.join(ROOT_PATH, 'src'),
    demo: path.join(ROOT_PATH, 'demo'),
  },
  filename: pkg.name,
  library: 'DumbComponent',
};

const AUTOPREFIXER_BROWSERS = [
  'Android 2.3', 'Android >= 4', 'Chrome >= 35', 'Firefox >= 31', 'Explorer >= 9',
  'iOS >= 7', 'Opera >= 12', 'Safari >= 7.1',
];

const CSS_PATHS = [
  config.paths.demo,
  config.paths.src,
];

process.env.BABEL_ENV = TARGET;


/* COMPONENT DEMO
 * ---------------------------------------------------------------------------------------------- */
const demoCommon = {
  resolve: {
    extensions: ['', '.js', '.jsx', '.css', '.png', '.jpg'],
  },
  module: {
    preLoaders: [
      { test: /\.jsx?$/, loaders: ['eslint'], include: [config.paths.demo, config.paths.src] },
    ],
    loaders: [
      { test: /\.png$/, loader: 'url?limit=100000&mimetype=image/png', include: config.paths.demo },
      { test: /\.jpg$/, loader: 'file', include: config.paths.demo },
      { test: /\.json$/, loader: 'json', include: path.join(ROOT_PATH, 'package.json') },
    ],
  },
};

if (TARGET === 'start' || !TARGET) {
  module.exports = merge(demoCommon, {
    devtool: 'eval-source-map',
    entry: config.paths.demo,
    plugins: [
      new webpack.DefinePlugin({
        'process.env.NODE_ENV': JSON.stringify('development'),
      }),
      new HtmlWebpackPlugin({
        title: pkg.name + ' - ' + pkg.description,
      }),
      new webpack.HotModuleReplacementPlugin(),
    ],
    module: {
      loaders: [
        { test: /\.scss$/, loaders: ['style', 'css', 'postcss'], include: CSS_PATHS },
        {
          test: /\.jsx?$/,
          loaders: ['babel?cacheDirectory'],
          include: [config.paths.demo, config.paths.src],
        },
      ],
    },
    postcss: (webpack) => {
      return [
        require('postcss-import')({ addDependencyTo: webpack }),
        require('precss')(),
        require('autoprefixer')({ browsers: AUTOPREFIXER_BROWSERS }),
      ];
    },
    devServer: {
      historyApiFallback: true,
      hot: true,
      inline: true,
      progress: true,
      host: process.env.HOST,
      port: process.env.PORT,
      stats: 'errors-only',
    },
  });
}


/* COMPONENT DIST
 * ---------------------------------------------------------------------------------------------- */
const distCommon = {
  devtool: 'source-map',
  output: {
    path: config.paths.dist,
    libraryTarget: 'umd',
    library: config.library,
  },
  entry: config.paths.src,
  externals: {
    react: {
      commonjs: 'react',
      commonjs2: 'react',
      amd: 'React',
      root: 'React',
    },
  },
  module: {
    loaders: [
      { test: /\.scss?$/, loaders: ['style', 'css', 'postcss'], include: config.paths.src },
      { test: /\.jsx?$/, loaders: ['babel'], include: config.paths.src },
    ],
  },
  postcss: (webpack) => {
    return [
      require('postcss-import')({ addDependencyTo: webpack }),
      require('precss')(),
      require('autoprefixer')({ browsers: AUTOPREFIXER_BROWSERS }),
    ];
  },
};

if (TARGET === 'dist') {
  module.exports = merge(distCommon, {
    output: {
      filename: config.filename + '.js',
    },
  });
}

if (TARGET === 'dist:min') {
  module.exports = merge(distCommon, {
    output: {
      filename: config.filename + '.min.js',
    },
    plugins: [
      new webpack.optimize.UglifyJsPlugin({
        compress: { warnings: false },
      }),
    ],
  });
}

@@ -2174,37 +2174,33 @@
 %0A    postcss: (w
-ebpac
+p
 k) =%3E %7B%0A      re
@@ -2252,37 +2252,33 @@
 dDependencyTo: w
-ebpac
+p
 k %7D),%0A        re
@@ -3239,21 +3239,17 @@
 tcss: (w
-ebpac
+p
 k) =%3E %7B%0A
@@ -3313,21 +3313,17 @@
 ncyTo: w
-ebpac
+p
 k %7D),%0A  

Fix linting errors