-
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
When not using dev server, compile is called for every single asset tag and dev and test page rendering is very slow #2814
Comments
Yes, you are correct. This issue would affect anyone not using the dev server for development as well.
The Webpacker manifest and compiler do not have knowledge of the request cycle but the Helper is a typical Rails view helper so it would— and could leverage request-specific state (but does not currently). The webpack executable is not getting called multiple times since after the first call to webpacker/lib/webpacker/compiler.rb Lines 21 to 23 in bb16e5a
webpacker/lib/webpacker/compiler.rb Lines 37 to 44 in bb16e5a
So my guess would be the slowness comes from the webpacker/lib/webpacker/compiler.rb Lines 49 to 61 in bb16e5a
A rudimentary improvement could be something like an instance variable flag set in |
Hmm, I guess that means it has to read in every single file in every watched directory every time a helper tag is used. I have to assume that many many apps would have a lot of stuff in there and thus would have this problem. At my next convenience I will see if this is what is slow. For anyone that has this problem, here is what i have done to make my tests at least not be slow:
For dev…I am setting Other info of relevance:
|
OK, coming back to this since Slack is down lol. I put in some But I guess I'm still stuck on the notion that this must be a problem for everyone all the time. Our app is pretty small, so any reasonably-complex app must have this problem in spades, and should've had this problem for a very long time since the Webpacker code has been relative stable. Perhaps 100ms per call is not normal and there's something about my setup? |
We recently stumbled on the same problem. With ~600 files to watch and a dozen of calls per request, we had 6-7s spent in the Webpacker stale check. Here is the solution I've ended up with: # config/initializers/webpacker.rb
# Only add file watcher if lazy compilation is enabled
if Webpacker.config.compile?
module Webpacker
module FileWatcherStaleCheck
STALE_CHECK_MUTEX = Mutex.new
def initialize(*)
super
@need_stale_check = true
end
def compile
STALE_CHECK_MUTEX.synchronize do
@watcher ||= build_watcher do
@need_stale_check = true
super.tap do
@need_stale_check = false
end
end
if @need_stale_check
@watcher.execute
else
@watcher.execute_if_updated
end
end
end
private
def build_watcher(&block)
reloadable_paths = Dir[*default_watched_paths, *watched_paths]
# Only watch for the app's file (excluding vendors, etc.)
reloadable_paths.select! { |path| path.match? %r{app/javascript} }
# We do not watch added/removed files (directories), 'cause we have hundreds of
# them and that's pretty slow
Rails.application.config.file_watcher.new(reloadable_paths, [], &block)
end
end
Compiler.prepend(FileWatcherStaleCheck)
end
end Now it's unnoticeable. Note that I'm not watching directories, thus, adding new files won't work. When I tried to add directories as well, it was much slower. |
This is not working for me. I thought it was that the regexp needs to be One thing about my setup that may be different is that we have spring/listen disabled and am using the |
Yeah, that could be the reason. But it still should call Webpack at least once, since we run |
thanks for the ideas @palkan. since we are using the dev server in development and thus only doing lazy compilation for specs in the test env (and don't care at all about source file changes during spec runs) just shortcutting the compiler after the first (successful) run does the trick: module Webpacker
module OneTimeCompiler
def compile
@compiled ||= super
end
end
Compiler.prepend(OneTimeCompiler) if Webpacker.config.compile?
end i'd love to see an option for this in the webpacker config - and default to it in the generated template's test env. probably lots of seconds could be saved for lots of developers. 🌎 🕐 |
problem
When not using
webpack-dev-server
, it seems that Webpacker calls compile for every single asset tag in dev and test, making dev and test very slow.Based on commit history, this seems intentional or at least this behavior has existed for a while. Is there a way to disable it in test at least?
background
We have a run-of-the-mill ERB-based app. We are on Webpacker 5.2.1, Rails 6.0.2.
Our header prefetches several fonts and our pages load many images. Webpacker and Webpack 100% work to compile things and make the site work as intended. Production is not affected.
The problem is when a page is loaded in dev or test, each time a webpacker-provided helper is called, compile is called:
Like so:
First,
lookup!
is called on the manifest:webpacker/lib/webpacker/helper.rb
Lines 20 to 24 in b6c2180
lookup!
callslookup
, which callscompile if compiling?
webpacker/lib/webpacker/manifest.rb
Lines 41 to 50 in b6c2180
compiling?
is true if we are set to compile (true dev and test) and the dev server is not running:webpacker/lib/webpacker/manifest.rb
Lines 53 to 55 in b6c2180
The result is that our image-heavy pages show
[Webpacker] Everything's up-to-date. Nothing to do
many times and a page that renders in < 1s in production takes 10s locally.What I need help with
compile: false
inwebpacker.yml
it takes 1.5 seconds.I think a solution might be to only call compile once per request, so if someone knows the codebase well, I can provide a patch.
See Also
The text was updated successfully, but these errors were encountered: