'use strict'
const baseWebpackConfig = require('./webpack.base.conf')
const path = require('path')
const webpack = require('webpack')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const merge = require('webpack-merge')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
// const HtmlWebpackPlugin = require('html-webpack-plugin')
// const ExtractTextPlugin = require('extract-text-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
const RedevelopmentPlugin = require('./webpack-plugins/zving-redevelopment-plugin/RedevelopmentPlugin')

const faster = process.env.MODE ? process.env.MODE.trim() === 'faster' : false
const output = process.env.OUTPUT ? process.env.OUTPUT.trim() : resolve('dist')

function resolve (dir) {
  return path.join(__dirname, '..', dir)
}

const webpackConfig = merge(baseWebpackConfig, {
  mode: 'production',
  devtool: false,
  output: {
    path: output
  },
  optimization: {
  },
  plugins: [
    new CaseSensitivePathsPlugin(),
    new VueLoaderPlugin(),
    // http://vuejs.github.io/vue-loader/en/workflow/production.html
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify('production')
    }),
    // extract css into its own file
    // new ExtractTextPlugin({
    //   filename: 'assets/css/[name].[chunkhash:6].css',
    //   allChunks: true
    // }),
    // 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({
    //   template: path.resolve(__dirname, '../src/index.html'),
    //   filename: path.resolve(__dirname, '../dist/index.html'),
    //   chunks: ['app'],
    //   inject: true
    // }),
    // keep module.id stable when vendor modules does not change
    new webpack.HashedModuleIdsPlugin(),
    // new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
    // 2019-4-19 因为 DllPlugin 插件与二次开发插件一个难以调和的冲突，不再便用 DllPlugin 插件
    // new webpack.DllReferencePlugin({
    //   manifest: require('../static/dll-manifest.json')
    // }),
    new RedevelopmentPlugin({
      // 为了二次开发设置一个全局变量指向 __webpack_require__
      path: path.join(__dirname, '../static/redevelopment-manifest.json')
    }),

    // enable scope hoisting
    new webpack.optimize.ModuleConcatenationPlugin(),

    // copy custom static assets
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, '../static'),
        to: '',
        ignore: ['*.psd']
      },
      {
        from: path.resolve(__dirname, '../src/*.html'),
        to: '',
        flatten: true,
        transform: function (content, path) {
          return content.toString().replace(/__replace_by_time__/g, ~~(Date.now() / 10000))
        }
      }, {
        from: path.resolve(__dirname, '../static/lib/vue.min.js'),
        to: 'lib/vue.js'
      }
    ])
  ]
})
if (faster) {
  webpackConfig.optimization.minimizer = []
} else {
  webpackConfig.optimization.minimizer = [
    new UglifyJsPlugin({
      uglifyOptions: {
        compress: {
          warnings: false
        }
      },
      sourceMap: true,
      parallel: true
    })
  ]
  webpackConfig.plugins.splice(3, 0, new CleanWebpackPlugin([output], {
    allowExternal: true,
    exclude: ['zcms.dll.js']
  }))
}

module.exports = webpackConfig
