联系客服
客服二维码

联系客服获取更多资料

微信号:LingLab1

客服电话:010-82185409

意见反馈
关注我们
关注公众号

关注公众号

linglab语言实验室

回到顶部
webpack进阶,如何摆脱cli工程师头衔

146 阅读 2020-08-17 09:15:02 上传

以下文章来源于 西语语言学工作坊

基本配置

拆分配置和merge

webpack.common.js

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { srcPath, distPath } = require('./paths')

module.exports = {
    entry: path.join(srcPath, 'index'),
    module: {
        rules: []
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: path.join(srcPath, 'index.html'),
            filename'index.html'
        })
    ]
}

webpack.dev.js

const path = require('path')
const webpack = require('webpack')
const webpackCommonConf = require('./webpack.common.js')
const { smart } = require('webpack-merge')
const { srcPath, distPath } = require('./paths')

module.exports = smart(webpackCommonConf, {
    mode'development',
    module: {
        rules: []
    },
    plugins: [
        new webpack.DefinePlugin({
            // window.ENV = 'development'
            ENV: JSON.stringify('development')
        })
    ],
})

webpack.prod.js

const path = require('path')
const webpack = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const webpackCommonConf = require('./webpack.common.js')
const { smart } = require('webpack-merge')
const { srcPath, distPath } = require('./paths')

module.exports = smart(webpackCommonConf, {
    mode'production',
    output: {
        filename'bundle.[contentHash:8].js',  // 打包代码时,加上 hash 戳
        path: distPath,
    },
    module: {
        rules: []
    },
    plugins: [
        new CleanWebpackPlugin(), // 会默认清空 output.path 文件夹
        new webpack.DefinePlugin({
            // window.ENV = 'production'
            ENV: JSON.stringify('production')
        })
    ]
})

path.js

const path = require('path')

const srcPath = path.join(__dirname, '.''src')
const distPath = path.join(__dirname, '.''dist')

module.exports = {
    srcPath,
    distPath
}

package.json

{
  "name""07-webpack-demo",
  "version""1.0.0",
  "description""",
  "main""index.js",
  "scripts": {
    "test""echo \"Error: no test specified\" && exit 1",
    "devBuild""webpack --config webpack.dev.js",
    "dev""webpack-dev-server --config webpack.dev.js",
    "build""webpack --config webpack.prod.js"
  },
  "keywords": [],
  "author""",
  "license""ISC",
  "devDependencies": {
    "@babel/core""^7.7.4",
    "@babel/preset-env""^7.7.4",
    "autoprefixer""^9.7.3",
    "babel-loader""^8.0.6",
    "clean-webpack-plugin""^3.0.0",
    "css-loader""^3.2.1",
    "file-loader""^5.0.2",
    "happypack""^5.0.1",
    "html-webpack-plugin""^3.2.0",
    "less""^3.10.3",
    "less-loader""^5.0.0",
    "mini-css-extract-plugin""^0.8.0",
    "optimize-css-assets-webpack-plugin""^5.0.3",
    "postcss-loader""^3.0.0",
    "style-loader""^1.0.1",
    "terser-webpack-plugin""^2.2.2",
    "url-loader""^3.0.0",
    "webpack""^4.41.2",
    "webpack-cli""^3.3.10",
    "webpack-dev-server""^3.9.0",
    "webpack-merge""^4.2.2",
    "webpack-parallel-uglify-plugin""^1.1.2"
  },
  "dependencies": {
    "lodash""^4.17.15",
    "moment""^2.24.0"
  }
}

启动本地服务

webpack.dev.js

{
 devServer: {
        port8080,
        progresstrue,  // 显示打包的进度条
        contentBase: distPath,  // 根目录
        open: true,  // 自动打开浏览器
        compress: true,  // 启动 gzip 压缩

        // 设置代理
        proxy: {
            // 将本地 /api/xxx 代理到 localhost:3000/api/xxx
            '/api''http://localhost:3000',
            // 将本地 /api2/xxx 代理到 localhost:3000/xxx
            '/api2': {
                target'http://localhost:3000',
                pathRewrite: {
                    '/api2'''
                }
            }
        }
    } 
}

处理ES6

webpack.common.js

{
    module: {
        rules: [
            {
                test/\.js$/,
                loader: ['babel-loader'],
                include: srcPath,
                exclude/node_modules/
            }
        ]
    }
}

.babelrc

{
    "presets":["@babel/preset-env"],
    "plugins":[]
}

处理样式

webpack.common.js

{
 module: {
        rules: [
            {
                test/\.css$/,
                // loader 的执行顺序是:从后往前
                loader: ['style-loader''css-loader''postcss-loader'// 加了 postcss
            },
            {
                test/\.less$/,
                // 增加 'less-loader' ,注意顺序
                loader: ['style-loader''css-loader''less-loader']
            }
        ]
   }
}

postcss.config.js

module.exports = {
    plugins: [require('autoprefixer')]
}

处理图片

webpack.dev.js

{
    module: {
        rules: [
            // 直接引入图片 url
            {
                test/\.(png|jpg|jpeg|gif)$/,
                use'file-loader'
            }
        ]
    }
}

webpack.prod.js

{
    module: {
        rules: [
            // 图片 - 考虑 base64 编码的情况
            {
                test/\.(png|jpg|jpeg|gif)$/,
                use: {
                    loader'url-loader',
                    options: {
                        // 小于 5kb 的图片用 base64 格式产出
                        // 否则,依然延用 file-loader 的形式,产出 url 格式
                        limit: 5 * 1024,
                        // 打包到 img 目录下
                        outputPath: '/img/',
                    }
                }
            },
        ]
    }
}

高级配置

多入口

webpack.common.js

{
    entry: {
        index: path.join(srcPath, 'index.js'),
        other: path.join(srcPath, 'other.js')
    },
    plugins: [
        // 多入口 - 生成 index.html
        new HtmlWebpackPlugin({
            template: path.join(srcPath, 'index.html'),
            filename'index.html',
            // chunks 表示该页面要引用哪些 chunk (即上面的 index 和 other),默认全部引用
            chunks: ['index']  // 只引用 index.js
        }),
        // 多入口 - 生成 other.html
        new HtmlWebpackPlugin({
            template: path.join(srcPath, 'other.html'),
            filename'other.html',
            chunks: ['other']  // 只引用 other.js
        })
    ]
}

webpack.prod.js

{
    output: {
        filename'[name].[contentHash:8].js'// name 即多入口时 entry 的 key
        path: distPath,
    }
}

抽离CSS文件

webpack.common.js删除css-loader的rules

webpack.dev.js

{
 module: {
        rules: [
            {
                test/\.css$/,
                // loader 的执行顺序是:从后往前
                loader: ['style-loader''css-loader''postcss-loader'// 加了 postcss
            },
            {
                test/\.less$/,
                // 增加 'less-loader' ,注意顺序
                loader: ['style-loader''css-loader''less-loader']
            }
        ]
   }
}

webpack.prod.js

const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const TerserJSPlugin = require('terser-webpack-plugin')
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')

{
    module: {
        rules: [
            // 抽离 css
            {
                test/\.css$/,
                loader: [
                    MiniCssExtractPlugin.loader,  // 注意,这里不再用 style-loader
                    'css-loader',
                    'postcss-loader'
                ]
            },
            // 抽离 less --> css
            {
                test/\.less$/,
                loader: [
                    MiniCssExtractPlugin.loader,  // 注意,这里不再用 style-loader
                    'css-loader',
                    'less-loader',
                    'postcss-loader'
                ]
            }
        ]
    },
    plugins: [
        // 抽离 css 文件
        new MiniCssExtractPlugin({
            filename'css/main.[contentHash:8].css'
        })
    ],
    optimization: {
        // 压缩 css
        minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
    }
}

抽离公共代码

webpack.common.js

{
    plugins: [
        // 多入口 - 生成 index.html
        new HtmlWebpackPlugin({
            template: path.join(srcPath, 'index.html'),
            filename'index.html',
            // chunks 表示该页面要引用哪些 chunk (即上面的 index 和 other),默认全部引用
            chunks: ['index''vendor''common']  // 要考虑代码分割
        }),
        // 多入口 - 生成 other.html
        new HtmlWebpackPlugin({
            template: path.join(srcPath, 'other.html'),
            filename'other.html',
            chunks: ['other''common']  // 考虑代码分割
        })
    ]
}

webpack.prod.js

{
    optimization: {
        // 分割代码块
        splitChunks: {
            chunks'all',
            /**
             * initial 入口 chunk,对于异步导入的文件不处理
                async 异步 chunk,只对异步导入的文件处理
                all 全部 chunk
             */


            // 缓存分组
            cacheGroups: {
                // 第三方模块
                vendor: {
                    name'vendor'// chunk 名称
                    priority: 1// 权限更高,优先抽离,重要!!!
                    test: /node_modules/,
                    minSize0,  // 大小限制
                    minChunks: 1  // 最少复用过几次
                },

                // 公共的模块
                common: {
                    name'common'// chunk 名称
                    priority: 0// 优先级
                    minSize: 0,  // 公共模块的大小限制
                    minChunks: 2  // 公共模块最少复用过几次
                }
            }
        }
    }
}

懒加载

import('./a.js').then(res => {} )

处理JSX

npm install --save-dev @babel/preset-react

.babelrc

{
 "presets":["@babel/preset-react"],
}

处理Vue

npm i vue-loader

webpack.common.js

{
    module: {
        rules: [
            {
                test/\.vue$/,
                loader: ['vue-loader'],
                include: srcPath
            }
        ]
    }
}

module、chunk、bundle的区别

  • module-各个源码文件,webpack中一切皆模块
  • chunk-多模块合并成的,如entry、import()、splitChunk
  • Bundle-最终的输出文件

优化构建速度

优化babel-loader

webpack.common.js

{
    module: {
        rules: [
            {
                test/\.js$/,
                loader: ['babel-loader?cacheDirectory'],
                include: srcPath,
                // exclude: /node_modules/
            }
        ]
    }
}

IgnorePlugin

webpack.prod.js

{
    plugins: [
        // 忽略 moment 下的 /locale 目录
        new webpack.IgnorePlugin(/\.\/locale/, /moment/),
    ]
}

noParse

webpack.prod.js

{
 module:{
  noParse:/jquery/,//不去解析jquery中的依赖库
 }
}

happyPack

  • JS单线程,开启多进程打包
  • 提高构建速度(特别是多核CPU)

webpack.common.js删除babel-loader的rules

webpack.dev.js

{
    module: {
        rules: [
            {
                test/\.js$/,
                loader: ['babel-loader?cacheDirectory'],
                include: srcPath,
                // exclude: /node_modules/
            }
        ]
    }
}

webpack.prod.js

const HappyPack = require('happypack')

{
    module: {
        rules: [
            // js
            {
                test/\.js$/,
                // 把对 .js 文件的处理转交给 id 为 babel 的 HappyPack 实例
                use: ['happypack/loader?id=babel'],
                include: srcPath,
                // exclude: /node_modules/
            }
        ]
    },
    plugins: [
        // happyPack 开启多进程打包
        new HappyPack({
            // 用唯一的标识符 id 来代表当前的 HappyPack 是用来处理一类特定的文件
            id: 'babel',
            // 如何处理 .js 文件,用法和 Loader 配置中一样
            loaders: ['babel-loader?cacheDirectory']
        }),
    ]
}

ParallelUglifyPlugin

  • webpack内置Uglify工具压缩JS
  • JS单线程,开启多线程压缩更快
  • 和happyPack同理

webpack.prod.js

const ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin')

{
    plugins: [
        // 使用 ParallelUglifyPlugin 并行压缩输出的 JS 代码
        new ParallelUglifyPlugin({
            // 传递给 UglifyJS 的参数
            // (还是使用 UglifyJS 压缩,只不过帮助开启了多进程)
            uglifyJS: {
                output: {
                    beautifyfalse// 最紧凑的输出
                    comments: false// 删除所有的注释
                },
                compress: {
                    // 删除所有的 `console` 语句,可以兼容ie浏览器
                    drop_console: true,
                    // 内嵌定义了但是只用到一次的变量
                    collapse_vars: true,
                    // 提取出出现多次但是没有定义成变量去引用的静态值
                    reduce_vars: true,
                }
            }
        })
    ]
}

自动刷新

配置devServer会自动开启,无需配置

webpack.dev.js

{
 watchtrue // 开启监听,默认为false
   // 开启监听后,webpack-dev-server会自动开启刷新浏览器
   // 监听配置
   watchOptions: {
      ignored/node_modules/// 忽略哪些
      // 监听到变化发生后会等300ms再去执行动作,防止文件更新太快导致重新编译频率太高
      aggregateTimeout: 300// 默认为300ms
      // 判断文件是否发生变化是通过不停的去询问系统指定文件有没有变化实现的
      poll: 1000 // 默认每隔1000ms询问一次
    }
}

热更新

  • 自动刷新:整个网页全部刷新,速度较慢
  • 自动刷新:整个网页全部刷新,状态会丢失
  • 热更新:新代码生效,网页不刷新,状态不丢失

webpack.dev.js

const HotModuleReplacementPlugin = require('webpack/lib/HotModuleReplacementPlugin');

{
    entry: {
        // index: path.join(srcPath, 'index.js'),
        index: [
            'webpack-dev-server/client?http://localhost:8080/',
            'webpack/hot/dev-server',
            path.join(srcPath, 'index.js')
        ],
        other: path.join(srcPath, 'other.js')
    },
    plugins: [
        new HotModuleReplacementPlugin()
    ],
    devServer: {
        hottrue
    }
}
// 开启热更新之后的代码逻辑
if(module.hot){
  module.hot.accept(["./a.js"],() => {
    console.log("hot")
  })
}

DllPlugin动态链接库插件

  • 前端框架如Vue、React,体积大,构建慢

  • 较稳定,不常升级版本

  • 同一个版本只构建一次即可,不用每次都重新构建

  • webpack已内置DllPlugin支持

  • DllPlugin打包出dll文件

  • DllReferencePlugin使用dll文件

webpack.dll.js

const path = require('path')
const DllPlugin = require('webpack/lib/DllPlugin')
const { srcPath, distPath } = require('./paths')

module.exports = {
  mode'development',
  // JS 执行入口文件
  entry: {
    // 把 React 相关模块的放到一个单独的动态链接库
    react: ['react''react-dom']
  },
  output: {
    // 输出的动态链接库的文件名称,[name] 代表当前动态链接库的名称,
    // 也就是 entry 中配置的 react 和 polyfill
    filename: '[name].dll.js',
    // 输出的文件都放到 dist 目录下
    path: distPath,
    // 存放动态链接库的全局变量名称,例如对应 react 来说就是 _dll_react
    // 之所以在前面加上 _dll_ 是为了防止全局变量冲突
    library: '_dll_[name]',
  },
  plugins: [
    // 接入 DllPlugin
    new DllPlugin({
      // 动态链接库的全局变量名称,需要和 output.library 中保持一致
      // 该字段的值也就是输出的 manifest.json 文件 中 name 字段的值
      // 例如 react.manifest.json 中就有 "name": "_dll_react"
      name: '_dll_[name]',
      // 描述动态链接库的 manifest.json 文件输出时的文件名称
      path: path.join(distPath, '[name].manifest.json'),
    }),
  ],
}

index.html

"./react.dll.js">script>

webpack.dev.js

// 第一,引入 DllReferencePlugin
const DllReferencePlugin = require('webpack/lib/DllReferencePlugin');

{
    module: {
        rules: [
            {
                test/\.js$/,
                loader: ['babel-loader'],
                include: srcPath,
                exclude/node_modules/ // 第二,不要再转换 node_modules 的代码
            },
        ]
    },
    plugins: [
        // 第三,告诉 Webpack 使用了哪些动态链接库
        new DllReferencePlugin({
            // 描述 react 动态链接库的文件内容
            manifest: require(path.join(distPath, 'react.manifest.json')),
        }),
    ]
}

优化产出代码

  • 小图片base64编码

  • bundle加hash

  • 懒加载

  • 提取公共代码

  • IgnorePlugin

  • 使用CDN加速

  • 使用production

    • ES6 Module静态引入,编译时引入
    • Commonjs动态引入,执行时引入
    • 只有ES6 Module才能静态分析,实现Tree-Shaking
  1. 自动开启代码压缩
  2. Vue React等会自动删掉调试代码(如开发环境的warning)
  3. 启动Tree-Shaking
  • Scope Hosting

  1. 代码体积更小
  2. 创建函数作用域更少
  3. 代码可读性更好

babel

环境搭建和基本配置

  • 环境搭建

package.json

{
  "name""09-babel-demo",
  "version""1.0.0",
  "description""",
  "main""index.js",
  "scripts": {
    "test""echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author""",
  "license""ISC",
  "devDependencies": {
    "@babel/cli""^7.7.5",
    "@babel/core""^7.7.5",
    "@babel/plugin-transform-runtime""^7.7.5",
    "@babel/preset-env""^7.7.5"
  },
  "dependencies": {
    "@babel/polyfill""^7.7.0",
    "@babel/runtime""^7.7.5"
  }
}
  • .babelrc配置
  • presets和plugins

.babelrc

{
 "presets":["@babel/preset-env"],
  "plugins":[]
}

babel-polyfill

  • 什么是polyfill
  • core-js和regenerator
  • babel-polyfill即两者的集合
  • babel7.4之后弃用babel-polyfill
  • babel-polyfill按需引入
  1. 文件较大
  2. 只有一部分功能,无需全部引入
  3. 配置按需引入

.babelrc

{
 "presets":[
    [
      "@babel/preset-env",
      {
        "useBuiltIns""usage",
        "corejs"3
      }
    ]
  ],
  "plugins":[]
}
  • babel-polyfill的问题
  1. 会污染全局环境
  2. 如果做一个独立的web系统,则无碍
  3. 如果做一个第三方lib,则会有问题

babel-runtime

.babelrc

{
  "plugins":[
    [
      "@babel/plugin-transform-runtime",
      {
        "absoluteRuntime"false,
        "corejs"3,
        "helpers"true,
        "regenerator"true,
        "useESModules"false
      }
    ]
  ]
}

点赞
收藏
表情
图片
附件