Skip to content

Commit a711ebb

Browse files
authored
docs: explain "why" importmaps + how they work. (#63)
* docs: explain "why" importmaps + how they work. * Update README.md * Update README.md * Update README.md
1 parent da691cc commit a711ebb

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,42 @@ Importmap for Rails is automatically included in Rails 7+ for new applications,
1717

1818
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.
1919

20+
## How do importmaps work?
21+
22+
At their core, importmaps are essentially a string substitution for what are referred to as "bare module specifiers". A "bare module specifier" looks like this: `import React from "react"`. This is not compatible with the ES Module loader spec. Instead, to be ESM compatible, you must provide 1 of the 3 following types of specifiers:
23+
24+
- Absolute path:
25+
```js
26+
import React from "/Users/DHH/projects/basecamp/node_modules/react"
27+
```
28+
29+
- Relative path:
30+
```js
31+
import React from "./node_modules/react"
32+
```
33+
34+
- HTTP path:
35+
```js
36+
import React from "https://ga.jspm.io/npm:[email protected]/index.js"
37+
```
38+
39+
Importmap-rails provides a clean API for mapping "bare module specifiers" like `"react"`
40+
to 1 of the 3 viable ways of loading ES Module javascript packages.
41+
42+
For example:
43+
44+
```rb
45+
# config/importmaps.rb
46+
pin "react" to "https://ga.jspm.io/npm:[email protected]/index.js"
47+
```
48+
49+
means "everytime you see `import React from "react"`
50+
change it to `import React from "https://ga.jspm.io/npm:[email protected]/index.js"`"
51+
52+
```js
53+
import React from "react"
54+
// => import React from "https://ga.jspm.io/npm:[email protected]/index.js"
55+
```
2056

2157
## Usage
2258

0 commit comments

Comments
 (0)