webpack插件使用

1.自动生成html页面:

npm install html-webpack-plugin --save-dev

webpack.config.js中添加plugin配置节点

2.搭建本地服务器。

npm install webpack-dev-server --save-dev

webpack.config.js中添加devServer配置节点

最终的webpack.config.js如下:

const path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
// const UglifyjsWebpackPlugin = require("uglifyjs-webpack-plugin")

module.exports = {
  entry: './src/main.js',
  output: {
    //path:'D:\12.vue\01-webpack的使用\02-webpack的配置\dist',
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js',
    //publicPath:'dist/'
  },
  //代码展示方式  production会把代码进行压缩
  mode: 'development', //production,development 设置mode

  module: {
    rules: [
      {
        test: /.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /.(png|jpg|gif)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              name: 'img/[name].[hash:8].[ext]',
              limit: 20000
            }
          }
        ]
      },
      {
        test: /.vue$/,
        use: ['vue-loader']
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  },
  plugins: [
    new VueLoaderPlugin(),
    //自动生成html页面
    new HtmlWebpackPlugin({
      title: 'webpack test',
      template: 'index.html'
    }),
    //版权说明
    new webpack.BannerPlugin('最终版权归fzj所有'),
    //new UglifyjsWebpackPlugin()
  ],
  //设置dev-server目录
  devServer: {
    contentBase: './dist',
    inline: true
  } ,
  //设置js文件大小限制
  performance: {
    hints: 'error', 
    maxAssetSize: 300000, // 整数类型(以字节为单位)
    maxEntrypointSize: 500000 // 整数类型(以字节为单位)
  }
}
View Code

 最终的package.json如下:

{
  "name": "meetwebpack",
  "version": "1.0.0",
  "description": "test",
  "main": "index.js",
  "dependencies": {
    "babel-loader": "^8.1.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "vue": "^2.6.11",
    "webpack": "^4.44.0"
  },
  "devDependencies": {
    "@babel/core": "^7.10.5",
    "css-loader": "^4.0.0",
    "file-loader": "^6.0.0",
    "html-webpack-plugin": "^4.3.0",
    "style-loader": "^1.2.1",
    "uglifyjs-webpack-plugin": "^2.2.0",
    "url-loader": "^4.1.0",
    "vue-loader": "^15.9.3",
    "vue-template-compiler": "^2.6.11",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0"
  },
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1",
    "build": "webpack",
    "dev":"webpack-dev-server --open"
  },
  "author": "fzj",
  "license": "ISC"
}
View Code
原文地址:https://www.cnblogs.com/LittleJin/p/13399603.html