1.1.1 原始阶段(2005-2009)
window.myApp = {}
(function(){
var privateVar = 'secret';
window.myModule = { /*...*/ }
})();
1.1.2 CommonJS规范(2009)
// math.js
exports.add = (a,b) => a + b;
// main.js
const math = require('./math');
math.add(2,3);
1.1.3 AMD/CMD规范(2011)
define(['dep1', 'dep2'], function(d1, d2) {
return { /* 模块内容 */ }
});
1.1.4 ES Modules(2015+)
<script type="module">
import { func } from './module.js';
script>
2.1.1 核心组件协作流程
2.1.2 模块解析机制
resolve.alias
)resolve.extensions
)module.rules
)2.1.3 依赖图谱构建
Entry: src/index.js
├── node_modules/react/index.js
│ └── react-dom/client.js
└── components/App.jsx
└── utils/helper.js
2.2.1 构建速度优化
module.exports = {
// 持久化缓存
cache: {
type: 'filesystem',
version: createEnvironmentHash(env.raw),
buildDependencies: {
config: [__filename],
},
},
// 多进程处理
module: {
rules: [{
test: /\.js$/,
use: [
{
loader: 'thread-loader',
options: { workers: 4 }
},
'babel-loader'
]
}]
},
// 资源压缩并行
optimization: {
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
compress: { drop_console: true }
}
})
]
}
}
2.2.2 代码分割策略
optimization: {
splitChunks: {
chunks: 'all',
minSize: 20000,
maxSize: 70000,
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
reuseExistingChunk: true,
},
common: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
},
},
runtimeChunk: 'single',
}
2.2.3 高级优化技巧
Tree Shaking深度配置
optimization: {
usedExports: true,
sideEffects: true,
concatenateModules: true
}
模块联邦实战
// app1/webpack.config.js
new ModuleFederationPlugin({
name: 'app1',
filename: 'remoteEntry.js',
exposes: {
'./Button': './src/components/Button.jsx'
}
});
// app2/webpack.config.js
new ModuleFederationPlugin({
remotes: {
app1: 'app1@http://localhost:3001/remoteEntry.js'
}
});
3.1.1 开发环境架构
3.1.2 关键技术创新
依赖预构建流程
node_modules/.vite
)按需编译机制
// 中间件处理示例
app.use(async (ctx, next) => {
if (ctx.path.endsWith('.jsx')) {
const code = await fs.readFile(ctx.path);
ctx.body = transformJSX(code);
}
await next();
});
3.2.1 自定义插件开发
// vite-plugin-example.ts
export default function examplePlugin(): Plugin {
return {
name: 'vite-plugin-example',
transform(code, id) {
if (id.endsWith('.custom')) {
return { code: compileCustomFormat(code) }
}
},
configureServer(server) {
server.middlewares.use((req, res, next) => {
console.log(`Request: ${req.url}`);
next();
});
}
}
}
3.2.2 性能优化策略
// vite.config.js
export default defineConfig({
build: {
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes('node_modules')) {
return 'vendor';
}
}
}
},
sourcemap: 'hidden',
minify: 'terser',
},
server: {
preTransformRequests: true,
}
});
维度 | Webpack优势场景 | Vite优势场景 |
---|---|---|
项目规模 | 大型复杂应用 | 中小型快速迭代项目 |
模块规范 | 混合模块系统 | 纯ESM项目 |
构建需求 | 深度定制打包流程 | 开箱即用 |
开发体验 | 成熟生态 | 极速HMR |
4.2.1 迁移步骤
安装基础依赖:
npm install vite vite-plugin-react @vitejs/plugin-legacy --save-dev
配置文件转换:
// vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
'@': path.resolve(__dirname, './src')
}
}
});
处理特殊资源:
// 处理Webpack特殊语法
import.meta.glob('../../assets/*.png');
4.2.2 常见问题解决方案
Node环境变量问题
// 替换process.env
import.meta.env.MODE
CSS Modules兼容
// vite.config.js
css: {
modules: {
localsConvention: 'camelCase'
}
}