var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

const PREPUBLISH_MODE = _getPrepublishMode();

function _getAutoPrefixerParams() {
  return '?{browsers:["' + [
    'Safari >= 6',
    'Chrome >= 26',
    'Firefox >= 10',
    'Explorer >= 9',
    'iOS >= 6',
    'ChromeAndroid >= 26'
  ].join('", "') + '"]}';
};

function _getBabelParams() {
  return '?optional[]=runtime';
};

function _getExternals() {
  if (PREPUBLISH_MODE) {
    return {
      'classnames': 'classnames',
      'clone': 'clone',
      'css-layout': 'css-layout',
      'd3': 'd3',
      'javascript-natural-sort': 'javascript-natural-sort',
      'moment': 'moment',
      'object-property-natural-sort': 'object-property-natural-sort',
      'react': 'react'
    };
  }
  return {};
};

function _getOutput() {
  if (PREPUBLISH_MODE) {
    return {
      filename: '[name].js',
      path: path.join(__dirname, './lib'),
      libraryTarget: 'umd',
      library: 'building-os-charts'
    };
  }
  return {
    filename: '[name].js',
    path: path.join(__dirname, './build')
  };
};

function _getPrepublishMode() {
  return process.env.NODE_ENV === 'prepublish' ? true : false;
};

function _getSassParams() {
  return '?outputStyle=compressed';
};

module.exports = {

  entry: {
    index: ['./src/index']
  },

  module: {
    preLoaders: [{
      test: /\.js$/,
      loader: 'eslint-loader',
      exclude: /node_modules/
    }],
    loaders: [{
      test: /\.js$/,
      loader: 'babel-loader' + _getBabelParams(),
      exclude: /node_modules/
    }, {
      test: /\.scss$/,
      loader: ExtractTextPlugin.extract('style-loader', [
        'css-loader',
        'autoprefixer-loader' + _getAutoPrefixerParams(),
        'sass-loader' + _getSassParams()
      ].join('!')),
      exclude: /node_modules/
    }]
  },

  output: _getOutput(),

  plugins: [
    new ExtractTextPlugin('[name].css'),
    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
  ],

  resolve: {
    alias: {
      'building-os-charts': '../../src/index'
    }
  },

  externals: _getExternals()
};

var webpack = require('webpack');
var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

const PREPUBLISH_MODE = process.env.NODE_ENV === 'prepublish' ? true : false;

function _getAutoPrefixerParams() {
  return '?{browsers:["' + [
    'Safari >= 6',
    'Chrome >= 26',
    'Firefox >= 10',
    'Explorer >= 9',
    'iOS >= 6',
    'ChromeAndroid >= 26'
  ].join('", "') + '"]}';
}

function _getBabelParams() {
  return '?optional[]=runtime';
}

function _getExternals() {
  if (PREPUBLISH_MODE) {
    return {
      'classnames': 'classnames',
      'clone': 'clone',
      'css-layout': 'css-layout',
      'd3': 'd3',
      'javascript-natural-sort': 'javascript-natural-sort',
      'moment': 'moment',
      'object-property-natural-sort': 'object-property-natural-sort',
      'react': 'react'
    };
  }
  return {};
}

function _getOutput() {
  if (PREPUBLISH_MODE) {
    return {
      filename: '[name].js',
      path: path.join(__dirname, './lib'),
      libraryTarget: 'umd',
      library: 'building-os-charts'
    };
  }
  return {
    filename: '[name].js',
    path: path.join(__dirname, './build')
  };
}

function _getSassParams() {
  return '?outputStyle=compressed';
}

module.exports = {

  entry: {
    index: ['./src/index']
  },

  module: {
    preLoaders: [{
      test: /\.js$/,
      loader: 'eslint-loader',
      exclude: /node_modules/
    }],
    loaders: [{
      test: /\.js$/,
      loader: 'babel-loader' + _getBabelParams(),
      exclude: /node_modules/
    }, {
      test: /\.scss$/,
      loader: ExtractTextPlugin.extract('style-loader', [
        'css-loader',
        'autoprefixer-loader' + _getAutoPrefixerParams(),
        'sass-loader' + _getSassParams()
      ].join('!')),
      exclude: /node_modules/
    }]
  },

  output: _getOutput(),

  plugins: [
    new ExtractTextPlugin('[name].css'),
    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
  ],

  resolve: {
    alias: {
      'building-os-charts': '../../src/index'
    }
  },

  externals: _getExternals()
};

tweak webpack.config.js so it passes linting