importmap-rails 0.5.2 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -3
- data/app/assets/javascripts/es-module-shims.js +768 -1
- data/app/helpers/importmap/importmap_tags_helper.rb +18 -6
- data/lib/importmap/engine.rb +0 -6
- data/lib/importmap/map.rb +20 -2
- data/lib/importmap/version.rb +1 -1
- metadata +2 -3
- data/app/assets/javascripts/es-module-shims@0.12.8.js +0 -698
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66bfab7feb560eed6e33b6fddac090f10ffe0d66604257953083294c6fe1eb43
|
4
|
+
data.tar.gz: 1aebac7c0ac6d5e2d241c31804730bf986dd1d6a095a0b930afbdb132c9b77e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad6c520f2677cb7b3322556258157c1abccc8bcacb21059803a7102bcd04ae1c047282febca97893b1f1b5cba4237d802ca1c2ea0b64b3cb09c3f26c6f7539af
|
7
|
+
data.tar.gz: ac70454957bfdb66c5316d3662f0cd44c5792676bea3f3c10b1ad2a85bb44d136cb15424729d8793350f5db515ed7706858c481c37718b71e4e7bd56507ce057
|
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
|
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
|
|
@@ -168,16 +168,30 @@ And pinning JavaScript modules from the engine:
|
|
168
168
|
pin_all_from File.expand_path("../app/assets/javascripts", __dir__)
|
169
169
|
```
|
170
170
|
|
171
|
-
## Caching the import map and preload modules
|
172
171
|
|
173
|
-
|
172
|
+
## Include a digest of the import map in your etag
|
174
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.config.importmap.digest(resolver: helpers) if request.format&.html? }
|
179
|
+
end
|
180
|
+
```
|
175
181
|
|
176
182
|
## Expected errors from using the es-module-shim
|
177
183
|
|
178
184
|
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.
|
179
185
|
|
180
186
|
|
187
|
+
## Turning off the shim
|
188
|
+
|
189
|
+
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.
|
190
|
+
|
191
|
+
## A note about browser extensions
|
192
|
+
|
193
|
+
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.
|
194
|
+
|
181
195
|
## License
|
182
196
|
|
183
197
|
Importmap for Rails is released under the [MIT License](https://opensource.org/licenses/MIT).
|