const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const webpack = require('webpack');
const cssnano = require('cssnano');
const context = path.join(__dirname);

const entryArray = [
  'layout/LayoutWrapper',
  'layout/MainHeader',
  'layout/MainFooter',
  'layout/MainContentWrapper',
  'layout/MainColumn',
  'layout/NavBar',
  'display/Title',
  'layout/Sidebar',
  'layout/Sidebar',
  'layout/Section',
  'layout/HeaderBrand',
  'display/Button',
  'display/Table',
  'display/Modal',
  'form/inputs/TextInput',
  'form/inputs/FieldControl',
  'display/Tooltip',
  'form/inputs/SelectInput',
  'display/Cards',
  'form/inputs/TextArea',
  'form/inputs/CheckBoxes',
  'form/inputs/CheckBox',
  'form/inputs/Radio',
  'display/Dropdown',
  'display/Alert',
  'display/Stepper',
  'display/Tabs',
  'display/Background',
  'display/Media',
  'form/inputs/Autocomplete',
  'display/TextIcon',
  'display/Icon',
  'display/Slideshow',
  'display/BreadCrumb',
  'display/SortableList',
  'display/Label',
  'display/Spinner',
  'display/UIStates',
  'form/inputs/RichTextEditor',
  'form/Form',
  'sigfox/SigfoxBrand',
  'sigfox/SigfoxLogoIdSide'
];
const entry = {
  'flive-ui': './src/index.js'
};

/* entryArray.forEach(itemEntry => {
  const key = itemEntry.split('/').pop();
  // eslint-disable-next-line no-param-reassign
  entry[key] = `./src/${itemEntry}/index.js`;
}); */

const config = {
  context,
  entry,
  output: {
    library: 'flive-ui',
    libraryTarget: 'umd',
    filename: '[name].js',
    path: path.resolve(__dirname, 'lib')
  },
  stats: 'minimal',
  externals: {
    'prop-types': {
      commonjs: 'prop-types',
      commonjs2: 'prop-types',
      amd: 'PropTypes',
      root: 'PropTypes'
    },
    react: {
      commonjs: 'react',
      commonjs2: 'react',
      amd: 'React',
      root: 'React'
    },
    'react-dom': {
      commonjs: 'react-dom',
      commonjs2: 'react-dom',
      amd: 'ReactDom',
      root: 'ReactDom'
    }
  },
  plugins: [
    new ExtractTextPlugin('[name].css'),
    new OptimizeCssAssetsPlugin({
      cssProcessor: cssnano({ zindex: false }),
      cssProcessorOptions: { discardComments: { removeAll: true } },
      canPrint: true,
      zindex: false
    }),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': '"production"'
    })
  ],
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [{
          loader: 'babel-loader',
          options: {
            presets: ['es2015', 'stage-0', 'react'],
            babelrc: false,
            plugins: [
              'add-module-exports',
              ['react-css-modules', {
                context,
                generateScopedName: 'flive_[name]_[local]',
                filetypes: {
                  '.scss': {
                    syntax: 'postcss-scss'
                  }
                }
              }]
            ]
          }
        }],
        exclude: /node_modules/,
        include: context
      },
      {
        test: /\.(scss)$/,
        loader: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader?importLoader=1&modules&localIdentName=flive_[name]_[local]'
            },
            // { loader: 'resolve-url'},
            {
              loader: 'sass-loader',
              options: {
                includePaths: [
                  './node_modules/susy/sass',
                  `${context}/src/_assets/style`,
                  `${context}/src/_assets/style-override`
                ]
              }
            }
          ]
        })
      },
      {
        test: /\.(css)$/,
        loader: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [
            {
              loader: 'css-loader'
            }
          ]
        })
      },
      {
        test: /\.(png|jpg|gif|ttf|otf|svg|eot|woff|woff2)$/,
        use: ['file-loader']
      }
    ]
  }
};

module.exports = config;
