Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/unjs/unbuild
📦 An unified javascript build system
https://github.com/unjs/unbuild
universal-javascript
Last synced: 25 days ago
JSON representation
📦 An unified javascript build system
- Host: GitHub
- URL: https://github.com/unjs/unbuild
- Owner: unjs
- License: mit
- Created: 2021-04-07T17:23:00.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-04-13T19:35:00.000Z (7 months ago)
- Last Synced: 2024-04-13T20:45:37.847Z (7 months ago)
- Topics: universal-javascript
- Language: TypeScript
- Homepage:
- Size: 1.17 MB
- Stars: 2,049
- Watchers: 12
- Forks: 79
- Open Issues: 70
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-web-cn - unbuild - 与 tsup 一样,也可以用来打包你的 ts library,相比之下,unbuild 可以打包子目录,而 tsup 则使用起来更简单,更多区别可以看这篇[文章](https://antfu.me/posts/publish-esm-and-cjs#bundling) (Uncategorized / Uncategorized)
- jimsghstars - unjs/unbuild - 📦 A unified JavaScript build system (TypeScript)
- awesome-javascript - unbuild
- awesome-javascript - unbuild
README
# unbuild
[![npm version](https://img.shields.io/npm/v/unbuild)](https://npmjs.com/package/unbuild)
[![npm downloads](https://img.shields.io/npm/dm/unbuild)](https://npm.chart.dev/unbuild)> A unified JavaScript build system
### 📦 Optimized bundler
Robust [rollup](https://rollupjs.org) based bundler that supports typescript and generates commonjs and module formats + type declarations.
### 🪄 Automated config
Automagically infer build config and entries from `package.json`.
### 📁 Bundleless build
Integration with [mkdist](https://github.com/unjs/mkdist) for generating bundleless dists with file-to-file transpilation.
### ✨ Passive watcher
Stub `dist` once using `unbuild --stub` (powered by [jiti](https://github.com/unjs/jiti)) and you can try and link your project without needing to watch and rebuild during development.
### ✍ Untype Generator
Integration with [untyped](https://github.com/unjs/untyped).
### ✔️ Secure builds
Automatically check for various build issues such as potential **missing** and **unused** [dependencies](https://docs.npmjs.com/cli/v7/configuring-npm/package-json#dependencies) and fail CI.
CLI output also includes output size and exports for quick inspection.
## Usage
Create `src/index.ts`:
```js
export const log = (...args) => {
console.log(...args);
};
```Update `package.json`:
```json
{
"type": "module",
"scripts": {
"build": "unbuild",
"prepack": "unbuild"
},
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"files": ["dist"]
}
```> **Note**
> You can find a more complete example in [unjs/template](https://github.com/unjs/template) for project setup.Build with `unbuild`:
```sh
npx unbuild
```Configuration is automatically inferred from fields in `package.json` mapped to `src/` directory. For more control, continue with next section.
## Configuration
Create `build.config.ts`:
```js
export default {
entries: ["./src/index"],
};
```You can either use `unbuild` key in `package.json` or `build.config.{js,cjs,mjs,ts,mts,cts,json}` to specify configuration.
See options [here](./src/types.ts).
Example:
```js
import { defineBuildConfig } from "unbuild";export default defineBuildConfig({
// If entries is not provided, will be automatically inferred from package.json
entries: [
// default
"./src/index",
// mkdist builder transpiles file-to-file keeping original sources structure
{
builder: "mkdist",
input: "./src/package/components/",
outDir: "./build/components",
},
],// Change outDir, default is 'dist'
outDir: "build",// Generates .d.ts declaration file
declaration: true,
});
```Or with multiple builds you can declare an array of configs:
```js
import { defineBuildConfig } from "unbuild";export default defineBuildConfig([
{
// If entries is not provided, will be automatically inferred from package.json
entries: [
// default
"./src/index",
// mkdist builder transpiles file-to-file keeping original sources structure
{
builder: "mkdist",
input: "./src/package/components/",
outDir: "./build/components",
},
],// Change outDir, default is 'dist'
outDir: "build",/**
* * `compatible` means "src/index.ts" will generate "dist/index.d.mts", "dist/index.d.cts" and "dist/index.d.ts".
* * `node16` means "src/index.ts" will generate "dist/index.d.mts" and "dist/index.d.cts".
* * `true` is equivalent to `compatible`.
* * `false` will disable declaration generation.
* * `undefined` will auto detect based on "package.json". If "package.json" has "types" field, it will be `"compatible"`, otherwise `false`.
*/
declaration: "compatible",
},
{
name: "minified",
entries: ["./src/index"],
outDir: "build/min",
rollup: {
esbuild: {
minify: true,
},
},
},
]);
```## Recipes
### Decorators support
In `build.config.ts`
```ts
import { defineBuildConfig } from "unbuild";export default defineBuildConfig({
rollup: {
esbuild: {
tsconfigRaw: {
compilerOptions: {
experimentalDecorators: true,
},
},
},
},
});
```### Generate sourcemaps
```ts
import { defineBuildConfig } from "unbuild";export default defineBuildConfig({
sourcemap: true,
});
```## 💻 Development
- Clone this repository
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` (use `npm i -g corepack` for Node.js < 16.10)
- Install dependencies using `pnpm install`
- Run interactive tests using `pnpm dev`## License
[MIT](./LICENSE)
[npm-version-src]: https://img.shields.io/npm/v/unbuild?style=flat-square
[npm-version-href]: https://npmjs.com/package/unbuild
[npm-downloads-src]: https://img.shields.io/npm/dm/unbuild?style=flat-square
[npm-downloads-href]: https://npmjs.com/package/unbuild
[github-actions-src]: https://img.shields.io/github/actions/workflow/status/unjs/unbuild/ci.yml?style=flat-square
[github-actions-href]: https://github.com/unjs/unbuild/actions?query=workflow%3Aci
[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/unbuild/main?style=flat-square
[codecov-href]: https://codecov.io/gh/unjs/unbuild