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


開發時和生產時的配置不同,我們需要將兩者分開配置

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM