importmap-rails 0.6.1 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -4
- data/app/assets/javascripts/es-module-shims.js +768 -1
- data/app/helpers/importmap/importmap_tags_helper.rb +4 -4
- data/lib/importmap/commands.rb +1 -1
- data/lib/importmap/engine.rb +20 -1
- data/lib/importmap/map.rb +16 -1
- data/lib/importmap/reloader.rb +1 -1
- data/lib/importmap/version.rb +1 -1
- data/lib/install/install.rb +4 -2
- data/lib/tasks/importmap_tasks.rake +1 -1
- metadata +16 -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: 6f178f76220fe01a13e2d79033cabc5261f790102abdd8d0209741cb8c7a6b17
|
4
|
+
data.tar.gz: 450c1fd3351c4caf38a88e58ad9c3d9cd25b919c3cafab86e5acbcf977ab6e38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e119612311447feb13be274fb897a44f1a25d08d12287a66ecd8661638e4884c4f79b6d275e630c0e1661f70a5b9753d57c1e33d8dcbc605a5470fb2add8bc0b
|
7
|
+
data.tar.gz: b647688c8a0f634d97d9a3204673cb43eab80086ab3313debce1faee335d0f1abc0e22aada01552fc6f756a9a6233bd64c7dd949933b14fe5beaaa9312679fd4
|
data/README.md
CHANGED
@@ -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.
|
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.
|
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.
|
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
|
@@ -175,10 +175,16 @@ If you're using etags generated by Rails helpers like `stale?` or `fresh_when`,
|
|
175
175
|
|
176
176
|
```ruby
|
177
177
|
class ApplicationController < ActionController::Base
|
178
|
-
etag { Rails.application.
|
178
|
+
etag { Rails.application.importmap.digest(resolver: helpers) if request.format&.html? }
|
179
179
|
end
|
180
180
|
```
|
181
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
|
+
|
182
188
|
## Expected errors from using the es-module-shim
|
183
189
|
|
184
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.
|
@@ -189,6 +195,11 @@ While import maps are native in Chrome and Edge, they need a shim in other brows
|
|
189
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.
|
190
196
|
|
191
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
|
+
|
192
203
|
## License
|
193
204
|
|
194
205
|
Importmap for Rails is released under the [MIT License](https://opensource.org/licenses/MIT).
|