You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enable support for .ts files via node and esbuild type stripping
This enables support for .ts files anywhere in top-bun you could use a .js file.
In node contexts, type stripping is provided by node itself, so be sure to enable that feature in Node.js
In client contexts, esbuild strips the types.
│ ├── page.html # Raw html pages are also supported. They support handlebars template blocks.
74
75
│ ├── page.vars.js # pages can define page variables in a page.vars.js.
75
76
│ └── style.css
77
+
├── js-page
78
+
│ └── page.js # A page can also just be a plain javascript function that returns content
79
+
├── ts-page
80
+
│ ├── client.ts # client bundles can be written in typescript via type stripping
81
+
│ ├── page.vars.ts # pages can define page variables in a page.vars.js.
82
+
│ └── page.ts # Anywhere you can use js in top-bun, you can also use typescript files. They compile via speedy type stripping.
76
83
├── feeds
77
84
│ └── feeds.template.js # Templates let you generate any file you want from variables and page data.
78
85
├── layouts # layouts can live anywhere. The inner content of your page is slotted into your layout.
79
86
│ ├── blog.layout.js # pages specify which layout they want by setting a `layout` page variable.
80
87
│ ├── blog.layout.css # layouts can define an additional layout style.
81
88
│ ├── blog.layout.client.js # layouts can also define a layout client.
82
89
│ ├── article.layout.js # layouts can extend other layouts, since they are just functions.
90
+
│ ├── typescript.layout.ts # layouts can also be written in typescript
83
91
│ └── root.layout.js # the default layout is called root.
84
92
├── globals # global assets can live anywhere. Here they are in a folder called globals.
85
93
│ ├── global.client.js # you can define a global js client that loads on every page.
@@ -588,7 +596,7 @@ These imports will include the `root.layout.js` layout assets into the `blog.lay
588
596
589
597
All static assets in the `src` directory are copied 1:1 to the `public` directory. Any file in the `src` directory that doesn't end in `.js`, `.css`, `.html`, or `.md` is copied to the `dest` directory.
590
598
591
-
### 📁 `--copy` directories
599
+
### `--copy` directories
592
600
593
601
You can specify directories to copy into your `dest` directory using the `--copy` flag. Everything in those directories will be copied as-is into the destination, including js, css, html and markdown, preserving the internal directory structure. Conflicting files are not detected or reported and will cause undefined behavior.
594
602
@@ -926,18 +934,6 @@ Template files receive a similar set of variables:
926
934
-`pages`: An array of [`PageData`](https://github.com/bcomnes/top-bun/blob/master/lib/build-pages/page-data.js) instances for every page in the site build. Use this array to introspect pages to generate feeds and index pages.
927
935
-`template`: An object of the template file data being rendered.
928
936
929
-
### Variable types
930
-
931
-
The following types are exported from `top-bun`:
932
-
933
-
```ts
934
-
LayoutFunction<T>
935
-
PostVarsFunction<T>
936
-
PageFunction<T>
937
-
TemplateFunction<T>
938
-
TemplateAsyncIterator<T>
939
-
```
940
-
941
937
Where `T` is your set of variables in the `vars` object.
942
938
943
939
### `postVars` post processing variables (Advanced) {#postVars}
@@ -992,6 +988,115 @@ This `postVars` renders some html from page introspection of the last 5 blog pos
992
988
\{{{ vars.blogPostsHtml }}}
993
989
```
994
990
991
+
992
+
## TypeScript Support
993
+
994
+
`top-bun` now supports **TypeScript** via native type-stripping in Node.js.
995
+
996
+
-**Requires Node.js ≥23***(built-in)* or **Node.js 22** with the `NODE_OPTIONS="--experimental-strip-types" top-bun` env variable.
- No explicit compilation step needed—Node.js handles type stripping at runtime.
999
+
- Fully compatible with existing `top-bun` file naming conventions.
1000
+
1001
+
### Supported File Types
1002
+
1003
+
Anywhere you can use a `.js`, `.mjs` or `.cjs` file in top-bun, you can now use `.ts`, `.mts`, `.cts`.
1004
+
When running in a Node.js context, [type-stripping](https://nodejs.org/api/typescript.html#type-stripping) is used.
1005
+
When running in a web client context, [esbuild](https://esbuild.github.io/content-types/#typescript) type stripping is used.
1006
+
Type stripping provides 0 type checking, so be sure to set up `tsc` and `tsconfig.json` so you can catch type errors while editing or in CI.
1007
+
1008
+
### Recommended `tsconfig.json`
1009
+
1010
+
Install [@voxpelli/tsconfig](https://ghub.io/@voxpelli/tsconfig) which provides type checking in `.js` and `.ts` files and preconfigured for `--no-emit` and extend with type stripping friendly rules:
1011
+
1012
+
```json
1013
+
{
1014
+
"extends": "@voxpelli/tsconfig/node20.json",
1015
+
"compilerOptions": {
1016
+
"skipLibCheck": true,
1017
+
"erasableSyntaxOnly": true,
1018
+
"allowImportingTsExtensions": true,
1019
+
"rewriteRelativeImportExtensions": true,
1020
+
"verbatimModuleSyntax": true
1021
+
},
1022
+
"include": [
1023
+
"**/*",
1024
+
],
1025
+
"exclude": [
1026
+
"**/*.js",
1027
+
"node_modules",
1028
+
"coverage",
1029
+
".github"
1030
+
]
1031
+
}
1032
+
```
1033
+
1034
+
### Using TypeScript with top-bun Types
1035
+
1036
+
You can use `top-bun`'s built-in types to strongly type your layout, page, and template functions. The following types are available:
1037
+
1038
+
```ts
1039
+
importtype {
1040
+
LayoutFunction,
1041
+
PostVarsFunction,
1042
+
PageFunction,
1043
+
TemplateFunction,
1044
+
TemplateAsyncIterator
1045
+
} from'top-bun'
1046
+
```
1047
+
1048
+
They are all generic and accept a variable template that you can develop and share between files.
1049
+
1050
+
### TypeScript Examples
1051
+
1052
+
#### Page Function
1053
+
1054
+
```typescript
1055
+
// page.ts
1056
+
importtype { PageFunction } from'top-bun'
1057
+
1058
+
exportconst vars = {
1059
+
message: 'TypeScript pages are easy!'
1060
+
}
1061
+
1062
+
const page:PageFunction<typeofvars> =async ({ vars }) => {
1063
+
return`<h1>Hello from TypeScript!</h1><p>${vars.message}</p>`
- Convention over configuration. All configuration should be optional, and at most it should be minimal.
@@ -1013,8 +1118,8 @@ This `postVars` renders some html from page introspection of the last 5 blog pos
1013
1118
- Garbage in, garbage out. Don't over-correct bad input.
1014
1119
- Conventions + standards. Vanilla file types. No new file extensions. No weird syntax to learn. Language tools should just work because you aren't doing anything weird or out of band.
1015
1120
- Encourage directly runnable source files. Direct run is an incredible, undervalued feature more people should learn to use.
1016
-
- Support typescript, via ts-in-js and type stripping features when available.
1017
-
- Embrace the now. Limit support to pretend you are working in the future (technology predictions nearly always are wrong!)
1121
+
- Support typescript, via ts-in-js and type stripping features. Leave type checking to tsc.
1122
+
- Embrace the now. Limit support on features that let one pretend they are working with future ecosystem features e.g. pseudo esm (technology predictions nearly always are wrong!)
1018
1123
1019
1124
## FAQ
1020
1125
@@ -1077,7 +1182,9 @@ Some notable features are included below, see the [roadmap](https://github.com/u
1077
1182
-[x] Built in browsersync dev server
1078
1183
-[x] Real default layout style builds
1079
1184
-[x] Esbuild settings escape hatch
1080
-
-[x] Copy folders
1185
+
-[x] Copy folders
1186
+
-[x] Full Typescript support via native type stripping
0 commit comments