importmap-rails 0.5.3 → 0.6.3
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 +10 -6
- data/lib/importmap/engine.rb +0 -6
- data/lib/importmap/map.rb +20 -2
- data/lib/importmap/version.rb +1 -1
- data/lib/install/install.rb +4 -2
- metadata +2 -3
- data/app/assets/javascripts/es-module-shims@0.14.0.csp.js +0 -1635
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e83ed6ee74e2b39acc314afedb47331aa6499f73cefd377813d35042c5edef99
|
4
|
+
data.tar.gz: 2892356151da356c2ca221d9cdb34742c7f258641481079f32c7e3a65b92155b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a31bde4420db119b939f660310262b2ce56bf9dc2dba8c6a255c565ce99d127361584917e378445b6a500675295a502912e49e772aec37a95d32b1d51d9def85
|
7
|
+
data.tar.gz: 79dbc56c9c96dd85ef65d9d0cca3baee1c1e0c8e80e25f99b3823dd1828a20297261ea7fb21f13c7dea980c13358c9872cb09b2217704c293f260b17d11a510f
|
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).
|