You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+3-85
Original file line number
Diff line number
Diff line change
@@ -86,91 +86,12 @@ Note: Sprockets used to serve assets (albeit without filename digests) it couldn
86
86
87
87
## Using npm packages via JavaScript CDNs
88
88
89
-
Importmap for Rails is designed to be used with JavaScript CDNs for your npm package dependencies. The CDNs provide pre-compiled distribution versions ready to use, and offer a fast, efficient way of serving them.
89
+
Importmap for Rails downloads and vendors your npm package dependencies via JavaScript CDNs that provide pre-compiled distribution versions.
90
90
91
91
You can use the `./bin/importmap` command that's added as part of the install to pin, unpin, or update npm packages in your import map. This command uses an API from [JSPM.org](https://jspm.org) to resolve your package dependencies efficiently, and then add the pins to your `config/importmap.rb` file. It can resolve these dependencies from JSPM itself, but also from other CDNs, like [unpkg.com](https://unpkg.com) and [jsdelivr.com](https://www.jsdelivr.com).
92
92
93
-
It works like so:
94
-
95
-
```bash
96
-
./bin/importmap pin react react-dom
97
-
Pinning "react" to https://ga.jspm.io/npm:react@17.0.2/index.js
98
-
Pinning "react-dom" to https://ga.jspm.io/npm:react-dom@17.0.2/index.js
99
-
Pinning "object-assign" to https://ga.jspm.io/npm:object-assign@4.1.1/index.js
100
-
Pinning "scheduler" to https://ga.jspm.io/npm:scheduler@0.20.2/index.js
As you can see, the two packages react and react-dom resolve to a total of four dependencies, when resolved via the jspm default.
116
-
117
-
Now you can use these in your application.js entrypoint like you would any other module:
118
-
119
-
```js
120
-
importReactfrom"react"
121
-
importReactDOMfrom"react-dom"
122
-
```
123
-
124
-
You can also designate a specific version to pin:
125
-
126
-
```bash
127
-
./bin/importmap pin react@17.0.1
128
-
Pinning "react" to https://ga.jspm.io/npm:react@17.0.1/index.js
129
-
Pinning "object-assign" to https://ga.jspm.io/npm:object-assign@4.1.1/index.js
130
-
```
131
-
132
-
Or even remove pins:
133
-
134
-
```bash
135
-
./bin/importmap unpin react
136
-
Unpinning "react"
137
-
Unpinning "object-assign"
138
-
```
139
-
140
-
If you pin a package that has already been pinned, it'll be updated inline, along with its dependencies.
141
-
142
-
You can control the environment of the package for packages with separate "production" (the default) and "development" builds:
143
-
144
93
```bash
145
-
./bin/importmap pin react --env development
146
-
Pinning "react" to https://ga.jspm.io/npm:react@17.0.2/dev.index.js
147
-
Pinning "object-assign" to https://ga.jspm.io/npm:object-assign@4.1.1/index.js
148
-
```
149
-
150
-
You can also pick an alternative, supported CDN provider when pinning, like `unpkg` or `jsdelivr` (`jspm` is the default):
151
-
152
-
```bash
153
-
./bin/importmap pin react --from jsdelivr
154
-
Pinning "react" to https://cdn.jsdelivr.net/npm/react@17.0.2/index.js
155
-
```
156
-
157
-
Remember, though, that if you switch a pin from one provider to another, you may have to clean up dependencies added by the first provider that isn't used by the second provider.
158
-
159
-
Run `./bin/importmap` to see all options.
160
-
161
-
Note that this command is merely a convenience wrapper to resolving logical package names to CDN URLs. You can also just lookup the CDN URLs yourself, and then pin those. For example, if you wanted to use Skypack for React, you could just add the following to `config/importmap.rb`:
162
-
163
-
```ruby
164
-
pin "react", to:"https://cdn.skypack.dev/react"
165
-
```
166
-
167
-
168
-
## Downloading vendor files from the JavaScript CDN
169
-
170
-
If you don't want to use a JavaScript CDN in production, you can also download vendored files from the CDN when you're setting up your pins:
171
-
172
-
```bash
173
-
./bin/importmap pin react --download
94
+
./bin/importmap pin react
174
95
Pinning "react" to vendor/react.js via download from https://ga.jspm.io/npm:react@17.0.2/index.js
175
96
Pinning "object-assign" to vendor/object-assign.js via download from https://ga.jspm.io/npm:object-assign@4.1.1/index.js
The packages are downloaded to `vendor/javascript`, which you can check into your source control, and they'll be available through your application's own asset pipeline serving.
186
107
187
-
If you later wish to remove a downloaded pin, you again pass `--download`:
108
+
If you later wish to remove a downloaded pin:
188
109
189
110
```bash
190
111
./bin/importmap unpin react --download
191
112
Unpinning and removing "react"
192
113
Unpinning and removing "object-assign"
193
114
```
194
115
195
-
Just like with a normal pin, you can also update a pin by running the `pin --download` command again.
196
-
197
-
198
116
## Preloading pinned modules
199
117
200
118
To avoid the waterfall effect where the browser has to load one file after another before it can get to the deepest nested import, importmap-rails supports [modulepreload links](https://developers.google.com/web/updates/2017/12/modulepreload). Pinned modules can be preloaded by appending `preload: true` to the pin.
0 commit comments