var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var CopyWebPackPlugin = require('copy-webpack-plugin');
var helpers = require('./helpers');

var path = require('path');

module.exports = {
    entry: {
        'polyfills': './src/polyfills.ts',
        'vendor': './src/vendor.ts',
        'app': './src/main.ts'
    },
    resolve: {
        extensions: ['.js', '.ts']
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                enforce: 'pre',
                loader: 'tslint-loader',
                options: {
                    configuation: require('../tslint.json'),
                    emitErrors: true,
                    failOnHint: true
                }
            },
            {
                test: /\.ts$/,
                use: ['awesome-typescript-loader', 'angular2-template-loader', 'angular-router-loader']
            },
            {
                test: /\.html$/,
                loader: 'html-loader'
            },
            {
                test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
                loader: 'file-loader',
                options: { name: 'assets/[name].[hash].[ext]' }
            },
            {
                test: /\.css$/,
                exclude: [helpers.root('src', 'modules')],
                loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader?sourceMap' })
            },
            {
                test: /\.css$/,
                include: [helpers.root('src', 'modules')],
                loader: 'raw-loader'
            },
            {
                test: /\.scss$/,
                exclude: [helpers.root('src', 'modules')],
                loader: ExtractTextPlugin.extract({
                    fallback: 'style-loader', use: ['css-loader?sourceMap', 'sass-loader?sourceMap']
                })
            },
            {
                test: /\.scss$/,
                include: [helpers.root('src', 'modules')],
                use: ['raw-loader', {
                    loader: 'sass-loader',
                    // options: {
                    //     importer: (url, prev, done) => {
                    //         // if (url[0] === '~') {
                    //         //     console.log('inside', url,url.substr(1), path.resolve('node_modules', url.substr(1)));
                    //         //     url = path.resolve('node_modules', url.substr(1));
                    //         // } else if (url.startsWith('styles/')) {
                    //         //     url = path.resolve('src/', url);
                    //         // }

                    //         return { file: url }
                    //     }
                    // }
                }]
            },
        ]
    },

    plugins: [
        new webpack.ContextReplacementPlugin(
            /angular(\\|\/)core(\\|\/)(@angular|esm5)/, // /angular(\\|\/)core(\\|\/)@angular/,
             helpers.root('src')
        ),
        new webpack.optimize.CommonsChunkPlugin({
            name: ['app', 'vendor', 'polyfills']
        }),

        new HtmlWebpackPlugin({
            template: 'src/index.html'
        }),

        new CopyWebPackPlugin([{
            from: helpers.root('src', 'i18n'),
            to: 'i18n'
        }, {
            from: helpers.root('favicons'),
            to: 'favicons'
        }])
    ]
};
