importmap-rails 0.9.5 → 1.0.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 +37 -2
- data/app/assets/javascripts/es-module-shims.js +793 -778
- data/app/assets/javascripts/es-module-shims.js.map +1 -1
- data/app/assets/javascripts/es-module-shims.min.js +1 -1
- data/lib/importmap/map.rb +29 -9
- data/lib/importmap/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53d756788e46fd583d5f4dc1b813f11b8479237e766a556fbcbabebf4285ffdd
|
4
|
+
data.tar.gz: 640b9d1cce345d5193e12f17400c772532ddbc4fd97488567e9988eb3ac81fd7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 77a669609b58956d33610ebf3053e0ebe73bdfc1d79d1580df70f1f4d60f96ff92fec0dd0484bac01fe9ed678aac48a07da45402d0209c8a1869a3aece8db85a
|
7
|
+
data.tar.gz: 35e0355d199c75f066f6ce2bea61ceceec0d25e15b6cf10b5d1a052b583580413ab99c104a8a11435d72841b1675815d7535d3c44c976e51a2f3bf6e7910677e
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Importmap for Rails
|
2
2
|
|
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.
|
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 ES modules (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
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
|
|
@@ -42,7 +42,7 @@ to 1 of the 3 viable ways of loading ES Module javascript packages.
|
|
42
42
|
For example:
|
43
43
|
|
44
44
|
```rb
|
45
|
-
# config/
|
45
|
+
# config/importmap.rb
|
46
46
|
pin "react", to: "https://ga.jspm.io/npm:react@17.0.2/index.js"
|
47
47
|
```
|
48
48
|
|
@@ -224,6 +224,41 @@ pin_all_from File.expand_path("../app/assets/javascripts", __dir__)
|
|
224
224
|
```
|
225
225
|
|
226
226
|
|
227
|
+
## Selectively importing modules
|
228
|
+
|
229
|
+
You can selectively import your javascript modules on specific pages.
|
230
|
+
|
231
|
+
Create your javascript in `app/javascript`:
|
232
|
+
|
233
|
+
```js
|
234
|
+
// /app/javascript/checkout.js
|
235
|
+
// some checkout specific js
|
236
|
+
```
|
237
|
+
|
238
|
+
Pin your js file:
|
239
|
+
|
240
|
+
```rb
|
241
|
+
# config/importmap.rb
|
242
|
+
# ... other pins...
|
243
|
+
pin "checkout"
|
244
|
+
```
|
245
|
+
|
246
|
+
Import your module on the specific page. Note: you'll likely want to use a `content_for` block on the specifc page/partial, then yield it in your layout.
|
247
|
+
|
248
|
+
```erb
|
249
|
+
<% content_for :head do %>
|
250
|
+
<%= javascript_import_module_tag "checkout" %>
|
251
|
+
<% end %>
|
252
|
+
```
|
253
|
+
|
254
|
+
**Important**: The `javacript_import_module_tag` should come after your `javascript_importmap_tags`
|
255
|
+
|
256
|
+
```erb
|
257
|
+
<%= javascript_importmap_tags %>
|
258
|
+
<%= yield(:head) %>
|
259
|
+
```
|
260
|
+
|
261
|
+
|
227
262
|
## Include a digest of the import map in your ETag
|
228
263
|
|
229
264
|
If you're using [ETags](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag) 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:
|