惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
美团技术团队
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
博客园_首页
雷峰网
雷峰网
V
V2EX
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)
量子位
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 聂微东
V
Visual Studio Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Jina AI
Jina AI
月光博客
月光博客
L
LangChain Blog

时间的朋友

Windows 命令行密码重置 Anaconda安装 typescript 注解解读1 Konvajs Shape加载自定义图片 sshpass 使用 why-is-node-running webgl笔记 SharedArrayBuffer is not defined blender 常用快捷键 | 时间的朋友 vue -- v3.4commit提交记录2 vue2 升级vue3报错问题整理 着色器 expressjs 源码 hyper-V arch linux 网络配置 element input数字格式化 three 拼接货架 WebAudio笔记 Windows nginx重启bat脚本 vue -- v3.4commit提交记录 URI malformed vue3 -- Class 对象在组件中使用范例 | 时间的朋友 ruby 安装和升级 element-plus 老版本cascader使用卡死问题 vue3 内置Transition组件 | 时间的朋友 前端memo的实现 | 时间的朋友 Vue -- vue-class-component源码 | 时间的朋友 linux 优化脚本 typescript 装饰器 | 时间的朋友 WSL2问题解决WslRegisterDistribution failed with error: 0x800701bc vue -- vue3利用createVNode函数,建立命令式调用组件 | 时间的朋友
microbundle 源码 | 时间的朋友
2023-08-19 · via 时间的朋友

Published: · LastMod: August 20, 2023 · 543 words

microbundle 源码 🔗

microbundle 是一个零配置的js打包器,基于rollup进行的开发

不需要任何配置文件就可以打包出常用的commonjs, umd, es文件

基础使用 🔗

package.json

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{
  "name": "microbundle-demo",
  "version": "1.0.0",
  "type": "module",
  "main": "./dist/foo.cjs",          
  "module": "./dist/foo.module.js",
  "unpkg": "./dist/foo.umd.js",  
  "scripts": {
    "build": "microbundle src/index.ts",    
    "dev": "microbundle watch" 
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "microbundle": "^0.15.1"
  }
}

然后在自己的入口文件上进行开发

build就可以打包出目标库文件了

源码 🔗

  • 解析配置
1
2
3
4
let options = { ...inputOptions };

options.cwd = resolve(process.cwd(), inputOptions.cwd);
const cwd = options.cwd;
  • 从package.json中读取配置
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const { hasPackageJson, pkg } = await getConfigFromPkgJson(cwd);
	options.pkg = {
		...pkg,
		...pkg.publishConfig,
	};

const { finalName, pkgName } = getName({
  name: options.name,
  pkgName: options.pkg.name,
  amdName: options.pkg.amdName,
  hasPackageJson,
  cwd,
});
  • 读取入口配置、输出配置
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
options.input = await getInput({
		entries: options.entries,
		cwd,
		source: options.pkg.source,
		module: options.pkg.module,
	});

	options.output = await getOutput({
		cwd,
		output: options.output,
		pkgMain: options.pkg.main,
		pkgName: options.pkg.name,
	});

	options.entries = await getEntries({
		cwd,
		input: options.input,
	});

配置最终输出格式,并且把cjs放在第一个

1
2
3
4
5
let formats = (options.format || options.formats).split(',');
	// de-dupe formats and convert "esm" to "es":
	formats = Array.from(new Set(formats.map(f => (f === 'esm' ? 'es' : f))));
	// always compile cjs first if it's there:
	formats.sort((a, b) => (a === 'cjs' ? -1 : a > b ? 1 : 0));

多个入口文件创建多个步骤配置

1
2
3
4
5
6
7
8
	let steps = [];
	for (let i = 0; i < options.entries.length; i++) {
		for (let j = 0; j < formats.length; j++) {
			steps.push(
				createConfig(options, options.entries[i], formats[j], j === 0),
			);
		}
	}

这里并行打包,并保存缓存,这里cache其实是rollup的配置cache

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
let cache;
	let out = await series(
		steps.map(config => async () => {
			const { inputOptions, outputOptions } = config;
			if (inputOptions.cache !== false) {
				inputOptions.cache = cache;
			}
			let bundle = await rollup(inputOptions);
			cache = bundle;
			await bundle.write(outputOptions);
			return await config._sizeInfo;
		}),
	);