
const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const OpenBrowerPlugin = require('open-browser-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const merge = require('webpack-merge')

// webpack command run by npm start or npm run build, then it will be in the npm lifecycle.
const TARGET = process.env.npm_lifecycle_event
//const VERSION = process.env.npm_package_version


const PATH = {
  src: path.join(__dirname,'src'),
  example: path.join(__dirname, 'example'),
  dist: path.join(__dirname,'dist')
}

const common = {
  entry: TARGET === 'start' ? PATH.example : PATH.src,
  output: {
    path: PATH.dist,
    filename: 'bundle.js'
  },
  resolve: {
    extensions: ['.js','.jsx']
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.join(PATH.src, 'assets/tpl.html'),
      hash: true
    }),
    new ExtractTextPlugin('index.css')
  ],
  module: {
    rules: [
      {
        test: /\.css$/,
        include: [PATH.src, PATH.example],
        loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' })
      },
      {
        test: /\.(png|jpg)$/,
        loader: 'url-loader?limit=8000'
      },
      {
        test: /\.jsx?$/,
        include: [PATH.src, PATH.example],
        loader: 'babel-loader',
        query: {
          presets: ['es2015','stage-0','react']
        }
      }
    ]
  },
  externals: {
    jquery: 'jQuery'
  }
}

if(TARGET === 'start'){
  module.exports = merge(common, {
    devServer: {
      hot: true,
      inline: true,
      stats: 'errors-only',
      contentBase: PATH.dist
    },
    plugins: [
      new webpack.HotModuleReplacementPlugin(),
      new OpenBrowerPlugin({
        url: "http://localhost:8080"
      })
    ],
    devtool: 'source-map'
  })
}
if(TARGET === 'build'){
  module.exports = merge(common, {
    output: {
      library: 'td-components',
      libraryTarget: 'umd'
    },
    externals: {
      react: {
        root: 'React',
        commonjs2: 'react',
        commonjs: 'react',
        amd: 'react'
      },
      "react-dom": {
        root: 'ReactDOM',
        commonjs2: 'react-dom',
        commonjs: 'react-dom',
        amd: 'react-dom'
      },
      antd: {
        root: 'antd',
        commonjs2: 'antd',
        commonjs: 'antd',
        amd: 'antd'
      },
      handsontable: {
        root: 'Handsontable',
        commonjs2: 'handsontable',
        commonjs: 'handsontable',
        amd: 'handsontable'
      },
      "axios": "axios",
      "moment": "moment",
      "react-highcharts": {
        root: 'ReactHighcharts',
        commonjs2: 'react-highcharts',
        commonjs: 'react-highcharts',
        amd: 'react-highcharts'
      },
      "highcharts-export-csv": {
        root: 'HighchartsExportingCsv',
        commonjs2: 'highcharts-export-csv',
        commonjs: 'highcharts-export-csv',
        amd: 'highcharts-export-csv'
      },
      "highcharts-exporting": {
        root: 'HighchartsExporting',
        commonjs2: 'highcharts-exporting',
        commonjs: 'highcharts-exporting',
        amd: 'highcharts-exporting'
      },
      "highcharts-offline-exporting": {
        root: 'HighchartsOfflineExporting',
        commonjs2: 'highcharts-offline-exporting',
        commonjs: 'highcharts-offline-exporting',
        amd: 'highcharts-offline-exporting'
      },
      "td-react-2.0": {
        root: 'TdReact',
        commonjs2: 'td-react-2.0',
        commonjs: 'td-react-2.0',
        amd: 'td-react-2.0'
      }
    },
    plugins: [
        new webpack.optimize.UglifyJsPlugin({
            comments: false,        //去掉注释
            compress: {
                warnings: false    //忽略警告,要不然会有一大堆的黄色字体出现……
            },
            output: {
                ascii_only: true //解决Unicode字符在字符串和正则中出现会变乱码的问题
            }
        }),
      new webpack.DefinePlugin({
        'process.env.NODE_ENV': '"production"'
      })
    ]
  })
}
