大型 SPA 项目的模块化构建,也就是常说的 web 应用。
loader 处理各种各样的静态资源plugins 对整体文件进行一些处理Code splitting 将公共模块进行提取webpack-dev-server ,进行本地开发HMR 模块热替换Rollup 设计之初就是面向 ES Module 的,构建出结构扁平,性能出众的类库。
ES Module 的规则
import 只能作为模块顶层的语句出现,不能出现在 function 里或者 if 里。import 的模块名只能是字符串常量。import 的语句位置出现在哪里,在模块初始化的时候,所有的 import 都必须导入完成。使用工具静态分析的过程
Tree shaking ,摇树,让死了的叶子掉下来。ES Module 打包生成特定的 JS 模块文件,并最大程度的减小体积。通过以上的对比可以得出,构建App应用时,webpack 比较合适,如果是类库(纯js项目),rollup 更加合适。
HMR,按需加载,公共模块提取。web 开发的环节,图片自动转 base64,资源的缓存(添加 chunkID)。ES Module 的特性。build 那一个文件。node_modules 中的模块,找到使用的模块。js 文件。esm 模块,可以被最新的浏览器直接使用。bundle,全浏览器兼容的代码。ES Module,并且提供支持 typescript 的 type 文件。UMD默认的配置文件
// 打包 vue 文件
import vue from 'rollup-plugin-vue'
// 打包 css
import css from 'rollup-plugin-css-only'
// 打包 ts 文件
import ts from 'rollup-plugin-typescript2'
// 打包依赖
import { nodeResolve } from '@rollup/plugin-node-resolve'
// 文件名称
import { name } from './package.json'
// 文件
const file = type => `dist/${name}.${type}.js`
export { name, file }
// 开启 ts 的类型声明文件打包
const overrides = {
compilerOptions: {
declaration: true
},
exclude: ['src/main.ts', 'node_modules', 'src/App.vue']
}
export default {
input: "src/index.ts",
output: {
name,
file: file('esm'),
format: "es"
},
plugins: [
nodeResolve(),
vue(),
css({ output: 'bundle.css' }),
ts({ tsconfigOverride: overrides })
],
// 排除需要打包的第三方库
// external: (id) => {
// return /^vue/.test(id)
// }
external: ['vue', 'lodash-es']
}生成 umd 规范的组件库
import baseConfig, { file } from './rollup.config'
export default {
...baseConfig,
output: {
name: 'LegoComponents',
file: file('umd'),
format: 'umd',
// 第三方库的全局变量名称
globals: {
'vue': "Vue",
'lodash-es': '_'
},
// 组件库的全局变量名称
exports: 'named'
},
}生成 esm 规范的组件库
import baseConfig, { name, file } from './rollup.config'
export default {
...baseConfig,
output: {
name,
file: file('esm'),
format: 'es'
}
}package.json 内容
build, build:esm, build:umd 命令peerDependencies 用于第三方库,前置依赖,本地不安装devDependencies,来进行本地安装{
"name": "lego-component",
"version": "1.0.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "npm run clean && npm run build:esm && npm run build:umd",
"lint": "vue-cli-service lint",
"build:esm": "rollup --config rollup.esm.config.js",
"build:umd": "rollup --config rollup.umd.config.js",
"clean": "rimraf ./dist"
},
"peerDependencies": {
"vue": "^3.2.37"
},
"dependencies": {
"ant-design-vue": "^3.2.6",
"core-js": "^3.6.5",
"lodash-es": "^4.17.21"
},
"devDependencies": {
"vue": "^3.2.37"
}
}