开发时和生产时的配置不同,我们需要将两者分开配置

1安装webpack-merge

用于将配置文件进行合并

npm install webpack-merge

2配置(手动指定config)

package.json

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --config ./build/prod.config.js",
    "dev": "webpack-dev-server --open --config ./build/dev.config.js"
  },

3代码部署

webpack配置文件的分离 和webpack-merge的使用

build\base.config.js

公共配置

const path = require('path')
const webpack = require('webpack')
const htmlWebpackPlugin =require('html-webpack-plugin')

module.exports = {
  optimization: {
    minimize: false//取消压缩丑化
  },

  entry : './src/main.js',
  output:{
    path : path.resolve(__dirname,'../dist'), //路径拼接 //必须是绝对路径
    filename:'bundle.js',
    //publicPath:'dist/'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.less$/,
        use: [{
          loader: "style-loader" // creates style nodes from JS strings
        }, {
          loader: "css-loader" // translates CSS into CommonJS
        }, {
          loader: "less-loader" // compiles Less to CSS
        }]
      },
      {
        test: /\.(png|jpg|gif|jpeg)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              //当加载的图片小于limit时 回编译成base-64
              limit: 13000,
              //[name] 原来图片的名字
              //[hash:8] hash值截取8位
              //[ext] 扩展名
              name:'img/[name].[hash:8].[ext]'
            },

          }
        ]
      },



      {
        test: /\.js$/,
        //排除这连个文件夹
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            //presets: ['@babel/preset-env']
            presets: ['es2015']
          }
        }
      }
      ,
      {
        test: /\.vue$/,
        use: ['vue-loader']
      },



    ]
  },
  resolve:{
    alias:{
      'vue$':'vue/dist/vue.esm.js'
    }
  },
  plugins:[
    new webpack.BannerPlugin('最终版权归coderTTT所有'),
    new htmlWebpackPlugin({
      template:'index.html'
    }),
  ]

}
View Code

相关文章:

  • 2022-02-23
  • 2022-12-23
  • 2021-10-04
  • 2021-09-24
  • 2022-01-26
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-20
  • 2021-06-14
  • 2021-12-23
  • 2021-10-20
  • 2023-02-07
相关资源
相似解决方案