ElMassimo / js_from_routes

🛣️ Generate path helpers and API methods from your Rails routes
https://js-from-routes.netlify.app/
MIT License
100 stars 7 forks source link

config.output_folder doesn't support nested folders #35

Closed gurgeous closed 1 year ago

gurgeous commented 1 year ago

Hey, great gem and thank you so much for creating! I wanted to move app/frontend/api to app/frontend/vendor/api so I can easily exclude it from linting, etc. I tried this:

JsFromRoutes.config do
  _1.output_folder = Rails.root.join("app/frontend/vendor/api")
end

It sorta worked, but the generated imports don't include vendor:

// JsFromRoutes CacheKey a376287889ecd8b72c981c0366ab5059
//
// DO NOT MODIFY: This file was automatically generated by JsFromRoutes.
import * as helpers from '~/api/all'
export * from '~/api/all'
export default helpers

I can work around this with a monkeypatch, of course:

module JsFromRoutes
  class ControllerRoutes
    def import_filename
      dir = Pathname.new("vendor/api")
      # @config.output_folder.basename.join((basename.split(".")[0]).to_s)
      dir.join((basename.split(".")[0]).to_s)
    end
  end
end

Probably better fix though :) Thanks again!

gurgeous commented 1 year ago

Also, I had to add a custom template for index.js, which uses a hardcoded ~/api:

  _1.template_index_path = Rails.root.join("config/initializers/js_from_routes_index.js.erb")
//
// DO NOT MODIFY: This file was automatically generated by JsFromRoutes.
import * as helpers from '~/vendor/api/all'
export * from '~/vendor/api/all'
export default helpers
ElMassimo commented 1 year ago

Hi Adam! Thanks for reporting 😃

Released a fix in js_from_routes 2.1.0.

You should be able to remove your custom index template now.