Search code examples
javascriptruby-on-railsrubyruby-on-rails-7import-maps

Rails 7 Import Maps - import folder with custom scripts


playing around with Rails 7 and import maps. Trying to import a folder with some custom JS.

# config/importmap.rb
pin_all_from "app/javascript/custom", under: "custom"

# app/javascript/application.js
import "custom"

Gives Uncaught TypeError: Failed to resolve module specifier "custom" on Chrome and Uncaught Error: Unable to resolve specifier 'custom' from [shim] on Firefox

interestingly import "custom/script" works fine.

what am I doing wrong?


Solution

  • I had this same problem and found the following solution:

    # app/javascript/application.js
    import "custom/my_custom_code.js"
    

    This means that pin_all_from "app/javascript/custom", under: "custom" is really just making each custom JS file available under the custom namespace. Therefore, you still need to import each file individually.