proscenium 0.9.1-arm64-darwin → 0.11.0-arm64-darwin

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +451 -65
  3. data/lib/proscenium/builder.rb +144 -0
  4. data/lib/proscenium/css_module/path.rb +31 -0
  5. data/lib/proscenium/css_module/transformer.rb +82 -0
  6. data/lib/proscenium/css_module.rb +12 -25
  7. data/lib/proscenium/ensure_loaded.rb +27 -0
  8. data/lib/proscenium/ext/proscenium +0 -0
  9. data/lib/proscenium/ext/proscenium.h +20 -12
  10. data/lib/proscenium/helper.rb +85 -0
  11. data/lib/proscenium/importer.rb +110 -0
  12. data/lib/proscenium/libs/react-manager/index.jsx +101 -0
  13. data/lib/proscenium/libs/react-manager/react.js +2 -0
  14. data/lib/proscenium/libs/stimulus-loading.js +83 -0
  15. data/lib/proscenium/libs/test.js +1 -0
  16. data/lib/proscenium/log_subscriber.rb +1 -2
  17. data/lib/proscenium/middleware/base.rb +8 -8
  18. data/lib/proscenium/middleware/engines.rb +37 -0
  19. data/lib/proscenium/middleware/esbuild.rb +3 -5
  20. data/lib/proscenium/middleware/runtime.rb +18 -0
  21. data/lib/proscenium/middleware.rb +19 -4
  22. data/lib/proscenium/{side_load/monkey.rb → monkey.rb} +24 -12
  23. data/lib/proscenium/phlex/{resolve_css_modules.rb → css_modules.rb} +28 -16
  24. data/lib/proscenium/phlex/react_component.rb +27 -64
  25. data/lib/proscenium/phlex.rb +11 -30
  26. data/lib/proscenium/railtie.rb +49 -41
  27. data/lib/proscenium/react_componentable.rb +95 -0
  28. data/lib/proscenium/resolver.rb +37 -0
  29. data/lib/proscenium/side_load.rb +13 -72
  30. data/lib/proscenium/source_path.rb +15 -0
  31. data/lib/proscenium/templates/rescues/build_error.html.erb +30 -0
  32. data/lib/proscenium/utils.rb +13 -0
  33. data/lib/proscenium/version.rb +1 -1
  34. data/lib/proscenium/view_component/css_modules.rb +11 -0
  35. data/lib/proscenium/view_component/react_component.rb +15 -28
  36. data/lib/proscenium/view_component/sideload.rb +4 -0
  37. data/lib/proscenium/view_component.rb +8 -31
  38. data/lib/proscenium.rb +23 -68
  39. metadata +25 -59
  40. data/lib/proscenium/css_module/class_names_resolver.rb +0 -66
  41. data/lib/proscenium/css_module/resolver.rb +0 -76
  42. data/lib/proscenium/current.rb +0 -9
  43. data/lib/proscenium/esbuild/golib.rb +0 -97
  44. data/lib/proscenium/esbuild.rb +0 -32
  45. data/lib/proscenium/phlex/component_concerns.rb +0 -27
  46. data/lib/proscenium/phlex/page.rb +0 -62
  47. data/lib/proscenium/side_load/ensure_loaded.rb +0 -25
  48. data/lib/proscenium/side_load/helper.rb +0 -25
  49. data/lib/proscenium/view_component/tag_builder.rb +0 -23
data/README.md CHANGED
@@ -1,31 +1,65 @@
1
1
  # Proscenium - Modern client-side development for Rails
2
2
 
3
- Proscenium treats your client-side code as first class citizens of your Rails app, and assumes a
4
- "fast by default" internet. It bundles your JS, JSX and CSS in real time, on demand, and with zero
5
- configuration.
3
+ Proscenium treats your client-side code as first class citizens of your Rails app, and assumes a "fast by default" internet. It bundles your JavaScript and CSS in real time, on demand, and with zero configuration.
6
4
 
7
- - Fast real-time bundling, tree-shaking and minification.
8
- - Real time bundling of Javascript (.js,.jsx), Typescript (.ts,.tsx) and CSS (.css).
9
- - NO JavaScript runtime - just the browser!
5
+ **The highlights:**
6
+
7
+ - Fast real-time bundling, tree-shaking, code-splitting and minification of Javascript (.js,.jsx), Typescript (.ts,.tsx) and CSS (.css).
8
+ - NO JavaScript runtime needed - just the browser!
10
9
  - NO build step or pre-compilation.
11
10
  - NO additional process or server - Just run Rails!
12
11
  - Deep integration with Rails.
13
- - Zero configuration.
14
- - Serve assets from anywhere within your Rails root (/app, /config, /lib, etc.).
15
- - Automatically side load JS/TS/CSS for your layouts and views.
16
- - Import from NPM, URLs, and locally.
12
+ - Automatically side-load your layouts, views, and partials.
13
+ - Import from NPM, URL's, and locally.
17
14
  - Server-side import map support.
18
- - CSS Modules.
19
- - CSS mixins.
15
+ - CSS Modules & mixins.
20
16
  - Source maps.
21
- - Phlex and ViewComponent integration.
17
+
18
+ ## Table of Contents
19
+
20
+ - [Getting Started](#getting-started)
21
+ - [Installation](#installation)
22
+ - [Client-Side Code Anywhere](#client-side-code-anywhere)
23
+ - [Side Loading](#side-loading)
24
+ - [Importing](#importing-assets)
25
+ - [URL Imports](#url-imports)
26
+ - [Local Imports](#local-imports)
27
+ - [Import Maps](#import-maps)
28
+ - [Source Maps](#source-maps)
29
+ - [SVG](#svg)
30
+ - [Environment Variables](#environment-variables)
31
+ - [i18n](#i18n)
32
+ - [JavaScript](#javascript)
33
+ - [Tree Shaking](#tree-shaking)
34
+ - [Code Splitting](#code-splitting)
35
+ - [JavaScript Caveats](#javascript-caveats)
36
+ - [CSS](#css)
37
+ - [Importing CSS from JavaScript](#importing-css-from-javascript)
38
+ - [CSS Modules](#css-modules)
39
+ - [CSS Mixins](#css-mixins)
40
+ - [CSS Caveats](#css-caveats)
41
+ - [Typescript](#typescript)
42
+ - [Typescript Caveats](#typescript-caveats)
43
+ - [JSX](#jsx)
44
+ - [JSON](#json)
45
+ - [Phlex Support](#phlex-support)
46
+ - [ViewComponent Support](#viewcomponent-support)
47
+ - [Cache Busting](#cache-busting)
48
+ - [rjs is back!](#rjs-is-back)
49
+ - [Included Paths](#included-paths)
50
+ - [Thanks](#thanks)
51
+ - [Development](#development)
22
52
 
23
53
  ## Getting Started
24
54
 
25
- Getting started obviously depends on whether you are adding Proscenium to an existing Rails app, or creating a new Rails app. So please choose the appropriate guide below:
55
+ Getting started obviously depends on whether you are adding Proscenium to an existing Rails app, or creating a new one. So choose the appropriate guide below:
26
56
 
27
57
  - [Getting Started with a new Rails app](https://github.com/joelmoss/proscenium/blob/master/docs/guides/new_rails_app.md)
28
- - Getting Started with an existing Rails app *[Coming soon]*
58
+ - Getting Started with an existing Rails app
59
+ - [Migrate from Sprockets](docs/guides/migrate_from_sprockets.md)
60
+ - Migrate from Propshaft *[Coming soon]*
61
+ - Migrate from Webpacker *[Coming soon]*
62
+ - [Render a React component with Proscenium](docs/guides/basic_react.md)
29
63
 
30
64
  ## Installation
31
65
 
@@ -53,48 +87,107 @@ Using the examples above...
53
87
  - `app/components/menu_component.jsx` => `https://yourapp.com/app/components/menu_component.jsx`
54
88
  - `config/properties.css` => `https://yourapp.com/config/properties.css`
55
89
 
56
- ### Include Paths
90
+ ## Side Loading
91
+
92
+ > Prior to **0.10.0**, only assets with the extension `.js`, `.ts` and `.css` were side loaded. From 0.10.0, all assets are side loaded, including `.jsx`, `.tsx`, and `.module.css`. Also partials were not side loaded prior to 0.10.0.
57
93
 
58
- By default, Proscenium will serve files ending with any of these extension: `js,mjs,ts,css,jsx,tsx`, and only from `app/assets`, `config`, `app/views`, `lib` and `node_modules` directories.
94
+ Proscenium is best experienced when you side load your assets.
59
95
 
60
- However, you can customise these paths with the `include_path` config option...
96
+ ### The Problem
61
97
 
62
- ```ruby
63
- Rails.application.config.proscenium.include_paths << 'app/components'
98
+ With Rails you would typically declaratively load your JavaScript and CSS assets using the `javascript_include_tag` and `stylesheet_link_tag` helpers.
99
+
100
+ For example, you may have top-level "application" CSS located in a file at `/app/assets/application.css`. Likewise, you may have some global JavaScript located in a file at `/app/assets/application.js`.
101
+
102
+ You would manually and declaratively include those two files in your application layout, something like this:
103
+
104
+ ```erb
105
+ <%# /app/views/layouts/application.html.erb %>
106
+
107
+ <!DOCTYPE html>
108
+ <html>
109
+ <head>
110
+ <title>Hello World</title>
111
+ <%= stylesheet_link_tag 'application' %> <!-- << Your app CSS -->
112
+ </head>
113
+ <body>
114
+ <%= yield %>
115
+ <%= javascript_include_tag 'application' %> <!-- << Your app JS -->
116
+ </body>
117
+ </html>
64
118
  ```
65
119
 
66
- ## Side Loading
120
+ Now, you may have some CSS and JavaScript that is only required by a specific view and partial, so you would load that in your view (or layout), something like this:
67
121
 
68
- Proscenium has built in support for automatically side loading JS, TS and CSS with your views and
69
- layouts.
122
+ ```erb
123
+ <%# /app/views/users/index.html.erb %>
70
124
 
71
- Just create a JS and/or CSS file with the same name as any view or layout, and make sure your
72
- layouts include `<%= side_load_stylesheets %>` and `<%= side_load_javascripts %>`. Something like
73
- this:
125
+ <%= stylesheet_link_tag 'users' %>
126
+ <%= javascript_include_tag 'users' %>
74
127
 
75
- ```html
128
+ <%# needed by the `users/_user.html.erb` partial %>
129
+ <%= javascript_include_tag '_user' %>
130
+
131
+ <% render @users %>
132
+ ```
133
+
134
+ The main problem is that you have to keep track of all these assets, and make sure each is loaded by all the views that require them, but also avoid loading them when not needed. This can be a real pain, especially when you have a lot of views.
135
+
136
+ ### The Solution
137
+
138
+ When side loading your JavaScript, Typescript and CSS with Proscenium, they are automatically included alongside your views, partials, layouts, and components, and only when needed.
139
+
140
+ Side loading works by looking for a JS/TS/CSS file with the same name as your view, partial, layout or component. For example, if you have a view at `app/views/users/index.html.erb`, then Proscenium will look for a JS/TS/CSS file at `app/views/users/index.js`, `app/views/users/index.ts` or `app/views/users/index.css`. If it finds one, it will include it in the HTML for that view.
141
+
142
+ JSX is also supported for JavaScript and Typescript. Simply use the `.jsx` or `.tsx` extension instead of `.js` or `.ts`.
143
+
144
+ ### Usage
145
+
146
+ Simply create a JS and/or CSS file with the same name as any view, partial or layout.
147
+
148
+ Let's continue with our problem example above, where we have the following assets
149
+
150
+ - `/app/assets/application.css`
151
+ - `/app/assets/application.js`
152
+ - `/app/assets/users.css`
153
+ - `/app/assets/users.js`
154
+ - `/app/assets/user.js`
155
+
156
+ Your application layout is at `/app/views/layouts/application.hml.erb`, and the view that needs the users assets is at `/app/views/users/index.html.erb`, so move your assets JS and CSS alongside them:
157
+
158
+ - `/app/views/layouts/application.css`
159
+ - `/app/views/layouts/application.js`
160
+ - `/app/views/users/index.css`
161
+ - `/app/views/users/index.js`
162
+ - `/app/views/users/_user.js` (partial)
163
+
164
+ Now, in your layout and view, replace the `javascript_include_tag` and `stylesheet_link_tag` helpers with the `include_stylesheets` and `include_javascripts` helpers from Proscenium. Something like this:
165
+
166
+ ```erb
76
167
  <!DOCTYPE html>
77
168
  <html>
78
169
  <head>
79
170
  <title>Hello World</title>
80
- <%= side_load_stylesheets %>
171
+ <%= include_stylesheets %>
81
172
  </head>
82
173
  <body>
83
174
  <%= yield %>
84
- <%= side_load_javascripts defer: true, type: 'module' %>
175
+ <%= include_javascripts type: 'module', defer: true %>
85
176
  </body>
86
177
  </html>
87
178
  ```
88
179
 
89
- On each page request, Proscenium will check if your layout and view has a JS/TS/CSS file of the same
90
- name, and include them into your layout HTML. Partials are not side loaded.
180
+ > NOTE that Proscenium is desiged to work with modern JavaAscript, and assumes [ESModules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) are used everywhere. This is why the `type` attribute is set to `module` in the example above. If you are not using ESModules, then you can omit the `type` attribute.
181
+
182
+ On each page request, Proscenium will check if your views, layouts and partials have a JS/TS/CSS file of the same name, and then include them wherever your placed the `include_stylesheets` and `include_javascripts` helpers.
91
183
 
92
- Side loading is enabled by default, but you can disable it by setting `config.proscenium.side_load`
93
- to `false`.
184
+ Now you never have to remember to include your assets again. Just create them alongside your views, partials and layouts, and Proscenium will take care of the rest.
94
185
 
95
- ## Importing
186
+ Side loading is enabled by default, but you can disable it by setting `config.proscenium.side_load` to `false` in your `/config/application.rb`.
96
187
 
97
- Proscenium supports importing JS, JSX, TS and CSS from NPM, by URL, your local app, and even from Ruby Gems.
188
+ ## Importing Assets
189
+
190
+ Proscenium supports importing JS, JSX, TS, TSX, CSS and SVG from NPM, by URL, your local app, and even from Ruby Gems.
98
191
 
99
192
  Imported files are bundled together in real time. So no build step or pre-compilation is needed.
100
193
 
@@ -134,9 +227,23 @@ import utils from '/lib/utils'
134
227
  import constants from './constants'
135
228
  ```
136
229
 
137
- ## Import Map [WIP]
230
+ ```css /app/views/layouts/application.css
231
+ @import '/lib/reset';
232
+ ```
233
+
234
+ ```css /lib/reset.css
235
+ body {
236
+ /* some styles... */
237
+ }
238
+ ```
239
+
240
+ ## Import Maps
138
241
 
139
- [Import map](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap) for both JS and CSS is supported out of the box, and works with no regard to the browser being used. This is because the import map is parsed and resolved by Proscenium on the server. If you are not familiar with import maps, think of it as a way to define aliases.
242
+ > **[WIP]**
243
+
244
+ [Import maps](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap) for both JS and CSS is supported out of the box, and works with no regard to the browser being used. This is because the import map is parsed and resolved by Proscenium on the server, instead of by the browser. This is faster, and also allows you to use import maps in browsers that do not support them yet.
245
+
246
+ If you are not familiar with import maps, think of them as a way to define aliases.
140
247
 
141
248
  Just create `config/import_map.json` and specify the imports you want to use. For example:
142
249
 
@@ -166,7 +273,7 @@ and for CSS...
166
273
  @import '@radix-ui/colors/blue.css';
167
274
  ```
168
275
 
169
- You can also write your import map in Javascript instead of JSON. So instead of `config/import_map.json`, create `config/import_map.js`, and define an anonymous function. This function accepts a single `environment` argument.
276
+ You can also write your import map in JavaScript instead of JSON. So instead of `config/import_map.json`, create `config/import_map.js`, and define an anonymous function. This function accepts a single `environment` argument.
170
277
 
171
278
  ```js
172
279
  env => ({
@@ -176,21 +283,89 @@ env => ({
176
283
  })
177
284
  ```
178
285
 
179
- ## Importing SVG from JS(X) and TS(X)
286
+ ## Source Maps
287
+
288
+ Source maps can make it easier to debug your code. They encode the information necessary to translate from a line/column offset in a generated output file back to a line/column offset in the corresponding original input file. This is useful if your generated code is sufficiently different from your original code (e.g. your original code is TypeScript or you enabled minification). This is also useful if you prefer looking at individual files in your browser's developer tools instead of one big bundled file.
180
289
 
181
- Importing SVG from JS(X) will bundle the SVG source code. Additionally, if importing from JSX or TSX, the SVG source code will be rendered as a JSX/TSX component.
290
+ Source map output is supported for both JavaScript and CSS. Each file is appended with the link to the source map. For example:
291
+
292
+ ```js
293
+ //# sourceMappingURL=/app/views/layouts/application.js.map
294
+ ```
295
+
296
+ Your browsers dev tools should pick this up and automatically load the source map when and where needed.
297
+
298
+ ## SVG
299
+
300
+ You can import SVG from JS(X), which will bundle the SVG source code. Additionally, if importing from JSX or TSX, the SVG source code will be rendered as a JSX/TSX component.
182
301
 
183
302
  ## Environment Variables
184
303
 
185
- Import any environment variables into your JS(X) code.
304
+ > Available in `>=0.10.0`
305
+
306
+ You can define and access any environment variable from your JavaScript and Typescript under the `proscenium.env` namespace.
307
+
308
+ For performance and security reasons you must declare the environment variable names that you wish to expose in your `config/application.rb` file.
309
+
310
+ ```ruby
311
+ config.proscenium.env_vars = Set['API_KEY', 'SOME_SECRET_VARIABLE']
312
+ config.proscenium.env_vars << 'ANOTHER_API_KEY'
313
+ ```
314
+
315
+ This assumes that the environment variable of the same name has already been defined. If not, you will need to define it yourself either in your code using Ruby's `ENV` object, or in your shell.
316
+
317
+ These declared environment variables will be replaced with constant expressions, allowing you to use this like this:
318
+
319
+ ```js
320
+ console.log(proscenium.env.RAILS_ENV) // console.log("development")
321
+ console.log(proscenium.env.RAILS_ENV === 'development') // console.log(true)
322
+ ```
323
+
324
+ The `RAILS_ENV` and `NODE_ENV` environment variables will always automatically be declared for you.
325
+
326
+ In addition to this, Proscenium also provides a `process.env.NODE_ENV` variable, which is set to the same value as `proscenium.env.RAILS_ENV`. It is provided to support the community's existing tooling, which often relies on this variable.
327
+
328
+ Environment variables are particularly powerful in aiding [tree shaking](#tree-shaking).
186
329
 
187
330
  ```js
188
- import RAILS_ENV from '@proscenium/env/RAILS_ENV'
331
+ function start() {
332
+ console.log("start")
333
+ }
334
+ function doSomethingDangerous() {
335
+ console.log("resetDatabase")
336
+ }
337
+
338
+ proscenium.env.RAILS_ENV === "development" && doSomethingDangerous()
339
+
340
+ start()
189
341
  ```
190
342
 
191
- You can only access environment variables that are explicitly named. It will export `undefined` if the env variable does not exist.
343
+ In development the above code will be transformed into the following code, discarding the definition, and call to`doSomethingDangerous()`.
344
+
345
+ ```js
346
+ function start() {
347
+ console.log("start")
348
+ }
349
+ start()
350
+ ```
192
351
 
193
- ## Importing i18n
352
+ Please note that for security reasons environment variables are not replaced in URL imports.
353
+
354
+ An undefined environment variable will be replaced with `undefined`.
355
+
356
+ ```js
357
+ console.log(proscenium.env.UNKNOWN) // console.log((void 0).UNKNOWN)
358
+ ```
359
+
360
+ This means that code that relies on this will not be tree shaken. You can work around this by using the [optional chaining operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining):
361
+
362
+ ```js
363
+ if (typeof proscenium.env?.UNKNOWN !== "undefined") {
364
+ // do something if UNKNOWN is defined
365
+ }
366
+ ```
367
+
368
+ ## i18n
194
369
 
195
370
  Basic support is provided for importing your Rails locale files from `config/locales/*.yml`, exporting them as JSON.
196
371
 
@@ -226,6 +401,48 @@ function one() {
226
401
  one();
227
402
  ```
228
403
 
404
+ ### Code Splitting
405
+
406
+ > Available in `>=0.10.0`.
407
+
408
+ [Side loaded](#side-loading) assets are automatically code split. This means that if you have a file that is imported and used imported several times, and by different files, it will be split off into a separate file.
409
+
410
+ As an example:
411
+
412
+ ```js
413
+ // /lib/son.js
414
+ import father from "./father";
415
+
416
+ father() + " and Son";
417
+ ```
418
+
419
+ ```js
420
+ // /lib/daughter.js
421
+ import father from "./father";
422
+
423
+ father() + " and Daughter";
424
+ ```
425
+
426
+ ```js
427
+ // /lib/father.js
428
+ export default () => "Father";
429
+ ```
430
+
431
+ Both `son.js` and `daughter.js` import `father.js`, so both son and daughter would usually include a copy of father, resulting in duplicated code and larger bundle sizes.
432
+
433
+ If these files are side loaded, then `father.js` will be split off into a separate file or chunk, and only downloaded once.
434
+
435
+ - Code shared between multiple entry points is split off into a separate shared file that both entry points import. That way if the user first browses to one page and then to another page, they don't have to download all of the JavaScript for the second page from scratch if the shared part has already been downloaded and cached by their browser.
436
+
437
+ - Code referenced through an asynchronous `import()` expression will be split off into a separate file and only loaded when that expression is evaluated. This allows you to improve the initial download time of your app by only downloading the code you need at startup, and then lazily downloading additional code if needed later.
438
+
439
+ - Without code splitting, an import() expression becomes `Promise.resolve().then(() => require())` instead. This still preserves the asynchronous semantics of the expression but it means the imported code is included in the same bundle instead of being split off into a separate file.
440
+
441
+ Code splitting is enabled by default. You can disable it by setting the `code_splitting` configuration option to `false` in your application's `/config/application.rb`:
442
+ ```ruby
443
+ config.proscenium.code_splitting = false
444
+ ```
445
+
229
446
  ### JavaScript Caveats
230
447
 
231
448
  There are a few important caveats as far as JavaScript is concerned. These are [detailed on the esbuild site](https://esbuild.github.io/content-types/#javascript-caveats).
@@ -238,7 +455,7 @@ Note that by default, Proscenium's output will take advantage of all modern CSS
238
455
 
239
456
  The new CSS nesting syntax is supported, and transformed into non-nested CSS for older browsers.
240
457
 
241
- ### Importing from JavaScript
458
+ ### Importing CSS from JavaScript
242
459
 
243
460
  You can also import CSS from JavaScript. When you do this, Proscenium will automatically append each stylesheet to the document's head as a `<link>` element.
244
461
 
@@ -252,34 +469,74 @@ export let Button = ({ text }) => {
252
469
 
253
470
  ### CSS Modules
254
471
 
255
- Proscenium implements a subset of [CSS Modules](https://github.com/css-modules/css-modules). It supports the `:local` and `:global` keywords, but not the `composes` property. It is recommended that you use mixins instead of `composes`, as they work everywhere.
472
+ Proscenium implements a subset of [CSS Modules](https://github.com/css-modules/css-modules). It supports the `:local` and `:global` keywords, but not the `composes` property. (it is recommended that you use mixins instead of `composes`, as they will work everywhere, even in plain CSS files.)
256
473
 
257
- Give any CSS file a `.module.css` extension, and Proscenium will load it as a CSS Module...
474
+ Give any CSS file a `.module.css` extension, and Proscenium will treat it as a CSS Module, transforming all class names with a suffix unique to the file.
258
475
 
259
476
  ```css
260
- .header {
261
- background-color: #00f;
477
+ .title {
478
+ font-size: 20em;
262
479
  }
263
480
  ```
264
481
 
265
482
  The above input produces:
266
483
 
267
484
  ```css
268
- .header5564cdbb {
269
- background-color: #00f;
485
+ .title-5564cdbb {
486
+ font-size: 20em;
270
487
  }
271
488
  ```
272
489
 
273
- Importing a CSS file from JS will automatically append the stylesheet to the document's head. And the results of the import will be an object of CSS class to module names.
490
+ You now have a unique class name that you can use pretty much anywhere.
491
+
492
+ #### In your Views
493
+
494
+ You can reference CSS modules from your Rails views, partials, and layouts using the `css_module` helper, which accepts one or more class names, and will return the equivilent CSS module names - the class name with the unique suffix appended.
495
+
496
+ With [side-loading](#side-loading) setup, you can use the `css_module` helper as follows.
497
+
498
+ ```erb
499
+ <div>
500
+ <h1 class="<%= css_module :hello_title %>">Hello World</h1>
501
+ <p class="<%= css_module :body, paragraph: %>">
502
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit.
503
+ </p>
504
+ </div>
505
+ ```
506
+
507
+ `css_module` accepts multiple class names, and will return a space-separated string of transformed CSS module names.
508
+
509
+ ```ruby
510
+ css_module :my_module_name
511
+ # => "my_module_name-ABCD1234"
512
+ ```
513
+
514
+ You can even reference a class from any CSS file by passing the URL path to the file, as a prefix to the class name. Doing so will automatically [side load](#side-loading) the stylesheet.
515
+
516
+ ```ruby
517
+ css_module '/app/components/button.css@big_button'
518
+ # => "big_button"
519
+ ```
520
+
521
+ It also supports NPM packages (already installed in /node_modules):
522
+
523
+ ```ruby
524
+ css_module 'mypackage/button@big_button'
525
+ # => "big_button"
526
+ ```
527
+
528
+ #### In your JavaScript
529
+
530
+ Importing a CSS module from JS will automatically append the stylesheet to the document's head. And the result of the import will be an object of CSS class to module names.
274
531
 
275
532
  ```js
276
533
  import styles from './styles.module.css'
277
- // styles == { header: 'header5564cdbb' }
534
+ // styles == { header: 'header-5564cdbb' }
278
535
  ```
279
536
 
280
- It is important to note that the exported object of CSS module names is actually a Proxy object. So destructuring the object will not work. Instead, you must access the properties directly.
537
+ It is important to note that the exported object of CSS module names is actually a JavaScript [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) object. So destructuring the object will not work. Instead, you must access the properties directly.
281
538
 
282
- Also, importing a CSS module from another CSS module will result in the same digest string for all classes.
539
+ Also, importing a CSS module into another CSS module will result in the same digest string for all classes.
283
540
 
284
541
  ### CSS Mixins
285
542
 
@@ -377,13 +634,116 @@ console.log(version)
377
634
 
378
635
  ## Phlex Support
379
636
 
380
- *docs needed*
637
+ [Phlex](https://www.phlex.fun/) is a framework for building fast, reusable, testable views in pure Ruby. Proscenium works perfectly with Phlex, with support for side-loading, CSS modules, and more. Simply write your Phlex classes and inherit from `Proscenium::Phlex`.
638
+
639
+ ```ruby
640
+ class MyView < Proscenium::Phlex
641
+ def template
642
+ h1 { 'Hello World' }
643
+ end
644
+ end
645
+ ```
646
+
647
+ ### Side-loading
648
+
649
+ Any Phlex class that inherits `Proscenium::Phlex` will automatically be [side-loaded](#side-loading).
650
+
651
+ ### CSS Modules
652
+
653
+ [CSS Modules](#css-modules) are fully supported in Phlex classes, with access to the [`css_module` helper](#in-your-views) if you need it. However, there is a better and more seemless way to reference CSS module classes in your Phlex classes.
654
+
655
+ Within your Phlex classes, any class names that begin with `@` will be treated as a CSS module class.
656
+
657
+ ```ruby
658
+ # /app/views/users/show_view.rb
659
+ class Users::ShowView < Proscenium::Phlex
660
+ def template
661
+ h1 class: :@user_name do
662
+ @user.name
663
+ end
664
+ end
665
+ end
666
+ ```
667
+
668
+ ```css
669
+ /* /app/views/users/show_view.module.css */
670
+ .userName {
671
+ color: red;
672
+ font-size: 50px;
673
+ }
674
+ ```
675
+
676
+ In the above `Users::ShowView` Phlex class, the `@user_name` class will be resolved to the `userName` class in the `users/show_view.module.css` file.
677
+
678
+ The view above will be rendered something like this:
679
+
680
+ ```html
681
+ <h1 class="user_name-ABCD1234"></h1>
682
+ ```
683
+
684
+ You can of course continue to reference regular class names in your view, and they will be passed through as is. This will allow you to mix and match CSS modules and regular CSS classes in your views.
685
+
686
+ ```ruby
687
+ # /app/views/users/show_view.rb
688
+ class Users::ShowView < Proscenium::Phlex
689
+ def template
690
+ h1 class: :[@user_name, :title] do
691
+ @user.name
692
+ end
693
+ end
694
+ end
695
+ ```
696
+
697
+ ```html
698
+ <h1 class="user_name-ABCD1234 title">Joel Moss</h1>
699
+ ```
381
700
 
382
701
  ## ViewComponent Support
383
702
 
384
- *docs needed*
703
+ [ViewComponent](https://viewcomponent.org/) iA framework for creating reusable, testable & encapsulated view components, built to integrate seamlessly with Ruby on Rails. Proscenium works perfectly with ViewComponent, with support for side-loading, CSS modules, and more. Simply write your ViewComponent classes and inherit from `Proscenium::ViewComponent`.
385
704
 
386
- ## Cache Busting [*COMING SOON*]
705
+ ```ruby
706
+ class MyView < Proscenium::ViewComponent
707
+ def call
708
+ tag.h1 'Hello World'
709
+ end
710
+ end
711
+ ```
712
+
713
+ ### Side-loading
714
+
715
+ Any ViewComponent class that inherits `Proscenium::ViewComponent` will automatically be [side-loaded](#side-loading).
716
+
717
+ ### CSS Modules
718
+
719
+ [CSS Modules](#css-modules) are fully supported in ViewComponent classes, with access to the [`css_module` helper](#in-your-views) if you need it.
720
+
721
+ ```ruby
722
+ # /app/components/user_component.rb
723
+ class UserComponent < Proscenium::ViewComponent
724
+ def template
725
+ div.h1 @user.name, class: css_module(:user_name)
726
+ end
727
+ end
728
+ ```
729
+
730
+ ```css
731
+ /* # /app/components/user_component.module.css */
732
+ .userName {
733
+ color: red;
734
+ font-size: 50px;
735
+ }
736
+ ```
737
+
738
+ The view above will be rendered something like this:
739
+
740
+ ```html
741
+ <h1 class="user_name-ABCD1234">Joel Moss</h1>
742
+ ```
743
+
744
+ ## Cache Busting
745
+
746
+ > *COMING SOON*
387
747
 
388
748
  By default, all assets are not cached by the browser. But if in production, you populate the `REVISION` env variable, all CSS and JS URL's will be appended with its value as a query string, and the `Cache-Control` response header will be set to `public` and a max-age of 30 days.
389
749
 
@@ -403,17 +763,43 @@ The cache is set with a `max-age` of 30 days. You can customise this with the `c
403
763
  Rails.application.config.proscenium.cache_max_age = 12.months.to_i
404
764
  ```
405
765
 
406
- ## rjs is back!
766
+ ## rjs is back
407
767
 
408
768
  Proscenium brings back RJS! Any path ending in .rjs will be served from your Rails app. This allows you to import server rendered javascript.
409
769
 
410
- ## Serving from Ruby Gem
770
+ ## Resolution
771
+
772
+ Proscenium will serve files ending with any of these extension: `js,mjs,ts,css,jsx,tsx` from the following directories, and their sub-directories of your Rails application's root: `/app`, `/lib`, `/config`, `/node_modules`, `/vendor`.
773
+
774
+ So a file at `/app/views/users/index.js` will be served from `https://yourapp.com/app/views/users/index.js`.
775
+
776
+ You can continue to access any file in the `/public` directory as you normally would. Proscenium will not process files in the `/public` directory.
777
+
778
+ If requesting a file that exists in a root directory and the public directory, the file in the public directory will be served. For example, if you have a file at `/lib/foo.js` and `/public/lib/foo.js`, and you request `/lib/foo.js`, the file in the public directory (`/public/lib/foo.js`) will be served.
779
+
780
+ ### Assets from Rails Engines
781
+
782
+ Proscenium can serve assets from Rails Engines that are installed in your Rails app.
783
+
784
+ An engine that wants to expose its assets via Proscenium to the application must add Proscenium as a dependency, and add itself to the list of engines in the Proscenium config options `Proscenium.config.engines`.
785
+
786
+ For example, we have a gem called `gem1` that has Proscenium as a dependency, and exposes a Rails engine. It has some assets that it wants to expose to the application. To do this, it adds itself to the list of engines in the Proscenium config `engines` option:
787
+
788
+ ```ruby
789
+ class Gem1::Engine < ::Rails::Engine
790
+ config.proscenium.engines << self
791
+ end
792
+ ```
793
+
794
+ When this gem is installed in any Rails application, its assets will be available at the URL `/gem1/...`. For example, if the gem has a file `lib/styles.css`, it can be requested at `/gem1/lib/styles.css`.
795
+
796
+ The same directories and file extensions are supported as for the application itself.
411
797
 
412
- *docs needed*
798
+ It is important to note that the application takes precedence over the gem. So if the application has a file at `/public/gem1/lib/styles.css`, and the gem also has a file at `/lib/styles.css`, then the file in the application will be served. This is because both files would be accessible at the same URL: `/gem1/lib/styles.css`.
413
799
 
414
- ## Thanks 🙏
800
+ ## Thanks
415
801
 
416
- HUGE thanks go to [Evan Wallace](https://github.com/evanw) and his amazing [esbuild](https://esbuild.github.io/) project. Proscenium would not be possible without it, and it is esbuild that makes this so fast and efficient.
802
+ HUGE thanks 🙏 go to [Evan Wallace](https://github.com/evanw) and his amazing [esbuild](https://esbuild.github.io/) project. Proscenium would not be possible without it, and it is esbuild that makes this so fast and efficient.
417
803
 
418
804
  Because Proscenium uses esbuild extensively, some of these docs are taken directly from the esbuild docs, with links back to the [esbuild site](https://esbuild.github.io/) where appropriate.
419
805
 
@@ -430,7 +816,7 @@ bundle exec rake compile:local
430
816
  We have tests for both Ruby and Go. To run the Ruby tests:
431
817
 
432
818
  ```bash
433
- bundle exec rake test
819
+ bundle exec sus
434
820
  ```
435
821
 
436
822
  To run the Go tests:
@@ -447,7 +833,7 @@ go test ./internal/builder -bench=. -run="^$" -count=10 -benchmem
447
833
 
448
834
  ## Contributing
449
835
 
450
- Bug reports and pull requests are welcome on GitHub at https://github.com/joelmoss/proscenium. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/joelmoss/proscenium/blob/master/CODE_OF_CONDUCT.md).
836
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/joelmoss/proscenium>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/joelmoss/proscenium/blob/master/CODE_OF_CONDUCT.md).
451
837
 
452
838
  ## License
453
839