小白兔晒黑了

导航

 

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

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代码部署

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

build\prod.config.js

生产时配置,需要压缩代码

/**
 * 生产时依赖
 * @type {{plugins: [webpack.BannerPlugin]}}
 */
// const uglifyjsWebpackPlugin = require('uglifyjs-webpack-plugin')
const webpackMerge = require('webpack-merge')
const baseConfig = require('./base.config')

module.exports = webpackMerge(baseConfig,{
  plugins:[
   // new uglifyjsWebpackPlugin()
  ],
  optimization: {
    minimize: true //打开压缩丑化
  },
})


/*module.exports = {
  plugins:[
    new uglifyjsWebpackPlugin()
  ]
}*/
View Code

build\dev.config.js

开发时需要配置服务器

/**
 * 开发时依赖
 */
const webpackMerge = require('webpack-merge')
const baseConfig = require('./base.config')

module.exports = webpackMerge(baseConfig,{
  devServer: {
    contentBase:"./dist",
    inline:true
  },
  optimization: {
    minimize: false //取消压缩丑化
  },
})
/*
module.exports = {
  devServer: {
    contentBase:"./dist",
    inline:true
  },
}
*/
View Code

使用

生产时:npn run build

开发时:npm run dev

posted on 2020-06-04 16:34  小白兔晒黑了  阅读(1216)  评论(0编辑  收藏  举报