const webpack = require('webpack')
const merge = require('webpack-merge')
const baseConfig = require('./webpack.base.conf')
const cssLoaders = require('./css-loaders')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
// const HtmlWebpackPlugin = require('html-webpack-plugin')
const FileManagerPlugin = require('filemanager-webpack-plugin')
const ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin')
const path = require('path')

// 将日期时间进行格式化
function formatDate (date, format) {
  // name="date" type="Date" 要格式化的日期时间
  // name="format" type="String" 格式类型，形如 yyyy-MM-dd hh:mm:ss
  var o = {
    'M+': date.getMonth() + 1,
    // 月份
    'd+': date.getDate(),
    // 日
    'h+': date.getHours(),
    // 小时
    'm+': date.getMinutes(),
    // 分
    's+': date.getSeconds() // 秒
  }
  if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) if (new RegExp('(' + k + ')').test(format)) format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  return format
}
const timeStamp = formatDate(new Date(), 'yyyy-MM-dd-hh-mm-ss')
console.log('开始打包:', timeStamp)

// whether to generate source map for production files.
// disabling this can speed up the build.
const SOURCE_MAP = false

module.exports = merge(baseConfig, {
  externals: {
    'vue-client': 'vue-client',
    'system-clients': 'system-clients',
    'vue': 'vue'
  },
  stats: {
    children: false
  },
  devtool: SOURCE_MAP ? '#source-map' : false,
  entry: {
    app: './src/ldap.js'
  },
  output: {
    // naming output files with hashes for better caching.
    // dist/index.html will be auto-generated with correct URLs.
    publicPath: 'library/ldap/lib/',
    path: path.resolve(__dirname, '../lib/lib'),
    filename: 'lib.js',
    libraryTarget: 'umd'  // 不管在commonJS环境还是在AMD环境,能让任何形式引入都可以引入
  },
  vue: {
    loaders: cssLoaders({
      sourceMap: SOURCE_MAP,
      extract: true
    })
  },
  plugins: [
    // http://vuejs.github.io/vue-loader/workflow/production.html
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new ParallelUglifyPlugin({
      // 用作缓存的可选绝对路径。如果未提供，则不使用缓存。
      cacheDir: '.cache/',
      uglifyJS: {
        output: {
          // 是否保留代码中的注释，默认为保留
          comments: false
        },
        // 是否在UglifyJS删除没有用到的代码时输出警告信息，默认为false
        warnings: false,
        compress: {
          // 是否删除代码中所有的console语句，默认为false
          drop_console: true,
          // 是否内嵌虽然已经定义了，但是只用到一次的变量， 默认值false
          collapse_vars: true,
          // 是否提取出现了多次但是没有定义成变量去引用的静态值，默认为false
          reduce_vars: true
        }
      }
    }),
    // new webpack.optimize.UglifyJsPlugin({
    //   compress: {
    //     warnings: false
    //   }
    // }),
    new webpack.optimize.OccurenceOrderPlugin(),
    // extract css into its own file
    new ExtractTextPlugin('[name].css'),
    // generate dist index.html with correct asset hash for caching.
    // you can customize output by editing /index.html
    // see https://github.com/ampedandwired/html-webpack-plugin
    // new HtmlWebpackPlugin({
    //   filename: '../index.html',
    //   template: 'index.html',
    //   inject: true,
    //   minify: {
    //     removeComments: true,
    //     collapseWhitespace: true,
    //     removeAttributeQuotes: true
    //     // more options:
    //     // https://github.com/kangax/html-minifier#options-quick-reference
    //   }
    // }),
    // 打包文件压缩
    new FileManagerPlugin({
      onEnd: {
        copy: [
          {source: './package.json', destination: './lib/package_' + timeStamp + '.json'}
        ],
        archive: [
          { source: './lib', destination: './ldap_lib' + timeStamp + '.zip' }
        ]
      }
    })
  ]
})
