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
Copy file name to clipboardExpand all lines: README.md
+84-3Lines changed: 84 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -59,7 +59,11 @@ import { defineConfig } from "vite";
59
59
import { resXVitePlugin } from"rescript-x";
60
60
61
61
exportdefaultdefineConfig({
62
-
plugins: [resXVitePlugin()],
62
+
plugins: [
63
+
resXVitePlugin({
64
+
clientDirs: ["client"],
65
+
}),
66
+
],
63
67
server: {
64
68
port:9000,
65
69
},
@@ -326,7 +330,7 @@ GET /assets/logo.svg
326
330
327
331
### `assets` for assets that do need transformation
328
332
329
-
If you have assets you'd like transformed by Vite before using, put them in the top level `assets` folder. This could be CSS, images, additional JavaScript, and so on. Anything you might want Vite to transform.
333
+
If you have assets you'd like transformed by Vite before using, put them in the top level `assets` folder. This could be CSS, images, or browser entry JavaScript. Anything you might want Vite to transform.
330
334
331
335
Here's an example of how you wire up Tailwind:
332
336
@@ -347,6 +351,81 @@ Then, include it in your ReScript:
347
351
348
352
There! It's now available to you, and Vite will both transform and hot module reload the asset if it's possible.
349
353
354
+
#### Thinking about client side JavaScript
355
+
356
+
ResX is server-first. The default is:
357
+
358
+
- Render HTML on the server.
359
+
- Reach for normal links, forms and handlers first.
360
+
- Use HTMX or `ResX.Client` when declarative browser behavior is enough.
361
+
- Add your own browser JavaScript only when you actually need code running in the browser.
362
+
363
+
When you do need browser JavaScript, think in terms of browser entry modules, not loose script files. An entry module is the file you include from HTML. That file can then import whatever else it needs, and Vite will handle transformation, minification, hashing, CSS extraction, and shared chunks in production.
364
+
365
+
There are two intended places for those entry modules:
366
+
367
+
- Put small app-local entry files in top level `assets/` when they sit naturally next to your other transformed assets.
368
+
- Configure `clientDirs` when you want a dedicated folder for browser code, for example `client/`.
369
+
370
+
Top level JS and TS files in `assets/` become browser entries automatically. They are exposed through `ResXAssets.assets` and should be loaded as module scripts:
If you want browser entry files outside `assets/`, configure `clientDirs` in `resXVitePlugin`. Files found there are also exposed through `ResXAssets.assets`, prefixed by directory name:
Any CSS imported from those browser entries is emitted and loaded automatically in both development and production.
424
+
425
+
By default, only top level JS and TS files in `assets/` and each configured `clientDirs` folder become entries. Put shared support modules in subdirectories and import them from those entries so Vite can emit shared chunks for them. If you want a different discovery rule, set `assetEntryGlobs` and `clientEntryGlobs`.
426
+
427
+
Current limitation: this pipeline expects browser entries to be JavaScript or TypeScript by the time Vite sees them. Direct `.res` entry files are not part of this flow. If you want to write client code in ReScript, compile it to JS first and then point `clientDirs` or `extraClientEntries` at that generated JS.
428
+
350
429
#### Referring to transformed `assets`
351
430
352
431
Notice how we're not using a `"/assets/styles.css"` string to refer to `styles.css`, but rather `ResXAssets.assets.styles_css`? This is because ResX comes with a "type safe" asset layer - anything you put in `assets/` will be available via `ResXAssets.assets`.
@@ -684,10 +763,12 @@ These functions should only be used in exceptional cases where you need to:
684
763
685
764
ResX also ships with a tiny client side library that will help you do basic client side tasks fully declaratively. It's quite basic at the moment, but will be extended (tastefully) as we discover more places where it can help you avoid having to use a full blown client side framework to accomplish fairly basic tasks.
686
765
766
+
The browser bundle for this is shipped with `rescript-x`, so you can reference `ResXAssets.assets.resXClient_js` directly without adding your own `extraClientEntries` config.
767
+
687
768
To use ResX client, make sure you include its script:
0 commit comments