Skip to content

Confusion between the :to and :under options of pin_all_from #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
radiantshaw opened this issue Nov 11, 2021 · 1 comment
Closed

Confusion between the :to and :under options of pin_all_from #67

radiantshaw opened this issue Nov 11, 2021 · 1 comment

Comments

@radiantshaw
Copy link
Contributor

I was testing the pin_all_from method and found what might be a potential issue. pin_all_from requires both the :to and :under options to be passed for this test case to pass:

test "directory pin under custom asset path" do
assert_match %r|assets/spina/controllers/another_controller-.*\.js|, generate_importmap_json["imports"]["controllers/spina/another_controller"]
assert_match %r|assets/spina/controllers/deeper/again_controller-.*\.js|, generate_importmap_json["imports"]["controllers/spina/deeper/again_controller"]
end

This is also apparent by removing the line:

pin_all_from "app/javascript/spina/controllers", under: "controllers/spina", preload: true

from the setup and noticing that the tests still pass. I also verified it by creating a new Rails 7 app and adding both the lines:

pin_all_from "app/javascript/spina/controllers", under: "controllers/spina", preload: true
pin_all_from "app/javascript/spina/controllers", under: "controllers/spina", to: "spina/controllers", preload: true

to config/importmap.rb and running bin/importmap json. Commenting out the second line doesn't generate any imports but commenting out the first line does.

@radiantshaw
Copy link
Contributor Author

I think I figured out what's going on. The :under option to pin_all_from is used to find the "path" of the target file (processed file made available inside public/assets via the pipeline):

[ mapping.path || mapping.under, filename.to_s ].compact.join("/")

But, this only works if the value passed to :under is a suffix of the dir parameter (the first parameter to pin_all_from):

pin_all_from "app/javascript/spina/controllers", under: "spina/controllers"

If the value to :under is different (such as "controllers/spina"), then we have to pass the :to option, which will then be used to find the path instead:

pin_all_from "app/javascript/spina/controllers", under: "controllers/spina", to: "spina/controllers"

In this case, as well, the value passed to the :to parameter has to be a suffix of the dir parameter.

Feel free to correct me if I missed out on anything. If you want me to raise a PR to document this scenario in the README, then I'll be happy to do it.

I still feel that this could be improved so that the pin_all_from method behaves similar to the pin method:

pin_under "controllers/spina", all: "spina/controllers"

This does 2 things:

  1. The parameters are accepted in a similar way to pin
  2. We don't need the app/javascript prefix

But this obviously includes a change in the public API, so it is up for debate.

Another way can also be to just to modify the pin method to accept an :under argument:

pin "controllers/spina", under: "spina/controllers"

Note that in this case, :under is not accepting "what to pin as" (as seen for the current implementation of pin_all_from), but rather the target path.

I also noticed that this issue is overlapping with #66 so I'm just mentioning that here.

@dhh dhh closed this as completed Feb 19, 2022
ania-hm added a commit to fractalsoft/fractalsoft.org that referenced this issue Sep 4, 2023
https://trello.com/c/O1mjkISk

After successfully deploying a test company application to `fly.io`,
we noticed that our custom js files weren't working.
The problem was how we import custom js files.
We use relative imports the custom js files in the `application.js`
which works in the development but it breaks in production. [1]

To solve this problem we need to pin all files
in custom fractalsoft directory in `importmaps.rb`
and change the import in `application.js`. [2][3]

[1]: https://stackoverflow.com/questions/70548841/how-to-add-custom-js-file-to-new-rails-7-project
[2]: rails/importmap-rails#66
[3]: rails/importmap-rails#67
ania-hm added a commit to fractalsoft/fractalsoft.org that referenced this issue Sep 4, 2023
https://trello.com/c/O1mjkISk

After successfully deploying a test company application to `fly.io`,
we noticed that our custom js files weren't working.
The problem was how we import custom js files.
We use relative imports the custom js files in the `application.js`
which works in the development but it breaks in production. [1]

To solve this problem we need to pin all files
in custom fractalsoft directory in `importmaps.rb`
and change the import in `application.js`. [2][3]

[1]: https://stackoverflow.com/questions/70548841/how-to-add-custom-js-file-to-new-rails-7-project
[2]: rails/importmap-rails#66
[3]: rails/importmap-rails#67
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants