var fs = require('fs')
var webpack = require('webpack')
var merge = require('webpack-merge')
var baseConfig = require('./webpack.hwbase.conf')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var cssLoaders = require('./css-loaders')
var ExtractTextPlugin = require('extract-text-webpack-plugin')

// 把examples下所有index.html转换成测试例子.html
fs.readdirSync('./src/components/hanwei').forEach((file) => {
  baseConfig.plugins.push(
    new HtmlWebpackPlugin({
      filename: '../' + file + '.html',
      template: `src/components/hanwei/${file}/index.html`,
      inject: false,
      hash: true
    })
  )
})

// 把examples下子目录里的main.js打包成对应组件名的App.js
fs.readdirSync('./src/components/hanwei').forEach((file) => {
  baseConfig.entry[file + 'App'] = `./src/components/hanwei/${file}/main.js`
})

var SOURCE_MAP = true
module.exports = merge(baseConfig, {
  stats: {
    children: false
  },
  devtool: SOURCE_MAP ? '#source-map' : false,
  output: {
    filename: '[name].js',
    chunkFilename: '[id].[chunkhash].js'
  },
  vue: {
    loaders: cssLoaders({
      sourceMap: SOURCE_MAP,
      extract: true
    })
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false
      }
    }),
    new webpack.optimize.OccurenceOrderPlugin(),
    // extract css into its own file
    new ExtractTextPlugin('[name].css')
  ]
})
