Skip to content

Commit fa34a62

Browse files
authored
Merge pull request #95 from Rotario/main
Update README to mention pin_all_from for local modules
2 parents 59c48f2 + a66e8a0 commit fa34a62

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ It's in `app/javascript/application.js` you setup your application by importing
6363

6464
It makes sense to use logical names that match the package names used by npm, such that if you later want to start transpiling or bundling your code, you won't have to change any module imports.
6565

66+
### Local modules
67+
68+
If you want to import local js module files from `app/javascript/src` or other sub-folders of `app/javascript` (such as `channels`), you must pin these to be able to import them.
69+
You can use `pin_all_from` to pick all files in a specific folder, so you don't have to `pin` each module individually.
70+
71+
```rb
72+
# config/importmap.rb
73+
pin_all_from 'app/javascript/src`, under: 'src', to: 'src'
74+
```
75+
76+
The `:to` parameter is only required if you want to change the destination logical import name. If you drop the :to option, you must place the :under option directly after the first parameter.
77+
78+
Allows you to:
79+
80+
```js
81+
// app/javascript/application.js
82+
import { ExampleFunction } from 'src/example_function'
83+
```
84+
Which imports the function from `app/javascript/src/example_function.js`.
85+
86+
Note: Sprockets used to serve assets (albeit without filename digests) it couldn't find from the `app/javascripts` folder with logical relative paths, meaning pinning local files wasn't needed. Propshaft doesn't have this fallback, so when you use Propshaft you have to pin your local modules.
6687

6788
## Using npm packages via JavaScript CDNs
6889

0 commit comments

Comments
 (0)