const webpack = require('webpack'),
    ExtractTextPlugin = require('extract-text-webpack-plugin'),
    LessPluginAutoPrefix = require('less-plugin-autoprefix'),
    LessPluginCleanCSS = require('less-plugin-clean-css'),
    LessPluginGroupCSSMediaQueries =
        require('less-plugin-group-css-media-queries'),
    config = require('./project.config.js'),
    lintPath = require.resolve(
        'web-foundation/tests/config/lint' +
        (process.env.NODE_ENV === 'production' ? '' : '-dev') +
        '.json'
    ),
    lessParams = JSON.stringify({
        globalVars: {
            theme: config.theme || 'buyer'
        },
        sourceMap: true
    });

module.exports = {
    entry: config.entry,

    output: {
        path: __dirname,
        filename: '/static/js/' + config.project + '/[name].min.js'
    },

    resolve: {
        extensions: [ '', '.js', '.less', '.jsx' ],
        alias: config.alias || {}
    },

    devtool: 'source-map',

    // in a perfect world, there are no externals
    externals: config.externals || {},

    module: {
        preLoaders: [ {
            test: /\.(js|jsx)$/,
            exclude: /(node_modules)/,
            loaders: [ 'eslint-loader' ]
        } ],
        loaders: [ {
            test: /\.(less|css)$/,
            exclude: [
                'node_modules/**/*',
                '!node_modules/web-foundation/**.*'
            ],
            loader: ExtractTextPlugin.extract(
                'css?sourceMap!less?' + lessParams
            )
        }, {
            test: /\.(js|jsx)$/,
            exclude: [
                'node_modules/**/*',
                '!node_modules/dd/**.*',
                '!node_modules/web-foundation/**.*'
            ],
            loader: 'babel-loader',
            query: {
                presets: [ 'es2015', 'react', 'stage-0' ]
            }
        }, {
            test: /\.(jpe?g|png|gif|svg)$/i,
            loaders: [
                'file?name=/static/img/' + config.project + '/[hash:8].[ext]',
                'image-webpack?bypassOnDebug&' +
                    'optimizationLevel=7&interlaced=false'
            ]
        } ]
    },

    plugins: [
        // https://github.com/webpack/webpack/issues/787
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin({
            mangle: process.env.NODE_ENV === 'production'
        }),
        new ExtractTextPlugin(
            '/static/css/' + config.project + '/[name].min.css'
        )
    ],

    lessLoader: {
        lessPlugins: [
            LessPluginGroupCSSMediaQueries,
            new LessPluginAutoPrefix({
                browsers: [
                    'last 2 version',
                    'safari 5',
                    'ie 8',
                    'ie 9',
                    'opera 12.1',
                    'ios 6',
                    'android 4'
                ]
            }),
            new LessPluginCleanCSS({
                advanced: true
            })
        ]
    },

    eslint: {
        configFile: lintPath
    }
};
