importmap-rails 1.0.2 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/README.md +44 -4
- data/app/assets/javascripts/es-module-shims.js +838 -786
- 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/commands.rb +59 -0
- data/lib/importmap/map.rb +18 -23
- data/lib/importmap/npm.rb +120 -0
- data/lib/importmap/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c556b347ccc13c5ba397acdde373ee8b705cec57b267cd0fbcbf920b7e8b6f6e
|
4
|
+
data.tar.gz: 475f9c49a29536cef24f67cf70dbd1a1611a20c292bba384418448274c477650
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d12e8298a133d3362469bf9886d4bfe1d119b734b8f7a0766049e28c28e9a239d72ca53353ba58e03f4c32fdc7a0f1a60604829231588b829a31aa33b3ca4318
|
7
|
+
data.tar.gz: 22e6b50bcf18c1bc4979d1160fad4fb88483ae19268ae9cd9f41c30d144437f5dbc9bf0588419106edd2747f9b8ccad886a27dfd556881327626eac4569e4c21
|
data/MIT-LICENSE
CHANGED
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 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
|
-
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
|
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 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
|
|
@@ -11,9 +11,8 @@ There's [native support for import maps in Chrome/Edge 89+](https://caniuse.com/
|
|
11
11
|
|
12
12
|
Importmap for Rails is automatically included in Rails 7+ for new applications, but you can also install it manually in existing applications:
|
13
13
|
|
14
|
-
1.
|
15
|
-
2. Run `./bin/
|
16
|
-
3. Run `./bin/rails importmap:install`
|
14
|
+
1. Run `./bin/bundle add importmap-rails`
|
15
|
+
2. Run `./bin/rails importmap:install`
|
17
16
|
|
18
17
|
Note: In order to use JavaScript from Rails frameworks like Action Cable, Action Text, and Active Storage, you must be running Rails 7.0+. This was the first version that shipped with ESM compatible builds of these libraries.
|
19
18
|
|
@@ -224,6 +223,41 @@ pin_all_from File.expand_path("../app/assets/javascripts", __dir__)
|
|
224
223
|
```
|
225
224
|
|
226
225
|
|
226
|
+
## Selectively importing modules
|
227
|
+
|
228
|
+
You can selectively import your javascript modules on specific pages.
|
229
|
+
|
230
|
+
Create your javascript in `app/javascript`:
|
231
|
+
|
232
|
+
```js
|
233
|
+
// /app/javascript/checkout.js
|
234
|
+
// some checkout specific js
|
235
|
+
```
|
236
|
+
|
237
|
+
Pin your js file:
|
238
|
+
|
239
|
+
```rb
|
240
|
+
# config/importmap.rb
|
241
|
+
# ... other pins...
|
242
|
+
pin "checkout"
|
243
|
+
```
|
244
|
+
|
245
|
+
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.
|
246
|
+
|
247
|
+
```erb
|
248
|
+
<% content_for :head do %>
|
249
|
+
<%= javascript_import_module_tag "checkout" %>
|
250
|
+
<% end %>
|
251
|
+
```
|
252
|
+
|
253
|
+
**Important**: The `javascript_import_module_tag` should come after your `javascript_importmap_tags`
|
254
|
+
|
255
|
+
```erb
|
256
|
+
<%= javascript_importmap_tags %>
|
257
|
+
<%= yield(:head) %>
|
258
|
+
```
|
259
|
+
|
260
|
+
|
227
261
|
## Include a digest of the import map in your ETag
|
228
262
|
|
229
263
|
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:
|
@@ -265,6 +299,12 @@ In Firefox. when opening the browser console, the asm.js module lexer build will
|
|
265
299
|
|
266
300
|
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. You 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.
|
267
301
|
|
302
|
+
## Checking for outdated or vulnerable packages
|
303
|
+
|
304
|
+
Importmap for Rails provides two commands to check your pinned packages:
|
305
|
+
- `./bin/importmap outdated` checks the NPM registry for new versions
|
306
|
+
- `./bin/importmap audit` checks the NPM registry for known security issues
|
307
|
+
|
268
308
|
## License
|
269
309
|
|
270
310
|
Importmap for Rails is released under the [MIT License](https://opensource.org/licenses/MIT).
|