'use strict';

const merge = require('webpack-merge');
const common = require('./webpack.common.js');
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require("html-webpack-plugin");
const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const CleanWebpackPlugin = require('clean-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

const TARGET = process.env.npm_lifecycle_event;
const HOT = TARGET === 'start';

const extractSass = new ExtractTextPlugin({
  filename: "grafana.[name].css",
  disable: HOT
});

const entries = HOT ? {
  app: [
    'webpack-dev-server/client?http://localhost:3333',
    './public/app/dev.ts',
  ],
  vendor: require('./dependencies'),
} : {
    app: './public/app/index.ts',
    dark: './public/sass/grafana.dark.scss',
    light: './public/sass/grafana.light.scss',
    vendor: require('./dependencies'),
  };

module.exports = merge(common, {
  devtool: "cheap-module-source-map",

  entry: entries,

  resolve: {
    extensions: ['.scss', '.ts', '.tsx', '.es6', '.js', '.json', '.svg', '.woff2', '.png'],
  },

  devServer: {
    publicPath: '/public/build/',
    hot: HOT,
    port: 3333,
    proxy: {
      '!/public/build': 'http://localhost:3000'
    }
  },

  module: {
    rules: [
      {
        test: /\.tsx?$/,
        enforce: 'pre',
        exclude: /node_modules/,
        use: {
          loader: 'tslint-loader',
          options: {
            emitErrors: true,
            typeCheck: false,
          }
        }
      },
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              plugins: [
                'syntax-dynamic-import',
                'react-hot-loader/babel',
              ],
            },
          },
          {
            loader: 'awesome-typescript-loader',
            options: {
              useCache: true,
            },
          }
        ]
      },
      require('./sass.rule.js')({
        sourceMap: true, minimize: false, preserveUrl: HOT
      }, extractSass),
      {
        test: /\.(ttf|eot|svg|woff(2)?)(\?[a-z0-9=&.]+)?$/,
        loader: 'file-loader'
      },
      {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {}
          }
        ]
      },
    ]
  },

  plugins: [
    new CleanWebpackPlugin('../public/build', { allowExternal: true }),
    extractSass,
    new HtmlWebpackPlugin({
      filename: path.resolve(__dirname, '../../public/views/index.html'),
      template: path.resolve(__dirname, '../../public/views/index.template.html'),
      inject: 'body',
      chunks: ['manifest', 'vendor', 'app'],
      alwaysWriteToDisk: HOT
    }),
    new HtmlWebpackHarddiskPlugin(),
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      'GRAFANA_THEME': JSON.stringify(process.env.GRAFANA_THEME || 'dark'),
      'process.env': {
        'NODE_ENV': JSON.stringify('development')
      }
    }),
    new webpack.optimize.CommonsChunkPlugin({
      names: ['vendor', 'manifest'],
    }),
    // new BundleAnalyzerPlugin({
    //   analyzerPort: 8889
    // })
  ]
});

@@ -1020,16 +1020,360 @@
 ,%0A  %7D;%0A%0A
+const output = HOT ? %7B%0A  path: path.resolve(__dirname, '../../public/build'),%0A  filename: '%5Bname%5D.%5Bhash%5D.js',%0A  publicPath: %22/public/build/%22,%0A%7D : %7B%0A    path: path.resolve(__dirname, '../../public/build'),%0A    filename: '%5Bname%5D.%5Bhash%5D.js',%0A    // Keep publicPath relative for host.com/grafana/ deployments%0A    publicPath: %22public/build/%22,%0A  %7D;%0A%0A
 module.e
@@ -1455,16 +1455,35 @@
 tries,%0A%0A
+  output: output,%0A%0A
   resolv
@@ -2093,32 +2093,18 @@
    use: 
-%5B%0A          %7B%0A  
+%7B%0A
         
@@ -2114,21 +2114,34 @@
 oader: '
-babel
+awesome-typescript
 -loader'
@@ -2144,34 +2144,32 @@
 der',%0A          
-  
 options: %7B%0A     
@@ -2179,134 +2179,107 @@
     
-  plugins: %5B%0A                'syntax-dynamic-import',%0A                'react-hot-loader/babel',%0A              %5D,%0A            %7D
+useCache: true,%0A            useBabel: HOT,%0A            babelOptions: %7B%0A              babelrc: false
 ,%0A  
@@ -2290,22 +2290,22 @@
     
-%7D,%0A
     
-      %7B
+plugins: %5B
 %0A   
@@ -2317,100 +2317,108 @@
     
-loader: 'awesome-typescript-loader',%0A            options: %7B%0A              useCache: true,%0A  
+    'syntax-dynamic-import',%0A                'react-hot-loader/babel'%0A              %5D%0A            %7D%0A
     
@@ -2430,37 +2430,25 @@
  %7D,%0A        
-  %7D%0A        %5D
+%7D
 %0A      %7D,%0A  

Use babel and hot loader only in yarn start