importmap-rails 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 446689178bd41da319ca7d85a90090d8aaaebcda8ba79fb8216fc42a1948af4b
4
- data.tar.gz: d856a84a0acf723fe91b75ccb4e3f67727a5e1f64bde871a8604a79892cdc79c
3
+ metadata.gz: 041d46b392f2efffb635e2f9f25618564d6dd2f10b7fc79f1c20a1deb300ba91
4
+ data.tar.gz: c83a93b7c61426faa75190fdd9582cd05b4cd1f87a1a85d1ae1b0d19a5603d45
5
5
  SHA512:
6
- metadata.gz: cf025187c8d441b30e2c2d685c114063ef768132952a42038e94d99d42016af8e094586fe5939840cb42c99b449e91070c1b6666e0ed9d3167123a80a097c51c
7
- data.tar.gz: 1594f8e3c0bb824fbfd33bb04688a101330af87c91ad2bf3df3fd4531f0821d3b2f457e53a63ff03544307e0bd7199e2208718dda712ff50db1a5350535d4f60
6
+ metadata.gz: c1c6ac59101df2d7fadf3a9dc7e72bb5cfff9cfc74aa9e5672a3c5a3c08aaa29e976dbd9dbddd557b0f751531016ad196bf966c2899984bd16f8275e7968a49e
7
+ data.tar.gz: f9b6cb16445170034a0ca87f08d08321c8ab571dbaf851e0d7e561f8051bd6da11dcd0431e7b1ba4cc8fd0bf5b5acd5f52ecd4198a777cf0e3a450f68b11eb25
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [Import maps](https://github.com/WICG/import-maps) let you import JavaScript modules using logical names that map to versioned/digested files – directly from the browser. So you can [build modern JavaScript applications using JavaScript libraries made for ESM without the need for transpiling or bundling](https://world.hey.com/dhh/modern-web-apps-without-javascript-bundling-or-transpiling-a20f2755).This frees you from needing Webpack, Yarn, npm, or any other part of the JavaScript toolchain. All you need is the asset pipeline that's already included in Rails.
4
4
 
5
- With this approach you'll ship many small JavaScript files instead of one big JavaScript file. Thanks to HTTP2 that no longer carries a material performance penalty during the initial transport, and in fact offers substantial benefits over the long run due to better caching dynamics. Whereas before any change to any JavaScript file included in your big bundle would invalidate the cache for the the whole bundle, now only the cache for that single file is invalidated.
5
+ With this approach you'll ship many small JavaScript files instead of one big JavaScript file. Thanks to HTTP/2 that no longer carries a material performance penalty during the initial transport, and in fact offers substantial benefits over the long run due to better caching dynamics. Whereas before any change to any JavaScript file included in your big bundle would invalidate the cache for the the whole bundle, now only the cache for that single file is invalidated.
6
6
 
7
7
  There's [native support for import maps in Chrome/Edge 89+](https://caniuse.com/?search=importmap), and [a shim available](https://github.com/guybedford/es-module-shims) for any browser with basic ESM support. So your app will be able to work with all the evergreen browsers.
8
8
 
@@ -20,7 +20,7 @@ Note: In order to use JavaScript from Rails frameworks like Action Cable, Action
20
20
 
21
21
  ## Usage
22
22
 
23
- The import map is setup through `Rails.application.config.importmap` via the configuration in `config/importmap.rb`. This file is automatically reloaded in development upon changes, but note that you must restart the server if you remove pins and need them gone from the rendered importmap or list of preloads.
23
+ The import map is setup through `Rails.application.importmap` via the configuration in `config/importmap.rb`. This file is automatically reloaded in development upon changes, but note that you must restart the server if you remove pins and need them gone from the rendered importmap or list of preloads.
24
24
 
25
25
  This import map is inlined in the `<head>` of your application layout using `<%= javascript_importmap_tags %>`, which will setup the JSON configuration inside a `<script type="importmap">` tag. After that, the [es-module-shim](https://github.com/guybedford/es-module-shims) is loaded, and then finally the application entrypoint is imported via `<script type="module">import "application"</script>`. That logical entrypoint, `application`, is mapped in the importmap script tag to the file `app/javascript/application.js`.
26
26
 
@@ -143,9 +143,9 @@ pin "md5", to: "https://cdn.jsdelivr.net/npm/md5@2.3.0/md5.js", preload: false
143
143
 
144
144
  ## Composing import maps
145
145
 
146
- By default, Rails loads import map definition from the application's `config/importmap.rb` to the `Importmap::Map` object available at `Rails.application.config.importmap`.
146
+ By default, Rails loads import map definition from the application's `config/importmap.rb` to the `Importmap::Map` object available at `Rails.application.importmap`.
147
147
 
148
- You can combine multiple import maps by drawing their definitions onto the `Rails.application.config.importmap`. For example, appending import maps defined in Rails engines:
148
+ You can combine multiple import maps by drawing their definitions onto the `Rails.application.importmap`. For example, appending import maps defined in Rails engines:
149
149
 
150
150
  ```ruby
151
151
  # my_engine/lib/my_engine/engine.rb
@@ -168,11 +168,38 @@ And pinning JavaScript modules from the engine:
168
168
  pin_all_from File.expand_path("../app/assets/javascripts", __dir__)
169
169
  ```
170
170
 
171
+
172
+ ## Include a digest of the import map in your etag
173
+
174
+ If you're using etags generated by Rails helpers like `stale?` or `fresh_when`, you need to include the digest of the import map into this calculation. Otherwise your application will return 302 cache responses even when your JavaScript assets have changed. You can avoid this with something like:
175
+
176
+ ```ruby
177
+ class ApplicationController < ActionController::Base
178
+ etag { Rails.application.importmap.digest(resolver: helpers) if request.format&.html? }
179
+ end
180
+ ```
181
+
182
+
183
+ ## Sweeping the cache in development and test
184
+
185
+ Generating the import map json and modulepreloads may require resolving hundreds of assets. This can take a while, so these operations are cached, but in development and test, we watch for changes to both `config/importmap.rb` and files in `app/javascript` to clear this cache. This feature can be controlled in an environment configuration file via the boolean `config.importmap.sweep_cache`. If you're pinning local files from outside of `app/javascript`, you'll need to restart your development server upon changes.
186
+
187
+
171
188
  ## Expected errors from using the es-module-shim
172
189
 
173
190
  While import maps are native in Chrome and Edge, they need a shim in other browsers that'll produce a JavaScript console error like `TypeError: Module specifier, 'application' does not start with "/", "./", or "../".`. This error is normal and does not have any user-facing consequences.
174
191
 
175
192
 
193
+ ## Turning off the shim
194
+
195
+ Under certain circumstances, like running system tests using chromedriver under CI (which may be resource constrained and trigger errors in certain cases), you may want to explicitly turn off including the shim. If can do this by calling the bulk tag helper with `javascript_importmap_tags("application", shim: false)`. Thus you can pass in something like `shim: !ENV["CI"]`. If you want, and are sure you're not doing any full-page caching, you can also connect this directive to a user agent check (using a gem like `useragent`) to check whether the browser is chrome/edge 89+. But you really shouldn't have to, as the shim is designed to gracefully work with natively compatible drivers.
196
+
197
+
198
+ ## A note about browser extensions
199
+
200
+ Certain extensions that also load Javascript modules may block import maps from being loaded (for instance, the Apollo Client Devtools extension). If you see a console message like `An import map is added after module script load was triggered`, browser extensions are likely the culprit.
201
+
202
+
176
203
  ## License
177
204
 
178
205
  Importmap for Rails is released under the [MIT License](https://opensource.org/licenses/MIT).