How to use this gem from within rails engines?
I'm not sure if this is the right approach and maybe I've not understood this new part of the pipeline, yet, but:
Suppose, the major part of the app code lives inside a rails engine including all javascripts. And there are several main apps using that engine. The main apps only contain some layout changes and some minor patches.
Therefore, in order to integrate the webpacker gem into this setup, I'm trying to do the heavy lifting inside the engine, i.e. keep the work that has to be done inside all the main apps as little as possible.
How would I do that? Any pointers or suggestions are appreciated.
I'm not sure if this is a duplicate of https://github.com/rails/webpacker/issues/21. But in any case, I'd like to conclude this issue with a step-by-step guide how to approach this, for others facing the same use case.
What to do in the engine
- Include
webpackerin the*.gemspecfile. require 'webpacker'in thelib/foo/engine.rb.- ...
What to do in each main app
bundle install- Include
./bin/webpack-dev-serverin theProcfileif using Foreman. - ...
@fiedl Out-of-the-box webpacker doesn't support engines yet. There are quite a few moving pieces that needs to be considered for this setup to work properly and usually people will have different use cases. I started doing some work on this but it's going to take time.
Feel free to leave any ideas/suggestions 👍
@gauravtiwari Yes, after diving into this, I see your point. One would have to decide for each component where it belongs---to the engine, the main app, or both---and tie the pieces together in the right manner.
Some thoughts on this:
- The
bin/webpack-dev-serveris needed in the main app. One could add it to theProcfileof the main app to start the dev server with Foreman. Is there already a way to have the webpack dev server started by Pow? - But one would need the ability to add a package from the engine.
- The
app/javascriptfiles have to be loaded from both, the engine and the main app. The main app files need to have the final say. I guess if there are several engines, the files from all engines need to be loaded. - There are some files like
.babelrc,.postcssrc.ymlthat would seem right in the main app root folder; but I'm not sure what these files are doing, yet. - The
config/webpackfiles appear to be tricky. In my use case, I would like to minimise the work to be done in the main app. But for other use cases, I imagine, the main app would need more control over the configuration. I really like how initializers work. I have most of them in the engine, but those of the main app have the last say in case of a conflict. Maybe we could utilise this mechanism: Would it make sense to tell webpacker in an initializer where to look for the webpack config? If no initializer is present, just useconfig/webpackfrom the rails root folder. - For
package.jsonandyarn.lock, one would need a mechanism similar to what is used with the*.gemspec,GemfileandGemfile.lock. If the engine decides that a certain package version is required, the package will be updated together with the engine when runningbundle update my_enginefrom the main app. But the last say regarding the package versions has the main app as they are locked in the lock file. After all, theyarn.locklooks like aGemfile.lock. - This leads to the question how the upgrade process would look like from the view of the main app. I guess, just running
bundle installfrom the main app would not cover any javascript package updates from the engine.
@gauravtiwari I started adding webpacker support to ManageIQ, where the UI is just an engine (so, app root != UI root), and we may need to support reading assets from multiple engines, and we need to output them to the rails root folder, not engine root.
To achieve that, I needed to override most of the webpacker methods to use the engine root instead of hardcoding Rails.root, I also needed to add a rake task that outputs Rails.root and call that from webpack config (didn't find another way of providing that path to webpack). (This also means that we need to include the initializer that overrides those paths from our Rakefile, which is a bit unfortunate :).)
.. And I still haven't figured out what would be needed to be able to call webpacker:compile, so far it it works if you manually call Webpacker.bootstrap but only when called from the root app, not when called from the engine.
I still haven't finished work on the "read assets from multiple engines" bit, but so far, I have a rake task that outputs a json of all the engines and their root paths (and filters those engines by existence of /app/javascript, until there's a proper way to register this), and intending to read that in the webpack config.
So.. I think at least these changes would be needed:
- don't assume all paths are relative to the current directory
- don't hardcode
Rails.rootanywhere - come up with a sane way of passing those engine paths all the way to JS
If you're interested in the changes that we needed (and feel free to criticize and/or make suggestions), https://github.com/ManageIQ/manageiq-ui-classic/pull/1132 .
Furthermore (and these are only my personal opinion):
- we should default to expecting the assets compiled, UI devels can enable
webpack-dev-server, and for the rest, it should Just Work as long as they don't make UI changes. - we should never deal with yarn, yarn and webpack are separate tools, and webpacker's job starts when you have all the assets, not before
- all those safety checks (
compiledepends onverify_install,verify_installdepends oncheck_nodeandcheck_yarn, ..) are out of scope and needlessly complicate these tasks. All we need is to see the real error when it fails.
EDIT: Oh, and if there was a nice way of disabling the assets:precompile hook, it would be lovely, when wrapping that task, you may want the other taks to run on precompile :).
EDIT2: Aand making all the compile-time yarn deps as --dev, so that non-dev is reserved for actual UI dependencies, but I guess that's a separate issue :)
Right now i'm doing this, but obviously the solution is quite incomplete. Here is what I'm doing, what I see as the potential solution
by default wepbacker will always look for the manifest in the /public/packs folder of the Root application, that's because the default webpacker.yml is configured as such. However when coming from an engine environment it should respect the setting of the engine's webpacker.yml as well. which means webpacker needs to be aware of multiple namespaces.
For webpacker to be successful in an engine environment we need things to be isolated like every other part.
Which means, when we install the engine into an app, it needs to have an 'install step' that basically installs the pre-built modules into the rails app /public/packs/engine/name/manifest.json
We will probably need to modify the stylesheet_pack_tag and javascript_pack_tag helper to be aware of namespaces. So basically if you want to use the engine's assets you should be able to do something like javascript_pack_tag 'engine/name/vendor' etc... or javascript_pack_tag 'vendor', module: 'Engine::Name' which resolves to /public/packs/engine/name/vendor.js
I will have more clarity on this as I develop the solution. right now for me developing the engine with JS code I am just using a symlink from the dummy app's public/packs folder to the engine's public/packs folder, which works for development purposes.
A question for @dhh is also that should the Root app using the engine have to worry about compiling an engine's assets? If not it should make thing simpler since we would expect the JS code from the engine to already be compiled and usable which would mean we just need to make the javascript_pack_tag and stylesheet_pack_tag aware of those assets based on namespaces. or the module: 'Engine::Name' option that gets passed in.
I've made a POC using webpack and rails engines that I believe that can be reused to solve this request: see https://github.com/ohadlevy/foreman/commit/2afa796552c3331a1f22df7143ef6f9def733b67 for a reference.
I would love to see some progress on this feature - this is preventing me from using webpacker in my newest project.
Please Do Investigate 👍
On Jul 19, 2017, at 23:03, Jevon Wright [email protected] wrote:
I would love to see some progress on this feature - this is preventing me from using webpacker in my newest project.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.
we have been using webpack successfully (without webpacker) with rails engines on theforeman/foreman project. they key was to create a simple ruby script that loads all engines paths, and pass that output as json to webpack, which in turn simply add those path to its lookup paths
Sounds like something we should be able to automate 👍
On Thu, Jul 20, 2017 at 7:01 AM, Ohad Levy [email protected] wrote:
we have been using webpack successfully (without webpacker) with rails engines on theforeman/foreman https://github.com/theforeman/foreman project. they key was to create a simple ruby script https://github.com/theforeman/foreman/blob/develop/script/plugin_webpack_directories.rb that loads all engines paths, and pass that output as json to webpack, which in turn simply add those path to its lookup paths https://github.com/theforeman/foreman/blob/develop/config/webpack.config.js#L30
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rails/webpacker/issues/348#issuecomment-316682127, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAKtcL8JHDO8sv-eFyRzPOOtBxPNcmoks5sP0GAgaJpZM4NUgTi .
Should the Root app using the engine have to worry about compiling an engine's assets?
On the surface, it's a fair argument that an asset engine (which is essentially what a webpack app gem is) could be expected to have all the assets pre-compiled, because... once it's released the assets aren't going to change... but looking at how assets in engines currently work, that is not the case, probably to allow including uncompiled assets within other uncompiled assets in the Root app (e.g. application.scss could use mixins from an engine's scss files) if that's the intent of the engine. I'm not totally sure whether that would ever be the case with a webpack engine I guess... is it feasible that there might be a use case for an engine that provides pieces to be used within a larger webpack app? Perhaps the engine would expose some Elm module or React component, and the Root app's UI would utilize that component from its own Elm/React app?
In Elm at least, that actually doesn't make sense as a use case, because all modules have to be explicitly included in the elm-package.json, and will then be added to the dependency graph independently of any Rails Engine. But I'm not sure the same can be said of every potential webpacked app - for instance, what if your Engine's app is just straight ES6 that exposes some modules? Could the Root app import those modules from the engine to be used as potential internal components of it's reactive UI?
If that kind of integration is possible (and useful, which seems likely to me if it's possible), then in order to facilitate it, a Root app obviously needs to be able to compile the engine's assets (after they are included and used however they are going to be used).
My main argument against allowing this type of integration, through a Rails Engine specifically, would be that it's mixing what are becoming ever more the separate responsibilities of the server-side app and the client-side app. That is, if your reactive UI requires some component, it should import it using a javascript dependency manager like npm, whereas the Gemfile should manage dependencies for your server-side Rails app (which may include some compiled reactive UI app to be used on the front-end, like any other asset to be sent to the client)
My question is: is that a good enough argument to make it impossible using webpacker (by assuming there will be no precompiled integration between the UI components of an engine and its root app, and hence not providing any considerations to facilitate such integration)?
I was originally patiently waiting for this kind of support so I could upgrade a gem I manage to be used with webpacker, but @mltsy's argument resonated pretty well with me.
Javascript provided by an engine to be used through webpacker could just be put in (an) npm package(s). This would also allow the javascript to become part of the npm ecosystem, becoming a dependency or depending on other packages.
The engine could still provide CSS and JS when needed through sprockets. One downside is the potential overlap of an engine providing a page that depends on javascript that is also provided in its npm package. It could lead to a few use-cases of an end user having to download the same javascript code from multiple locations
@mltsy @tomprats I partially agree with this view.
However, let's say the engine is a pluggable CMS that defines its own JS dependencies (similarly to gems in .gemspec). I would hope there would be a way for the main Rails app to pickup these dependencies as well.
As of now, in my case, most of the JS dependencies defined in a Rails engine are facilitated via https://rails-assets.org. It would be fantastic to be able to define them via webpacker.
Yeah - this is a fairly unique situation where we have essentially two dependency managers in a single application (bundler and npm), and specifically the situation you're describing, where we have a Rails app with a bundler dependency on a Rails Engine, and an npm dependency on the "webpack app" (for lack of a better term, assuming it's not published as an npm package) that is defined in the Rails Engine. And that's the sticking point - the Rails engine "depends on" the webpack app that is implemented in its javascript/packs directory, but that dependency isn't really part of bundler or npm. So it's hard to say how/if it should be exposed in the contexts of npm and bundler. If we expose that somehow, we would essentially be adding javascript dependencies via the ruby dependency manager, which confuses things a bit when it comes to the javascript dependency manager... (there are some things included via an external channel, which creates somewhat of a mess when it comes to dependency management).
The best solution I can think of to handle that would be to somehow create/expose a kind of meta-package (or legitimate package?) out of the webpack app(s) contained within the Rails Engine, and automatically add that package to the packages.json of the Rails app before compiling with webpack (or maybe when the Engine is installed?). But this is all very theoretical, so maybe I should just shut my mouth and let someone who has attempted something like this share their wisdom ;)
@mltsy @tomasc @tomprats @ohadlevy @soundasleep @fiedl:
The beta version of React on Rails is built on top of Webpacker and here are the docs for using it.
I just released v9 beta.1, built on top of:
- https://github.com/rails/webpacker/pull/601
- https://github.com/shakacode/react_on_rails/pull/908
How about specifying more than one in source_path of webpacker.yml?
default: &default
source_path:
- app/javascript
- engine/javascript
source_entry_path: packs
public_output_path: packs
cache_path: tmp/cache/webpacker
Specify the entry point of the application and the entry point of the engine, and build it with the application.
If it is dynamic, it will be glad even if it can be done dynamically with ERB etc.
Thanks @chimame, looks like a good idea, I will give it a try and report back.
@chimame I have successfully done something similar to your suggestion. Since all the engines in my app are in vendor/gems, my source_path definition in webpacker.yml is:
source_path: '**/app/javascript'
And this pulls in the modules from all the engines as well as the main app.
This is working very well for me, except I had to hack a single line in the rails webpacker npm module, as the manifest.json keys were not correct. I would happily create a PR for this, but I think there may be other use cases I'm not thinking of. Also, although my hack doesn't break the tests, I don't think this line of code is covered by tests and it's not clear to me why it was written that way.
I have a small example app with this implemented at https://github.com/lazylester/webpack_example
Hmm... You're right! That's cool! At least partly... I mean that example app isn't quite the proof of concept that is necessary, because it doesn't actually require a module defined in the engine, but I bet it could!
I was slightly misunderstanding the role of npm and webpack. npm defines and downloads the sources (dependencies) from which webpack can choose what to include in any given pack. An engine is just another source for webpack to pull from, so it supplements the sources supplied by npm. Then webpack can choose from either npm's dependencies or the additional sources provided by the engine(s)...
Now... that is still a little bit disconcerting, just because those sources are outside of npm's dependency graph, meaning... the other thing we still need to solve is how to install the dependencies of the engine's pack(s). I see your "inventory" engine has no package.json (no JS dependencies) - that's not too likely in the real world, I imagine. Would you want to try adding and using one dependency and see if that works? I can't imagine running npm install on the main app would also install the engine's dependencies. But maybe there's a way to get around that... ? (I'm no expert in npm, so someone else here might have a better idea than I do about how to solve that issue)
@mitsy you're right, I don't currently require any modules in the engine. I have done it elsewhere but forgot to include it in the example app. I'll update the example and post here. However my engines do not have any of their own npm dependencies, just local modules, within the engine, and a global (ractivejs) module that is in the main app's node modules.
So, yes, my approach has limitations. and doesn't cover many use cases. But it gets me to production!
@justin808 - now that I understand what I'm looking for more I looked through the docs for using react_on_rails to see how you're handling this issue too. I see you're using a rails task to run a command provided by the engine (configuration.build_production_command) to compile the engine's pack, but (and this may be my ignorance about webpack) I see two issues in using this as a general solution:
-
It looks like that just runs webpack... when do the dependencies get installed? Does webpack install them? Does the user need to install them before running the task? (I see a
yarn build:productionversion of the command in the test suite, but it looks like that script is not actually implemented or used) -
If that command runs webpack with a totally separate webpack.yml, that wouldn't allow the parent application to include any of the engine's modules, etc. right? It's just compiling a totally independent pack? I see there's a comment just above the command saying if you don't want the engine to build the pack for you, that command can be set to nil, but I don't see any documentation on how to compile your pack in that case. Have you designed a workflow for compiling a webpack in the parent app that uses components/modules from your engine?