Skip to content

Commit 648b9b4

Browse files
Barbapapazespi0
andauthored
docs: add examples (#334)
Co-authored-by: Pooya Parsa <[email protected]>
1 parent e1e0935 commit 648b9b4

18 files changed

+206
-3
lines changed

examples/1.zero-config/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Unbuild zero config example
2+
3+
Unbuild automatically infers the build configuration from `exports` field in [`package.json`](./package.json).
4+
5+
Since 3 `types`, `import` and `require` fields are set, build automatically includes them.
6+
7+
Unbuild also supports building multiple entries.

examples/1.zero-config/package.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "unbuild-example-zero-config",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"exports": {
6+
".": {
7+
"import": {
8+
"types": "./dist/index.d.mts",
9+
"default": "./dist/index.mjs"
10+
},
11+
"require": {
12+
"types": "./dist/index.d.cts",
13+
"default": "./dist/index.cjs"
14+
}
15+
},
16+
"./utils": {
17+
"import": {
18+
"types": "./dist/utils.d.mts",
19+
"default": "./dist/utils.mjs"
20+
},
21+
"require": {
22+
"types": "./dist/utils.d.cts",
23+
"default": "./dist/utils.cjs"
24+
}
25+
}
26+
},
27+
"files": [
28+
"dist"
29+
],
30+
"scripts": {
31+
"build": "unbuild",
32+
"build:stub": "unbuild --stub"
33+
},
34+
"devDependencies": {
35+
"unbuild": "^2.0.0"
36+
}
37+
}

examples/1.zero-config/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function main(data: string): string {
2+
return `Hello ${data}!`;
3+
}

examples/1.zero-config/src/utils.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function sum(a: number, b: number) {
2+
return a + b;
3+
}

examples/2.mkdist/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Unbuild mkdist example
2+
3+
A simple example of how to generate ESM, CJS and DTS from TypeScript using a folder as entry point.

examples/2.mkdist/build.config.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { defineBuildConfig } from "unbuild";
2+
3+
export default defineBuildConfig({
4+
entries: [
5+
"src/index.ts",
6+
{
7+
input: "src/plugins/",
8+
outDir: "dist/plugins/",
9+
format: "esm",
10+
},
11+
{
12+
input: "src/plugins/",
13+
outDir: "dist/plugins/",
14+
format: "cjs",
15+
ext: "cjs",
16+
declaration: false,
17+
},
18+
],
19+
declaration: true,
20+
rollup: {
21+
emitCJS: true,
22+
},
23+
});

examples/2.mkdist/package.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "unbuild-example-mkdist",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"exports": {
6+
".": {
7+
"import": {
8+
"types": "./dist/index.d.mts",
9+
"default": "./dist/index.mjs"
10+
},
11+
"require": {
12+
"types": "./dist/index.d.cts",
13+
"default": "./dist/index.cjs"
14+
}
15+
},
16+
"./plugins/*": {
17+
"import": {
18+
"types": "./dist/plugins/*.d.mts",
19+
"default": "./dist/plugins/*.mjs"
20+
},
21+
"require": {
22+
"types": "./dist/plugins/*.d.cts",
23+
"default": "./dist/plugins/*.cjs"
24+
}
25+
}
26+
},
27+
"files": [
28+
"dist"
29+
],
30+
"scripts": {
31+
"build": "unbuild",
32+
"build:stub": "unbuild --stub"
33+
},
34+
"devDependencies": {
35+
"unbuild": "^2.0.0"
36+
}
37+
}

examples/2.mkdist/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function main(data: string): string {
2+
return `Hello ${data}!`;
3+
}

examples/2.mkdist/src/plugins/vite.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function vitePlugin(): string {
2+
return "Hello Vite!";
3+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function webpackPlugin(): string {
2+
return "Hello Webpack!";
3+
}

examples/3.untyped/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
schema

examples/3.untyped/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Unbuild untyped example

examples/3.untyped/build.config.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineBuildConfig } from "unbuild";
2+
3+
export default defineBuildConfig({
4+
entries: [
5+
"src/index.ts",
6+
{
7+
builder: "untyped",
8+
input: "src/index.ts",
9+
outDir: "schema",
10+
name: "schema",
11+
},
12+
],
13+
declaration: true,
14+
rollup: {
15+
emitCJS: true,
16+
},
17+
});

examples/3.untyped/package.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "unbuild-example-untyped",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"exports": {
6+
".": {
7+
"import": {
8+
"types": "./dist/index.d.mts",
9+
"default": "./dist/index.mjs"
10+
},
11+
"require": {
12+
"types": "./dist/index.d.cts",
13+
"default": "./dist/index.cjs"
14+
}
15+
}
16+
},
17+
"files": [
18+
"dist"
19+
],
20+
"scripts": {
21+
"build": "unbuild"
22+
},
23+
"devDependencies": {
24+
"unbuild": "^2.0.0"
25+
}
26+
}

examples/3.untyped/src/index.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export function sendMessage(
2+
message: string,
3+
date = new Date(),
4+
flash?: boolean,
5+
): string {
6+
return "OK";
7+
}
8+
9+
export const config = {
10+
name: "default",
11+
price: 12.5,
12+
/**
13+
* checked state
14+
*/
15+
checked: false,
16+
dimensions: {
17+
/** width in px */
18+
width: 10,
19+
/** height in px */
20+
height: 10,
21+
},
22+
tags: {
23+
$resolve: (val?: string[]) => ["tag1", ...(val || [])].filter(Boolean),
24+
},
25+
};

examples/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# unbuild examples
2+
3+
In this directory you can find some examples of how to use unbuild.
4+
5+
- [Zero Config](./1.zero-config/)
6+
- [mkdist](./2.mkdist/)
7+
- [untyped](./3.untyped/)

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"scripts": {
2020
"build": "pnpm unbuild",
2121
"dev": "pnpm unbuild test/fixture",
22-
"lint": "eslint . && prettier -c src test",
23-
"lint:fix": "eslint --fix . && prettier -w src test",
22+
"lint": "eslint . && prettier -c src test examples",
23+
"lint:fix": "eslint --fix . && prettier -w src test examples",
2424
"prepack": "pnpm unbuild",
2525
"release": "pnpm test && changelogen --release && git push --follow-tags && npm publish",
2626
"prerelease": "pnpm test && changelogen --prerelease --release && git push --follow-tags && npm publish --tag rc",
@@ -63,7 +63,8 @@
6363
"eslint-config-unjs": "^0.3.2",
6464
"prettier": "^3.3.0",
6565
"typescript": "^5.4.5",
66-
"vitest": "^1.6.0"
66+
"vitest": "^1.6.0",
67+
"unbuild": "workspace:."
6768
},
6869
"peerDependencies": {
6970
"typescript": "^5.4.5"

pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)