How can I run custom code before any asset precompile in Rails?
As I can see from the source, Sprockets does not have such a hook, but you could use rake task hooks. For example, you would create a rake task that starts all the preprocessors, gulp, etc, so this task could be put before precompilation.
# lib/tasks/before_assets_precompile.raketask :before_assets_precompile do # run a command which starts your packaging system('gulp production')end# every time you execute 'rake assets:precompile'# run 'before_assets_precompile' first Rake::Task['assets:precompile'].enhance ['before_assets_precompile']
Then you just run rake assets:precompile
, and as a result the task before_assets_precompile
will be executed right before it.
Also make sure to use system
instead of exec
, because exec
will exit the process on a stage of running this pre-task and will not run assets:precompile
after itself as it was expected.
Sources: