'use strict';

var webpack = require('webpack');
var autoprefixer = require('autoprefixer');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var OfflinePlugin = require('offline-plugin');

const WebpackShellPlugin = require('webpack-shell-plugin');

const PROD_DOMAIN = 'domain';
const PROD_ROOT_PATH = '/' + PROD_DOMAIN;
const PreBuildTask = `ls -l ${PROD_DOMAIN} && rm -rf  ${PROD_DOMAIN} && ls -l ${PROD_DOMAIN}`;
console.log('PreBuildTask ' + PreBuildTask);

require('url-loader');

  
module.exports = function makeWebpackConfig() {
  var config = {};
  config.entry = {
    app: './src/app/app.js'
  };

  config.output = {
    path: __dirname + PROD_ROOT_PATH,
    publicPath: PROD_ROOT_PATH,
    filename: '[name].[hash].js',
    chunkFilename: '[name].[hash].js'
  };

  config.devtool = 'source-map';

  config.module = {
    preLoaders: [],
    loaders: [{
      test: /\.js$/,
      loader: 'babel',
      exclude: /node_modules/
    }, {
      test: /\.css$/,
      loader: ExtractTextPlugin.extract('style', 'css?sourceMap!postcss')
    }, {
      test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/,
      loader: 'file'
    }, {
      test: /\.html$/,
      loader: 'raw'
    },
          { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&minetype=application/font-woff" },
      { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "file-loader" }
]
  };
  config.postcss = [
    autoprefixer({
      browsers: ['last 2 version']
    })
  ];



  config.plugins = [];

  config.plugins.push(
    new WebpackShellPlugin({
      onBuildStart: [PreBuildTask],
      onBuildEnd: [`echo "Ending "${PROD_ROOT_PATH}`]
    }),
    new HtmlWebpackPlugin({
      template: './src/public/index.prod.html',
      filename: 'index.html',
      inject: 'body'
    }),

    new ExtractTextPlugin('[name].[hash].css', {
      disable: false
    }),

    new webpack.NoErrorsPlugin(),

    new webpack.optimize.DedupePlugin(),

    new webpack.optimize.UglifyJsPlugin(),

    new CopyWebpackPlugin(
      [{from: __dirname + '/src/public'}], 
      {
      ignore: [{glob: '**/*.prod.html'}]
    }),
    new OfflinePlugin({
      // All options are optional
      caches: 'all',
      scope: PROD_ROOT_PATH + '/',
      updateStrategy: 'all',
      version: 'v2',

      ServiceWorker: {
        output: 'sw.js'
      },

      AppCache: {
        directory: 'appcache/'
      }
    })
  )
  return config;
}();
@@ -538,16 +538,116 @@
 MAIN%7D%60;%0A
+const PROD_DEPLOY_PATH = '/www';%0Aconst PostBuildTask = %60cp -r $%7BPROD_DOMAIN%7D $%7BPROD_DEPLOY_PATH%7D%60;%0A%0A
 console.
@@ -652,18 +652,19 @@
 e.log('P
-re
+ost
 BuildTas
@@ -670,18 +670,19 @@
 sk ' + P
-re
+ost
 BuildTas
@@ -714,11 +714,9 @@
 );%0A%0A
-  
 %0A
+
 modu
@@ -1420,20 +1420,16 @@
   %7D,
+ %7B
 %0A     
-     %7B
  tes
@@ -1463,32 +1463,38 @@
 0-9%5D%5C.%5B0-9%5D)?$/,
+%0A     
  loader: %22url-lo
@@ -1545,19 +1545,23 @@
 off%22
+%0A   
  %7D,
+ %7B
 %0A     
- %7B
  tes
@@ -1608,16 +1608,22 @@
 -9%5D)?$/,
+%0A     
  loader:
@@ -1636,19 +1636,22 @@
 -loader%22
+%0A   
  %7D
-%0A
 %5D%0A  %7D;%0A 
@@ -1786,16 +1786,17 @@
 s.push(%0A
+%0A
     new 
@@ -1875,41 +1875,80 @@
 d: %5B
-%60
+'
 echo %22
-Ending %22$%7BPROD_ROOT_PATH%7D%60
+endddddd%22 && ls -l ./domain', PostBuildTask, 'echo %22endddddd222%22'
 %5D%0A  
@@ -2324,16 +2324,25 @@
       %5B%7B
+%0A        
 from: __
@@ -2368,21 +2368,23 @@
 lic'
-%7D%5D, 
 %0A     
+ %7D%5D,
  %7B%0A
+  
     
@@ -2395,16 +2395,27 @@
 nore: %5B%7B
+%0A          
 glob: '*
@@ -2428,19 +2428,30 @@
 od.html'
+%0A        
 %7D%5D%0A
+  
     %7D),%0A

support deployment to external directory outside of the project