forked from nsjcy/frontEnd/nsjcy

1
liuwh
2020-05-27 6fe0bbe094b843b9ff3a29649c6f786ee6261a89
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
module.exports = function (target, basepath, htmls) {
 
  const plugins = htmls.map(({ name, html }) => new HtmlWebpackPlugin({
    template: html,
    chunks: [name],
    filename: `${name}.html`,
    minify: true
  }))
  plugins.push(new MiniCssExtractPlugin({
    filename: "[name].css?[hash]"
  }))
  if (fs.existsSync(path.join(basepath, 'src', 'copy'))) {
    plugins.push(new CopyWebpackPlugin([{
      to: path.join(basepath, target),
      from: path.join(basepath, 'src', 'copy'),
      toType: 'dir'
    }]));
  }
  if (target === 'dist') {
    plugins.push(new CleanWebpackPlugin(['dist'], {
      root: basepath
    }));
  } else {
    plugins.push(new webpack.HotModuleReplacementPlugin());
  }
 
  return plugins;
};