webpack之常用plugin的配置和使用

时间:2020-07-28 22:05:47   收藏:0   阅读:103

概述

webpack中的插件主要是用来完成loader和配置无法完成的事情

常见的几种Plugins

HtmlWebpackPlugin

安装

npm i -D html-webpack-plugin

使用

module.exports = {
    plugins: [
      new HtmlWebpackPlugin({
            // 复制./src/js/index.html 文件
            template: ‘./src/js/index.html‘,
            title: ‘webpack build‘, // template设置时或者使用html loader时 不起作用
            filename: ‘index.html‘,
            minify: {
                collapseWhitespace: true, // 移除空格
                removeComments: true // 删除html文件注释 打包时有效
            }
       })
    ]
}

MiniCssExtractPlugin

安装

npm i -D mini-css-extract-plugin

使用

module.exports = {
  plugins: [
    new MiniCssExtractPlugin({
      filename: ‘[name].[contenthash].css‘,
      chunkFilename: ‘[name].[contenthash].css‘,
    })
  ],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, ‘css-loader‘]
      }
    ]
  }
};

OptimizeCssAssetsWebpackPlugin

安装

npm i -D optimize-css-assets-webpack-plugin

使用

module.exports = {
  plugins: [
    new OptimizeCssAssetsWebpackPlugin(),
  ]
};

NamedChunksPlugin

使用

module.exports = {
  plugins: [
    new webpack.NamedChunksPlugin()
  ]
};

ProvidePlugin

使用

module.exports = {
  plugins: [
    new webpack.ProvidePlugin({ // 
        $: ‘jquery‘,
        jQuery: ‘jquery‘
    })
  ]
};

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!