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

  • 相关阅读:
    java.lang.NoSuchMethodError: org.springframework.beans.factory.annotation.InjectionMetadata.<init>(Ljava/lang/Class;)V
    tomcat下jndi的三种配置方式
    使用@RequestParam绑定请求参数到方法参数
    IE下easyui 缓存问题
    many to one could not resolve property
    aop郁闷错误
    Spring 运用 pointcut 和 advisor 对特定的方法进行切面编程
    放大改进版~
    cocos2d-x嵌入移动MM短代支付IAP2.4的SDK,点击支付崩溃的解决的方法
    在 Android* 商务应用中实施地图和地理围栏特性
  • 原文地址:https://www.cnblogs.com/polax/p/13044436.html
Copyright © 2011-2022 走看看