var webpack = require('webpack');
//var webpackMerge = require('webpack-merge');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
//var commonConfig = require('./webpack.common.js');

var helpers = require('./helpers');

const SETTINGS = {
    API_URL: 'http://localhost:52281/v1',
    API_IDENTIFIER: "138659EBEBBF408AA1282D46EBDFBDC7"
};

module.exports = {
    devtool: 'inline-source-map',

    resolve: {
        extensions: [ '.ts', '.js']
    },

    module: {
        rules: [
            {
                test: /\.ts$/,
                loaders: ['awesome-typescript-loader', 'angular2-template-loader']
            },
            {
                test: /\.html$/,
                loader: 'html-loader'
            },
            {
                test: /\.(pug|jade)$/,
                exclude: /node_modules/,
                loader: 'pug-ng-html-loader',
                query: {
                  doctype: 'html'
                }
              },
            {
                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'),
                ],
                loader: ExtractTextPlugin.extract({
                  fallback: 'style-loader',
                  use: [
                    {
                      loader: 'css-loader',
                      options: {sourceMap: true, importLoaders: 1, minimize: true}
                    }
                  ]
                })
              },
              {
                test: /\.css$/,
                include: [
                  helpers.root('src')
                ],
                loader: 'raw-loader'
              },
              {
                test: /\.scss$/,
                exclude: [
                  helpers.root('src')
                ],
                loader: ExtractTextPlugin.extract({
                  fallback: 'style-loader',
                  use: [
                    {
                      loader: 'css-loader',
                      options: {sourceMap: true, importLoaders: 1, minimize: true}
                    },
                    {
                      loader: 'sass-loader',
                      options: {
                        sourceMap: true,
                        includePaths: [
                          helpers.root('node_modules')/*,
                          helpers.root('node_modules/@material/*')*/
                        ]
                      }
                    }
                  ]
                })
              },
              {
                test: /\.scss$/,
                include: [
                  helpers.root('src')
                ],
                use: [
                  'raw-loader',
                  'resolve-url-loader',
                  {
                    loader: 'sass-loader',
                    options: {
                      sourceMap: true,
                      includePaths: [
                        helpers.root('node_modules')/*,
                        helpers.root('node_modules/@material/*')*/
                      ]
                    }
                    // 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.DefinePlugin({
            SETTINGS: JSON.stringify(SETTINGS)
        }),
        new webpack.ContextReplacementPlugin(
            /angular(\\|\/)core(\\|\/)(@angular|esm5)/, // /angular(\\|\/)core(\\|\/)@angular/,
            helpers.root('src'),
            {}
        ),
        new ExtractTextPlugin('[name].css')
    ]
}
