webpack区分打包类库代码及代码优化

1.配置文件webpack.config

const path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const HTMLPlugin = require('html-webpack-plugin')
const webpack = require('webpack')
const ExtractPlugin = require('extract-text-webpack-plugin')

const isDev = process.env.NODE_ENV =='development'
const config= {
  target:'web',
  entry: path.join(__dirname, "src/index.js"), //输入文件
  output: {
    filename: "build.[hash:8].js", //输出文件
    path: path.join(__dirname, 'dist') //输出路径
  },
  module:{
    rules:[
      {
        test:/\.vue$/,
        loader:'vue-loader'
      },{
        test: /\.jsx$/,
        loader: 'babel-loader'
      },
      {
        test:/\.(gif|png|jpg|jpeg|svg)$/,
        use:[{
          loader:'url-loader',
          options:{
            limit:1024,
            name:'[name]-[hash].[ext]'
          }
        }]
      },
     
    ]
  },
  plugins: [
    // make sure to include the plugin for the magic
    new VueLoaderPlugin(),
    new webpack.DefinePlugin({
      'process.env':{
        NODE_ENV:isDev?'"development"':'"production"',
      }
    }),
    new HTMLPlugin(),
  ],
}

if(isDev){
  config.module.rules.push({
       test: /\.styl/,
       use: ['style-loader', 'css-loader', {
         loader: 'postcss-loader',
         options: {
           sourceMap: true
         }
       }, 'stylus-loader']
  })
  config.devtool='#cheap-module-eval-source-map',
  config.devServer={
    port:8000,
    host:'0.0.0.0',
    overlay:{
      errors:true
    },
    // open:true   //每次都打开一个网页
    hot:true //只渲染一个组件
  }
  config.plugins.push(
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
  )
}else{
  config.entry={
    app:path.join(__dirname,'src/index.js'),
    vendor:['vue']
  }
  config.output.filename = '[name].[chunkhash:8].js'
  config.module.rules.push(
     {
       test: /\.styl/,
       use:ExtractPlugin.extract({
         fallback:'style-loader',
         use:[
            'css-loader', 
            {
              loader: 'postcss-loader',
              options: {
                sourceMap: true
              }
            },
            'stylus-loader'
         ]
       })
     }
  )
  config.optimization={
      splitChunks: {
        cacheGroups: {
          commons: {
            name: "vendor",
            chunks: "initial",
            minChunks: 2
          }
        }
      }
    }
 config.optimization = {
      splitChunks: {
        cacheGroups: {
          commons: {
            name: "runtime",
            chunks: "initial",
            minChunks: 2
          }
        }
      }
    }
  config.plugins.push(
    new ExtractPlugin('styles.[name].css'),
    // new webpack.optimize.CommonsChunkPlugin({
    //   name:'vendor'
    // })
  )
}
module.exports=config

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 写在开头 先说说为什么要写这篇文章, 最初的原因是组里的小朋友们看了webpack文档后, 表情都是这样的: (摘...
    Lefter阅读 5,425评论 4 31
  • 目录第1章 webpack简介 11.1 webpack是什么? 11.2 官网地址 21.3 为什么使用 web...
    lemonzoey阅读 1,814评论 0 1
  • GitChat技术杂谈 前言 本文较长,为了节省你的阅读时间,在文前列写作思路如下: 什么是 webpack,它要...
    萧玄辞阅读 12,838评论 7 110
  • 记得2004年的时候,互联网开发就是做网页,那时也没有前端和后端的区分,有时一个网站就是一些纯静态的html,通过...
    阳阳阳一堆阳阅读 3,438评论 0 5
  • 左邻右舍 文/黄影 仿佛若干个箱体 在一起堆积 在一起挤压 挤得那么地近 近得你我他 在咫尺内 把一颗颗心放下 然...
    黄影诗风阅读 861评论 14 33

友情链接更多精彩内容