import HtmlWebpackPlugin from 'html-webpack-plugin'
import HtmlWebpackTemplate from 'html-webpack-template'
import CopyWebpackPlugin from 'copy-webpack-plugin'
import path from 'path'

module.exports = (webpackConfig, env) => {
  const production = process.env.NODE_ENV === 'production';
  // webpackConfig.output.path = path.resolve(__dirname, './dist/static/js/');
  webpackConfig.output.chunkFilename = '[name].[chunkhash].js';

  // https://github.com/pcardune/handlebars-loader
  // https://github.com/emaphp/handlebars-template-loader
  if (webpackConfig.module) {
    webpackConfig.module.rules = webpackConfig.module.rules.concat([
      {
        test: /\.hbs$/,
        loader: 'handlebars-loader',
      }
    ]);
    webpackConfig.module.rules[2].exclude.push(/\.hbs$/)
  }

  webpackConfig.plugins = webpackConfig.plugins.concat([
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, './src/public'),
        to: path.resolve(__dirname, './dist/static'),
      },
    ], {
      copyUnmodified: true,
    }),
    new HtmlWebpackPlugin({
      hash: true,
      mobile: true,
      title: '九毛九SRM系统',
      inject: false,
      appMountId: 'root',
      lang: 'zh-CN',
      template: `!!ejs-loader!${HtmlWebpackTemplate}`,
      // filename: env === production ? '../dist/index.html' : 'index.html',
      minify: {
        collapseWhitespace: true, //删除空白符与换行符
        removeComments: true, //移除HTML中的注释
      },
      // scripts: ['https://cdn.bootcss.com/react/16.2.0/cjs/react.production.min.js'],
      meta: [{
        name: 'description',
        content: '九毛九SRM系统',
      }, {
        name: 'author',
        content: '上海埃林哲软件股份有限公司',
      }, {
        name: 'keywords',
        content: '九毛九SRM系统',
      }, {
        name: 'viewport',
        content: 'width=device-width, initial-scale=1.0',
      }],
    }),

  ]);

  // console.log(webpackConfig.module.rules[5])

  // Alias
  webpackConfig.resolve.alias = {
    '@components': `${__dirname}/src/components`,
    '@bizComponents': `${__dirname}/src/routes/components`,
    '@interfaces': `${__dirname}/src/interfaces`,
    '@utils': `${__dirname}/src/utils`,
    '@config': `${__dirname}/src/utils/config`,
    '@services': `${__dirname}/src/services`,
    '@models': `${__dirname}/src/models`,
    '@routes': `${__dirname}/src/routes`,
    '@themes': `${__dirname}/src/themes`,
    '@public': `${__dirname}/src/public`,
  };

  return webpackConfig;
};
