webpack优化构建速度示例-resolve.extensions:

当项目不仅仅包含.js或.json文件,还包含其他类型文件(如.ts、.vue、.css)作为模块时,配置resolve.extensions可以不必要的文件搜索提高性能。

c 复制代码
src/index.ts
import { someFuction } from './module'

someFuction()
c 复制代码
src/module.ts

import {otherSomeFuction} from './othermodule'


export const someFuction = otherSomeFuction
c 复制代码
src/othermodule.ts
export const otherSomeFuction = () => {
    console.log('otherSomeFuction...')
}

优化前

c 复制代码
webpack.config.js

const config = {
    entry: './src/index.ts',
    output: {
        filename: 'main.js'
    },
  mode: 'development',
  resolve: {  
    extensions: [ '.js', '.json', '.jsx', '.tsx', '.ts'], 
  },  
  module: {
    rules: [
      {
        test: /\.ts$/,
        loader: 'ts-loader'
      }
    ]
  }
}

module.exports = config;

优化后

c 复制代码
webpack.config.js

const config = {
    entry: './src/index.ts',
    output: {
        filename: 'main.js'
    },
  mode: 'development',
  resolve: {  
    extensions: [ '.ts', '.tsx', '.js', '.jsx', '.json'], 
  },  
  module: {
    rules: [
      {
        test: /\.ts$/,
        loader: 'ts-loader'
      }
    ]
  }
}

module.exports = config;

可以看到优化resolve.extensions的顺序,简单的三个小模块的打包就带来了1508-1467=41ms的性能提升

相关推荐
Sayai5 分钟前
【无标题】ECharts 实现日志量异常检测:基线 ±Kσ 基带 + 灵敏度切换(Vue2 实战)
前端·javascript·echarts
用户31693538118328 分钟前
SpringBoot + Vue 项目生产环境部署完整指南
前端·后端
Csvn41 分钟前
🔍 TypeScript `satisfies` 操作符:既要类型安全,又要推断的原始字面量类型
前端
Csvn1 小时前
🚦 手写 Promise 并发控制:如何优雅限制异步请求的并发数?
前端
gb42152871 小时前
python中Web应用服务器
开发语言·前端·python
默_笙1 小时前
💌 为了把"主题色"传给孙子组件,我翻了五层楼——直到遇见了 useContext
前端·javascript
颜进强1 小时前
04 - 一张图看懂 OpenSpec 变更的一生:从创建到归档
前端·后端·ai编程
the_answer1 小时前
JS 垃圾回收机制
前端
小KK_1 小时前
JS 事件循环从小白视角入门:宏任务、微任务与 async/await 一网打尽
前端·javascript
aloha_1 小时前
Linux服务器上指定目录的文件下载
前端