I have a rails '4.2.11.1' application. I want to dissable assets compilation, minifying, pipeline in development. in order to use indium debugger
The rails app seems to be set up with sprokets. so in my config/application.rb I changed to this:
# Enable the asset pipeline
config.assets.enabled = false
also in config/environmetns/development.rb I have this setup:
# Do not compress assets
config.assets.compress = false
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
config.serve_static_files = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
but nothing happens and I get the files:
assets/stixs.self-7246a54effa0c5edaa806b9c4fbc6ba4617c0d80bef8c236f9a4cbcfcf7f0be7.js
finally I get this files not minifyed, or compresed but how can I get that rails work with serving the files in app/assets/javascripts in de delopment environment
Technique Sprockets uses for fingerprinting is to insert a hash of the content into the name
Add this in your development.rb
config.assets.digest = false
I hope that work for you.
Related
So I updated Ckeditor to 4.2. After I did this, Ckeditor no longer appears in production. Instead, I get the following error in the console:
Uncaught TypeError: CKEDITOR.style.customHandlers[e.type] is not a constructor
I've consulted a few sources, including this post, which claimed the issue was in precompilation. I've edited my application.rb to include Ckeditor in precompilation, but it still doesn't work.
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module DeployTest
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
config.assets.precompile += Ckeditor.assets
config.assets.precompile += %w( ckeditor/* )
config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
config.active_record.default_timezone = :local
config.time_zone = 'Eastern Time (US & Canada)'
end
end
Weirdly enough, when I turn config.assets.debug = true, Ckeditor starts working again, but my CSS turns off.
Why does config.assets.debug work? Why does it turn off my CSS? And how can I get a permanent solution for ckeditor?
Using ckeditor gem from git solved this issue for me
gemfile:
gem 'ckeditor', github: 'galetahub/ckeditor'
https://github.com/galetahub/ckeditor/issues/719
I am using Middleman with the activate :asset_hash option in order to compile assets with the hash to force cache invalidation. The problem is that I also load some html templated with Angular templateUrl like this:
function($routeProvider) {
$routeProvider.
when('/dashboard', {
templateUrl: 'templates/dashboard.html',
....
but the dashboard file gets compiled with an hash like templates/dashboard-cc1554f0.html and so Angular doesn't find the file.
Is there a way to pass within the Angular file the same hash that Middleman generates on build ?
I must tell you that I didn't meet this problem (All my js, css and images are hashed, but html files are not). Here is my Gemfile
Gemfile
# If you do not have OpenSSL installed, update
# the following line to use "http://" instead
source 'https://ruby.taobao.org'
gem "middleman", "~>4.0.0"
# Live-reloading plugin
# gem "middleman-livereload", "~> 3.2.0"
# For faster file watcher updates on Windows:
gem "wdm", "~> 0.1.0", :platforms => [:mswin, :mingw]
# Windows does not come with time zone data
gem "tzinfo-data", platforms: [:mswin, :mingw, :jruby]
gem "middleman-minify-html"
gem "middleman-sprockets", "~> 4.0.0.rc"
And here is my config.rb
config.rb
set :css_dir, 'css'
set :js_dir, 'app'
set :images_dir, 'images'
set :layout, false
# Build-specific configuration
configure :development do
set :backend, 'http://localhost:3000'
set :frontend, 'http://localhost:4567/#'
end
configure :build do
set :backend, 'https://api.example.com'
set :frontend, 'https://www.example.com/#'
# For example, change the Compass output style for deployment
activate :minify_css
# Minify Javascript on build
activate :minify_javascript
# Minify HTML files on build, requires gem `middleman-minify-html`
activate :minify_html
# Enable cache buster
activate :asset_hash
# gzip text files
activate :gzip
end
You can see that I use Middleman 4.0.
If your problem remains after upgrading middleman, maybe you can rename your js file to xxx.js.erb and try the helper url_for.
For this particular rails project, I want to save on an http request, so I'd like to output all the javascript onto the body that has been in the rails asset pipeline.
Is there a way to do this in rails?
You unfortunately won't be able to get this working easily in development since the files are being served live by Sprocket's server, but getting this working in production is fairly simple: you just need to loop over the compiled scripts and render them in a blob.
Here's a helper that you can use instead of javascript_include_tag which will do just that:
module ApplicationHelper
if Rails.env.production?
def embedded_javascript_include_tag(*sources)
options = sources.extract_options!.stringify_keys
scripts = sources.uniq.map { |source|
File.read("#{Rails.root}/public/#{javascript_path(source)}")
}.join("\n").html_safe
content_tag(:script, scripts, options)
end
else
def embedded_javascript_include_tag(*sources)
javascript_include_tag(sources)
end
end
end
Note you'll still need to run rake assets:precompile for this to work. Don't forget to set the environment to enable any uglifiers and minifers! (RAILS_ENV=production rake assets:precompile)
In order to debug javascript in my heroku production environment, I need to disable asset compression (or at least compression of javascript). I tried config.assets.compress = false along with config.assets.debug = true, and the compressed assets were still used. I then deleted the compressed assets, at which point no assets were rendered at all. I added config.assets.enabled = false, which did not help. I tried copying the uncompressed assets into various directories, including the application root, public, and public/assets (the latter two using both the folders "images, "javascripts", and "stylesheets", and putting the assets directly into the folders without the three subfolders). I was eventually able to get the javascripts to work by changing the html to directly reference all of the javascript files. But the CSS and images still are not working.
I would have thought that my original config.assets.compress = false should have worked. Any ideas what I did wrong?
I came up with this workaround after reading the docs:
create a module that does nothing to compress js / css here: lib/modules/no_compression.rb
class NoCompression
def compress(string)
# do nothing
string
end
end
configure your assets to (not) be compressed with your do-nothing compressor
config.assets.compress = true
config.assets.js_compressor = NoCompression.new
config.assets.css_compressor = NoCompression.new
Under Rails 4 just commenting out the line
# config.assets.js_compressor = :uglifier
in config/environments/production.rb worked for me. Looks like default is no compresson.
I also need to debug my js so I tried ncherro's solution. The problem was that it would still throw
rake aborted!
uninitialized constant NoCompression
So I just put the NoCompression class in the production.rb file
# Compress JavaScripts and CSS
class NoCompression
def compress(string)
# do nothing
string
end
end
config.assets.compress = true
config.assets.js_compressor = NoCompression.new
config.assets.css_compressor = NoCompression.new
Comment out the uglifier and add config.assets.debug = true. This worked for me.
Compress JavaScripts and CSS:
config.assets.js_compressor = :uglifier
Debug mode disables concatenation and preprocessing of assets. But this option may cause significant delays in view rendering with a large number of complex assets:
config.assets.debug = true
Also worth noting... In addition to ncherro solution you will need to do the following:
make sure to put your new module somewhere where it will be loaded by default. Was lib/extras in my case.
run rake assets:clean to clean your existing assets.
run rake assets:precompile to compile your assets using the new compressor.
restart your app... i use touch tmp/restart.txt
Happy debugging ;)
With Rails 4 on Heroku you need to do two things. First as #geekQ mentioned, comment out the js_compressor line in config/environments/production.rb
# config.assets.js_compressor = :uglifier
Second, you need to consider Heroku's asset pipeline cache for Rails 4. Any file with the same MD5 as the version in the cache will not be recompiled. The previous (possibly compressed) version will be served. Any file you edit will have a new MD5 and be recompiled.
You can also purge the entire asset cache with the Heroku Repo plugin to the Heroku toolbelt. Install that, then use the command
heroku repo:purge_cache
Deploy a new version after purging the cache and all your assets will be recompiled.
I had to update Rails.application.config.assets.version in config/initializers/assets.rb for the production.rb changes to take effect.
Find and comment out these line in environments/production.rb:
config.assets.js_compressor = ...
config.assets.css_compressor = ...
Looks like this MAY have been a bug in Rails. From the changelog for upcoming rails 3.2.9, is this what you were running into?
Respect config.digest = false for asset_path
Previously, the asset_path internals only respected the :digest option, but ignored the global config setting. This meant that config.digest = false could not be used in conjunction with config.compile = false this corrects the behavior.
http://weblog.rubyonrails.org/2012/10/29/ann-rails-3-2-9-rc1-has-been-released/
Do you think that could be related?
I've migrated a rails 3.0 app to 3.1 on Heroku. It's running on the cedar stack and everything is fine except that the app's javascript won't run. The application.js file is compiled and looks just as it should. It's accessible by going to myapp.com/assets/application.js. It just doesn't get run!
If I run the app locally, the javascript works, so I suspect that there must be some simple configuration issue that I'm missing. Here's my production.rb file:
FloridaBirdTrail::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = true
# Compress JavaScripts and CSS
config.assets.compress = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
end
edit
Replacing the contents of production.rb with that of development.rb has allowed at least some of my javascript to run (gmap3 isn't working, for some reason). But which of the settings is making the difference?
Open your application.rb file and make sure your Bundler require statement looks like the following:
Bundler.require *Rails.groups(:assets)
By default it looks like
# If you precompile assets before deploying to production, use this line
Bundler.require *Rails.groups(:assets => %w(development test))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
Manually precompiling worked for me.
bundle exec rake assets:precompile before you push to heroku.
Were you sure to switch Heroku to the Cedar stack? Here are some docs for upgrading
http://devcenter.heroku.com/articles/rails31_heroku_cedar#upgrading_from_previous_rails_31_releases
http://devcenter.heroku.com/articles/cedar
I had some Javascript problems (server-side on Heroku, locally everything was fine) that went away when I
* moved all my Javascript into a separate file instead of application.js
* removed require_tree from application.js and instead called up every javascript I wanted by name
* removed bootstrap.js from my app/assets/javascript folder
My guess is that compilation somehow screws things up.
I hope you did a local pre-compilation of assets before your latest Heroku push (as advised in one of the responses above).
Please check if your system is blocking the execution of JavaScripts. For this, open up the console while you are on your Heroku app, and check for exceptions. In case you see exceptions related to JavaScripts being blocked, that could be the issue. In my case, the same happened, and unfortunately, I was not able to do anything about it, as I didn't have admin privileges.