Skip to content

Commit 947b31a

Browse files
committed
Improve docs
1 parent 33d5eca commit 947b31a

1 file changed

Lines changed: 82 additions & 75 deletions

File tree

packages/workers-builder/README.md

Lines changed: 82 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,31 @@ Just provide your source code and dependencies — no config files needed:
1515
```typescript
1616
import { createWorker } from 'workers-builder';
1717

18-
const { mainModule, modules } = await createWorker({
19-
files: {
20-
'src/index.ts': `
21-
import { Hono } from 'hono';
22-
import { cors } from 'hono/cors';
23-
24-
const app = new Hono();
25-
app.use('*', cors());
26-
app.get('/', (c) => c.text('Hello from Hono!'));
27-
app.get('/json', (c) => c.json({ message: 'It works!' }));
28-
29-
export default app;
30-
`,
31-
'package.json': JSON.stringify({
32-
dependencies: {
33-
hono: '^4.0.0'
34-
}
35-
}),
36-
},
18+
const worker = env.LOADER.get('my-worker', async () => {
19+
const { mainModule, modules } = await createWorker({
20+
files: {
21+
'src/index.ts': `
22+
import { Hono } from 'hono';
23+
import { cors } from 'hono/cors';
24+
25+
const app = new Hono();
26+
app.use('*', cors());
27+
app.get('/', (c) => c.text('Hello from Hono!'));
28+
app.get('/json', (c) => c.json({ message: 'It works!' }));
29+
30+
export default app;
31+
`,
32+
'package.json': JSON.stringify({
33+
dependencies: {
34+
hono: '^4.0.0'
35+
}
36+
}),
37+
},
38+
});
39+
40+
return { mainModule, modules, compatibilityDate: '2026-01-01' };
3741
});
3842

39-
// Use with Worker Loader binding
40-
const worker = env.LOADER.get('my-worker', async () => ({
41-
mainModule,
42-
modules,
43-
compatibilityDate: '2026-01-01',
44-
}));
45-
4643
await worker.getEntrypoint().fetch(request);
4744
```
4845

@@ -94,32 +91,36 @@ Priority order:
9491
### Multiple Dependencies
9592

9693
```typescript
97-
const { mainModule, modules } = await createWorker({
98-
files: {
99-
'src/index.ts': `
100-
import { Hono } from 'hono';
101-
import { zValidator } from '@hono/zod-validator';
102-
import { z } from 'zod';
103-
104-
const app = new Hono();
105-
106-
const schema = z.object({ name: z.string() });
107-
108-
app.post('/greet', zValidator('json', schema), (c) => {
109-
const { name } = c.req.valid('json');
110-
return c.json({ message: \`Hello, \${name}!\` });
111-
});
112-
113-
export default app;
114-
`,
115-
'package.json': JSON.stringify({
116-
dependencies: {
117-
hono: '^4.0.0',
118-
'@hono/zod-validator': '^0.4.0',
119-
zod: '^3.23.0'
120-
}
121-
}),
122-
},
94+
const worker = env.LOADER.get('my-worker', async () => {
95+
const { mainModule, modules } = await createWorker({
96+
files: {
97+
'src/index.ts': `
98+
import { Hono } from 'hono';
99+
import { zValidator } from '@hono/zod-validator';
100+
import { z } from 'zod';
101+
102+
const app = new Hono();
103+
104+
const schema = z.object({ name: z.string() });
105+
106+
app.post('/greet', zValidator('json', schema), (c) => {
107+
const { name } = c.req.valid('json');
108+
return c.json({ message: \`Hello, \${name}!\` });
109+
});
110+
111+
export default app;
112+
`,
113+
'package.json': JSON.stringify({
114+
dependencies: {
115+
hono: '^4.0.0',
116+
'@hono/zod-validator': '^0.4.0',
117+
zod: '^3.23.0'
118+
}
119+
}),
120+
},
121+
});
122+
123+
return { mainModule, modules, compatibilityDate: '2026-01-01' };
123124
});
124125
```
125126

@@ -128,37 +129,43 @@ const { mainModule, modules } = await createWorker({
128129
For projects that need specific compatibility settings or are migrating from existing Workers:
129130

130131
```typescript
131-
const { mainModule, modules, wranglerConfig } = await createWorker({
132-
files: {
133-
'src/index.ts': `
134-
export default {
135-
fetch: () => new Response('Hello!')
136-
}
137-
`,
138-
'wrangler.toml': `
139-
main = "src/index.ts"
140-
compatibility_date = "2026-01-01"
141-
compatibility_flags = ["nodejs_compat"]
142-
`,
143-
},
132+
const worker = env.LOADER.get('my-worker', async () => {
133+
const { mainModule, modules, wranglerConfig } = await createWorker({
134+
files: {
135+
'src/index.ts': `
136+
export default {
137+
fetch: () => new Response('Hello!')
138+
}
139+
`,
140+
'wrangler.toml': `
141+
main = "src/index.ts"
142+
compatibility_date = "2026-01-01"
143+
compatibility_flags = ["nodejs_compat"]
144+
`,
145+
},
146+
});
147+
148+
return {
149+
mainModule,
150+
modules,
151+
compatibilityDate: wranglerConfig?.compatibilityDate ?? '2026-01-01',
152+
compatibilityFlags: wranglerConfig?.compatibilityFlags,
153+
};
144154
});
145-
146-
const worker = env.LOADER.get('my-worker', async () => ({
147-
mainModule,
148-
modules,
149-
compatibilityDate: wranglerConfig?.compatibilityDate ?? '2026-01-01',
150-
compatibilityFlags: wranglerConfig?.compatibilityFlags,
151-
}));
152155
```
153156

154157
### Transform-only Mode
155158

156159
Skip bundling to preserve module structure:
157160

158161
```typescript
159-
const { mainModule, modules } = await createWorker({
160-
files: { /* ... */ },
161-
bundle: false,
162+
const worker = env.LOADER.get('my-worker', async () => {
163+
const { mainModule, modules } = await createWorker({
164+
files: { /* ... */ },
165+
bundle: false,
166+
});
167+
168+
return { mainModule, modules, compatibilityDate: '2026-01-01' };
162169
});
163170
```
164171

0 commit comments

Comments
 (0)