Webpacker: rake assets:precompile without running webpacker:compile

Created on 21 Oct 2017  Â·  7Comments  Â·  Source: rails/webpacker

(not sure how to label it, but it'd be "help wanted")

It's kind of all in the title, to expose the context : We have a single rails app, containing the backend — not really using webpacker, standard pipeline and the frontend — new webpacker pipeline. Those end up being deployed as two different apps in production. Not ideal, but we'd like to stick to it.

We would need to compile our assets via :

  • [NOT OK] : rake assets:precompile for the backend, without running webpacker:compile
  • [OK] : webpacker:compile only when deploying the frontend

Looking here, feels like you can't skip running webpacker:compile when running rake assets:precompile, and I'm not sure to be able to ovveride it cleanly. Or maybe there's an option available to skip webpacker:compile ?

Thx a lot if any help

Most helpful comment

@rossta there are actually other good reasons for keeping these steps separate such as caching in the Dockerfile. The steps and dependencies involved in compiling webpack are pretty orthogonal to those required by rails and Sprockets. For projects transitioning that have both, it is pretty inefficient to have to lose the cached webpack output whenever any file changes.

Perhaps there is another way to achieve this, I'm not really sure.

Ah digging through some of the links I see that this is done through an environment variable:

WEBPACKER_PRECOMPILE=false bundle exec rails assets:precompile

Thanks

All 7 comments

The use case you're describing sounds interesting! Also, kind of unique, so I'm not sure personally how useful this feature would be.

One thing you can try is recreating the Sprockets-Rails rake task in the deployed environment where you want to omit Webpacker. You just need to do it conditionally after the bundled rake tasks are loaded in the Rakefile. I've only just played around with this in a sample app locally (YMMV), but here's what it could look like without conditionals:

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require_relative 'config/application'

Rails.application.load_tasks

# Redefine `assets:precompile` and related tasks
require 'sprockets/rails/task'
Sprockets::Rails::Task.new(Rails.application)

Initializing Sprockets::Rails::Task will clobber any pre-existing definition of the asset:precompile task (and its relatives) along with any enhancements like the Webpacker compilation task (source).

thx a lot for this pointer! well without expanding the use case, once you feel the need you get a bit trapped as there is no optional exit

Trying to understand what you've just showed, adding this would reset what you inforced in the gem, let one run a default rake assets:precompile (meaning would not run webpacker:compile along) ?

And then would be able to run rake webpacker:compile additionnaly, as it would still be available ? Or would such setup kick out webpacker:compile ?

We might add an env variable to skip enhancement at runtime. Would that help?

I might not have enough contexts in mind to name it the best solution, but ours being docker/kubernetes, it is surely a perfect match to have this option handled via env vars

@deesx The example I showed would mean you can run rake assets:precompile without triggering the Webpack compilation. It does not affect rake webpacker:compile; you can run that separately.

Duplicate #811

@rossta there are actually other good reasons for keeping these steps separate such as caching in the Dockerfile. The steps and dependencies involved in compiling webpack are pretty orthogonal to those required by rails and Sprockets. For projects transitioning that have both, it is pretty inefficient to have to lose the cached webpack output whenever any file changes.

Perhaps there is another way to achieve this, I'm not really sure.

Ah digging through some of the links I see that this is done through an environment variable:

WEBPACKER_PRECOMPILE=false bundle exec rails assets:precompile

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ijdickinson picture ijdickinson  Â·  3Comments

christianrojas picture christianrojas  Â·  3Comments

suhomozgy-andrey picture suhomozgy-andrey  Â·  3Comments

itay-grudev picture itay-grudev  Â·  3Comments

FrankFang picture FrankFang  Â·  3Comments