-
Notifications
You must be signed in to change notification settings - Fork 149
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
How to make it works with webpacker? #237
Comments
Generate separate file from js-routes and use it in webpack. For any routes change you need regenerate this file. |
Is there any way to automate this? |
@codEmotion rake task? |
Thanks. This is the recipe:
|
I think |
I think we should add a doc on that to readme. |
I created my own webpack loader for the routes, so it would rebuild when I changed I would instead recommend writing your own generator instead of a rake task because of the meaning of what this is doing. # lib/generators/routes_generator.rb
# run it with: rails generate routes
class RoutesGenerator < Rails::Generators::Base
def create_initializer_file
create_file "app/javascript/routes.js", JsRoutes.generate
end
end And use your own path/options. This way you don't need to remember options to pass to |
@gtarsia but you still have to use |
@hadees this is to use webpacker instead of sprockets. I used Also, remember that if you replace sprockets with webpacker, you should install and |
Another option to use js-routes with webpacker is this: Add
|
@ProGM your solution works, a couple of points to note:
|
@ProGM
if I think this is due to generated code not having Let me fork and test if adding Update 1: My solution
|
@PikachuEXE code have |
If you are looking for a solution that plays nicely with Webpacker, you might want to check out Although it doesn't come with a default implementation for url interpolation, it's very flexible and you can generate code that uses your own API helpers. ComparisonFor a comparison with this library, check this pull request, which illustrates how it can be customized to provide a nicer API. |
Sounds like this might not be necessary anymore with #257 fixed (released in v1.4.10). |
I'm also using Webpacker in getting a.. |
@stoplion there is not file that is generated that is called "js-routes.js". The default path is still Line 15 in b52648a
You can change that: +++ b/config/initializers/js_routes.rb
@@ -0,0 +1,3 @@
+JsRoutes.setup do |config|
+ config.file = "app/javascript/js-routes.js"
+end but even then, you need |
I made a few patches to make it more compatible with webpacker:
I would be grateful if someone can try to use the gem from master and report of these features are working correctly. |
I have not had any issues using
using 1.4.9, so I do not know why 7c654aa is necessary. But then again, I am far from being an expert on that topic. |
Just leave my notes how I migrated from Sprockets to Webpacker, perhaps somebody helps
$ mv app/assets app/deprecated_assets
$ rails js:routes
$ yarn add @babel/plugin-transform-modules-umd
plugins: [
require('@babel/plugin-transform-modules-umd'), // to import `JsRoutes.generate!` file
]
}
}
import Routes from 'routes'
// backward compatibility
window.Routes = document.Routes = Routes
|
@itsNikolay can you explain why you would need |
@bogdan I know I'm not @itsNikolay, but I was having the same problem. I upgraded my package from 1.4.9 to 1.4.13 and it broke my app. I was using the asset pipeline, but decided to make the jump and start using Webpacker for js-routes since we already have react in our project, and are using Webpacker already. I followed the set up in the readme that explains using app/javascript/routes.js.erb and I was getting this error in the browser:
Once I added @babel/plugin-transform-modules-umd the error went away. A UMD bundle, though making your shipped JS larger, allows it to be imported across browsers, nodejs, commonjs among others. Anyway, maybe a consideration would be to ship jsroutes with an option to create a UMD bundle instead, which would eliminate the need for the babel plugin. Thanks for all your hard work, it really helps those of us that know rails, but need to code in JS! |
@sec0ndhand I did what I could in v1.4.14. It is hard to make it work for all environments- js is such a mess. Can you try that and see if it works without |
https://build.opensuse.org/request/show/865199 by user coolo + dimstar_suse updated to version 1.4.14 see installed CHANGELOG.md ## v1.4.14 * Fix compatibility with UMD modules #237 [Comment](railsware/js-routes#237 (comment)) ## v1.4.13 * Improve compatibility with node environment #269. * Change default file location configuration to Webpacker if both Webpacker and Sprockets are loaded ## v1.4.11 * Use app/javascript/routes.js as a default file location if app/javascript directory exists * Add `default` export for better experience when used as es6 module ## v1.4.10 * Require engine only when sprockets is loaded #257.
Sorry it took so long @bogdan. I have tried this, and this did not work once I removed |
I've released v2.0 with complete support of ESM modules and webpacker. Webpacker setup is now in the readme. Please upgrade and open another issue if there are still problems. |
@le0pard it would be helpful if you could try upgrade guide https://github.com/railsware/js-routes/blob/master/VERSION_2_UPGRADE.md for any of your projects. |
@bogdan thanks. Will try later and will provide feedback (in PR or just as comment) |
@bogdan I did test on small project - all works. Currently I did not found project, which using js-routes in our company - all migrated to hardcoded values in JS :) |
Rails 5.2 introduce Webpacker gem which can help you to avoid using Sprockets.
Is there any way to make JS-Routes usable with Webpacker?
The text was updated successfully, but these errors were encountered: