zoukankan      html  css  js  c++  java
  • 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代码部署

    buildase.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

    buildprod.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

    builddev.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

  • 相关阅读:
    LeetCode 45 Jump Game II
    LeetCode 54. Spiral Matrix
    LeetCode 53. Maximum Subarray
    LeetCode 52. N-Queens II
    智齿的秘密
    《婚姻故事》观影笔记
    为什么在linux系统下安装anaconda的时候会报错
    pandas时间序列学习笔记
    极大似然估计和最小二乘法
    粗糙集学习笔记
  • 原文地址:https://www.cnblogs.com/polax/p/13044436.html
Copyright © 2011-2022 走看看