示例工程 16-mini-bundler
16-mini-bundler/01-mini-bundler/README.md
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
# 16 · 迷你 bundler
**演示**:7 个 CJS 模块(含 A↔B 循环依赖)经 `scripts/mini-bundler/build.mjs` 打包成一个 约 2KB的产物;`index.html` 用 `<script>` 直接引入并运行。
**怎么看**:
```sh
# 重新生成产物(可选;仓库里已提交当前产物)
node scripts/mini-bundler/build.mjs \
examples/16-mini-bundler/01-mini-bundler/src/main.js \
examples/16-mini-bundler/01-mini-bundler/dist/bundle.js \
--root examples/16-mini-bundler/01-mini-bundler
node scripts/serve.mjs
```
打开 <http://localhost:8080/examples/16-mini-bundler/01-mini-bundler/>,Console 与页面 `<pre>` 都会显示结果。
**预期输出**:
```text
bundle.run() => {
"banner": "[MINI-BUNDLER@1.0.0]",
"sameUtils": true,
"cycle": {
"a": "a",
"bName": "b",
"bSawA": "a"
}
}
banner:[MINI-BUNDLER@1.0.0]
共享 utils 引用:true
循环依赖视图:{"a":"a","bName":"b","bSawA":"a"}
产物由 scripts/mini-bundler/build.mjs 生成,index.html 也可以直接用 <script> 引入。
```
**注意**:迷你 bundler 用正则提取 `require('...')`,会误伤字符串/注释里的 require;只支持相对路径,不解析 `node_modules`,不做 ESM、tree-shaking、source map。它是教学实现,不是生产工具。循环依赖的结果与 CJS 一致(`b` 看到尚未完成的 `a`),产物每次求值都新建缓存。需 `http://` 协议。