Skip to content

Commit

Permalink
Merge pull request #95 from Rotario/main
Browse files Browse the repository at this point in the history
Update README to mention pin_all_from for local modules
  • Loading branch information
rafaelfranca authored Oct 27, 2023
2 parents 59c48f2 + a66e8a0 commit fa34a62
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ It's in `app/javascript/application.js` you setup your application by importing

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.

### Local modules

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.
You can use `pin_all_from` to pick all files in a specific folder, so you don't have to `pin` each module individually.

```rb
# config/importmap.rb
pin_all_from 'app/javascript/src`, under: 'src', to: 'src'
```
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.
Allows you to:
```js
// app/javascript/application.js
import { ExampleFunction } from 'src/example_function'
```
Which imports the function from `app/javascript/src/example_function.js`.
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.

## Using npm packages via JavaScript CDNs

Expand Down

0 comments on commit fa34a62

Please sign in to comment.