22
33In Superglue, there's no need to annotate your types in ruby just to regenerate
44them 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
77To get started, run the installation generator with the typescript flag.
88
99``` terminal
1010rails 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
1585Superglue 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
2494build] ( ../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
2797For example:
2898
0 commit comments