unbuild

介绍

unbuild 通常用于做:工具库、npm 包、Vue 插件,是一个专门为 npm 库设计的构建工具。

特点是:

  1. 自动输出 ESM + CJS
  2. 自动生成类型
  3. 零配置
  4. 适合库开发
  5. unbuild 底层用的是 Rollup,所有 node_modules 默认 external。

配置

js 复制代码
import { defineBuildConfig } from 'unbuild';

export default defineBuildConfig({
  // 每次构建前,先删除 dist 目录
  clean: true,
  // 生成 TypeScript 类型声明文件 (.d.ts)
  declaration: true,
  // 以 src/index.ts 作为入口文件
  entries: ['src/index'],
  rollup: { emitCJS: true }
});

打包产物分析

打包产物

js 复制代码
index.cjs
index.mjs
index.d.ts
index.d.mts
index.d.cts
  1. index.mjs
  • ES Module 版本,老 Node 项目或老工具用。
  • 用于import { xxx } from 'your-lib'
  1. index.cjs
  • CommonJS 版本
  • 用于const lib = require('your-lib')
  1. index.d.ts
  • 通用 TypeScript 类型声明,
  • 默认类型入口
  1. index.d.mts
  • ESM 模块的类型声明
  • 用于type: "module" 或者 Node ESM 解析场景。
  • 和 d.ts 的区别:明确声明是 ESM 类型
  1. index.d.cts
  • CommonJS 类型声明
  • 在严格 CJS + TS 模式下用。

三种类型文件

js 复制代码
index.d.ts   ← 最重要
index.mjs    ← 主入口
index.cjs    ← 兼容

d.mts / d.cts 是增强兼容用。

unbuild package.json

json 复制代码

"type": "module"

type: module 限制的是包内部的 .js 文件

如果你有:

js 复制代码
dist/index.js

在:

js 复制代码
"type": "module"

下,它会被当作 ESM。

架构

js 复制代码
vue → peerDependencies
@iconify/vue → peerDependencies

图标组件是 UI 层能力,不是运行时核心依赖,让宿主决定 iconify 版本是更安全的。

相关推荐
FreeTinker5 小时前
HTML本地存储技术解析:localStorage与sessionStorage的原理、差异与应用场景
前端·html
qq_452396235 小时前
第十二篇:《全链路监控与可观测性:前端错误追踪与性能分析》
前端
wangruofeng6 小时前
Phosphor Icons 官网源码拆解:URL 即存储、水波动画与 7362 Star 图标库的架构实践
前端·架构·github
BigTopOne7 小时前
WebRTC Native / Android 开发学习资源整理
前端
excel8 小时前
nuxt 3 升级 nuxt 4 报错之 hasInjectionContext
前端
何以解忧,唯有..8 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
软件聚导航9 小时前
「聚小软 AI 助手」技术升级:从数据库检索到 RAG 知识库问答
前端·数据库·mysql·微信小程序·小程序·ai编程·rag
恋猫de小郭9 小时前
看懂大模型架构术语,帮助你理解目前常见的大模型开源架构
前端·人工智能·ai编程
yma169 小时前
通过提示词实现GitHub Pages 和 Nginx 双部署竟然这么简单?
前端
前端 贾公子9 小时前
第09章:上下文与记忆 (2)
java·服务器·前端