Skip to content

Commit b361aa7

Browse files
committed
fin
1 parent 439a2ac commit b361aa7

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

docs/runtime-types.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,84 @@
22

33
In Superglue, there's no need to annotate your types in ruby just to regenerate
44
them in typescript. Instead you write **typescript first** and let runtime type
5-
validation give you the developer feedback to build your '.props`.
5+
validation give you the developer feedback to build your `.props`.
66

77
To get started, run the installation generator with the typescript flag.
88

99
```terminal
1010
rails g superglue:install --typescript
1111
```
1212

13+
And make use of the following esbuild plugin, deepkit plugins are also available for vite and bun:
14+
15+
```diff
16+
import * as esbuild from 'esbuild'
17+
import svgr from 'esbuild-plugin-svgr'
18+
+ import { DeepkitLoader } from '@deepkit/type-compiler'
19+
import { readFileSync } from 'fs'
20+
import ts from 'typescript'
21+
import path from 'node:path'
22+
23+
const isWatch = process.argv.includes('--watch')
24+
25+
// Deepkit transformation plugin for tsup/esbuild
26+
const deepkitLoader = new DeepkitLoader()
27+
28+
+ const deepkitPlugin = {
29+
+ name: 'deepkit',
30+
+ setup(build) {
31+
+ const loaderMap = {
32+
+ '.ts': 'ts',
33+
+ '.tsx': 'tsx',
34+
+ '.js': 'js',
35+
+ '.jsx': 'jsx',
36+
+ }
37+
+ build.onLoad({ filter: /\.(tsx?|jsx?)$/ }, async (args) => {
38+
+ if (args.path.includes('node_modules')) {
39+
+ return null
40+
+ }
41+
+
42+
+ const source = readFileSync(args.path, 'utf8')
43+
+
44+
+ const deepkitTransformed = deepkitLoader.transform(source, args.path)
45+
+
46+
+ const ext = path.extname(args.path)
47+
+ const loader = loaderMap[ext] || 'js'
48+
+
49+
+ return {
50+
+ contents: deepkitTransformed,
51+
+ loader,
52+
+ }
53+
+ })
54+
+ },
55+
+ }
56+
57+
const buildOptions = {
58+
entryPoints: [
59+
'app/javascript/application_superglue.tsx',
60+
'app/javascript/admin/application.js',
61+
'app/javascript/application.js',
62+
],
63+
bundle: true,
64+
sourcemap: true,
65+
format: 'esm',
66+
outdir: 'app/assets/builds',
67+
publicPath: '/assets',
68+
+ plugins: process.env.NODE_ENV === 'production' ? [svgr()] : [deepkitPlugin, svgr()] ,
69+
metafile: true,
70+
+ conditions: process.env.NODE_ENV === 'production' ? ['production'] : [],
71+
}
72+
73+
if (isWatch) {
74+
const ctx = await esbuild.context(buildOptions)
75+
await ctx.watch()
76+
console.log('Watching for changes...')
77+
} else {
78+
const result = await esbuild.build(buildOptions)
79+
console.log(await esbuild.analyzeMetafile(result.metafile))
80+
}
81+
```
82+
1383
## How It Works
1484

1585
Superglue uses [Deepkit](https://deepkit.io/) for runtime type validation during development:
@@ -22,7 +92,7 @@ Superglue uses [Deepkit](https://deepkit.io/) for runtime type validation during
2292

2393
`useContent` is the generic hook used to access the props [you
2494
build](../docs/shaping.md). To make use of runtime types, simply pass a type
25-
describing your page's props:
95+
describing your page's props as you normally would:
2696

2797
For example:
2898

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ nav:
106106
- Rails Forms / React UI kits: forms.md
107107
- Shared data: shared-data.md
108108
- Fragments: fragments.md
109+
- Working with types: runtime-types.md
109110

110111
- Updating:
111112
- Super Turbo Streams: super-turbo-streams.md

0 commit comments

Comments
 (0)