-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Refactor singleton usage #1808
Refactor singleton usage #1808
Conversation
That makes it possible to run webpack/webpack-dev-server scripts not only from the app dir, e.g.: $ path/to/my/app/bin/webpack-dev-server
Thanks, @palkan Is this still a WIP? I would very much like the engines support to be in. |
Hi @gauravtiwari, It's not WIP from the functionality point of view (we're already using this brunch). Btw, added one more commit to make env prefix for dev server configurable ( What's left then? I think, we need a way to somehow automate the process of configuring Webpacker for an engine (all the steps I described above). Something like Or maybe it's better to add Webpacker support to Another thing not covered here: how to deploy the application? We use the following trick: # main app Rakefile
Rake::Task["assets:precompile"].enhance do
Rake::Task["my_engine:webpacker:compile"].invoke
end So, we don't need separate tasks to be invoked during the deployment (works perfectly on Heroku). To sum up, from the code perspective this PR looks complete to me (that means that we're happily use this branch without additional monkey-patches). |
# Returns current Webpacker instance. | ||
# Could be overriden to use multiple Webpacker | ||
# configurations within the same app (e.g. with engines) | ||
def current_webpacker |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just call Webpacker.instance
directly instead of putting into a method. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal is to make it possible to override the Webpacker instance, so in the engine's helper we do:
require "webpacker/helper"
module MyEngine
module ApplicationHelper
include ::Webpacker::Helper
def current_webpacker
MyEngine.webpacker # or whatever
end
end
end
Webpacker.instance
would point to global Webpacker (i.e. ::Webpacker.instance
), so that won't work
Thanks, @palkan This is great 👍 Perhaps, make a changelog entry with experimental note and link to this PR. We can work on another PR to add installation support either via plugin or rake task. Add relevant documentation. |
Could you please rebase with the master branch? |
This branch is based on |
Probably would be best to have this for v4. |
Absolutely. Right now I'm migrating our app to v4) I'll open a new PR as soon as I test this feature for v4 in prod/dev. |
Closed in favour of #1836 |
Context
I'm working on the application consisting of multiple engines. Some of these engines provide user interface.
We wanted to make our engines as isolated as possible (though keeping in the same monorepo); hence we needed our webpack(-er) to be isolated too: separate
package.json
, dev server, compilation process.It turned out that it could be done with Webpacker if we replace the global
Webpacker.instance
usage with the local instance–i.e. if we "fix" dependency injection.NOTE: this PR should considered as an experimental (though the functionality has been extracted from the production app); I'd like to discuss this approach first of all.
NOTE 2: this PR doesn't introduce any backward compatibility changes and only deals with the internals.
Related tickets
#348
What's inside?
Compiler
class refactored to use@webpacker
instance instead ofWebpacker
(7a0a58c)
webpacker
option toDevServerProxy
to make it possible to use with non-default instance (53c8a77)
chdir
for scripts (webpack
,webpack-dev-server
) to make it possible to run not only from the app root (e2bc236)
current_webpacker
toHelper
to make it possible to override the default Webpacker (aa2aabd)
env_prefix
config param todev_server
section ofwebpacker.yml
to make it possible to use different env variable for different webpackers (32fd72e)Usage example
Suppose that we have a Rails engine
MyEngine
.To add an isolated Webpacker configuration to that gem we need:
config/webpacker.yml
andconfig/webpack/*.js
filesbin/webpack
andbin/webpack-dev-server
filesNow you can use
stylesheet_pack_tag
andjavascript_pack_tag
from within your engine.rake my_engine:webpacker:compile
)I think, most of this work could be automated via generators in the future, but that's not the scope of this PR: here I want to slightly change the Webpacker design to make it more flexible.