Coder Social home page Coder Social logo

js_assets's Introduction

js_assets

Gem provides a helper asset_path in javascript.

Installation

Add gem to your Gemfile

gem 'js_assets'

And run bundle install.

The current version supports only Sprockets 3.x. For Sprockets 2.x use version 0.0.10

gem 'js_assets', '<= 0.0.10'

Usage

In your application.js

//= require app_assets

This directive adds the method asset_path in the global scope.

Get the path to the template app/assets/javascripts/rubrics/views/index.html in javascript:

var path = asset_path('rubrics/views/index.html')
// the function will return for development:
// /assets/rubrics/views/index.html
// and for production
// /assets/rubrics/views/index-5eb3bb250d5300736006c8944e436e3f.html

You can look at the raw hash of assets by inspecting JsAssets::List.fetch on the Rails console or the global JavaScript variable, project_assets, in your browser's console.

Updating after changes

js_assets would be pretty slow if it had to calculate the assets on every request! For performance reasons, js_assets therefore keeps a cache of assets and the cache will survive app restarts, even if you restart Spring during development. If you change js_assets' configuration or add/remove any asset files, you should delete tmp/cache under Rails root.

To automatically update app_assets.js when adding new files in app/assets, you can use the guard tool - it monitors the file system and performs actions when certain events occur. This can be useful during development.

To setup Guard, Add to Gemfile:

group :development do
  gem 'guard'
  gem 'guard-shell'
end

Add to Guardfile:

guard :shell do
    watch(%r{^app/assets/.*}) { `rm -rf tmp/cache` }
end

Run the command bundle exec to install the gems. Before starting to develop run guard:

$ bundle exec guard

Warning! This may adversely affect the rate of return assets list in the development environment. Since they will be compiled at each change.

Example: Reference Slim-generated HTML assets from JavaScript

For example we want to use templating Slim in AngularJS app. Let our templates will be in app/assets/webapp/. We make the following settings:

In general, the standard config to generate HTML from Slim would look something like this:

# config/application.rb
config.assets.paths << Rails.root.join('app', 'assets', 'webapp')

# config/initizlizers/assets_engine.rb
Rails.application.assets.register_engine('.slim', Slim::Template)

# config/environments/production.rb
config.assets.precompile += ['*.html']

By default, js_assets will make all "*.html" assets available, so we don't have to do any special js_assets config. To reference these templates in JavaScript, just include app_assets in application.js.

//= require app_assets

And now we get a path for these Slim-generated assets in our JavaScript! e.g. to reference the template app/assets/webapp/blogs/edit.html.slim:

var path = asset_path('blogs/edit.html')
// the function will return for development:
// /assets/blogs/edit.html
// and for production
// /assets/blogs/edit-5eb3bb250d5300736006c8944e436e3f.html

The asset_path function will be available to any JavaScript, including those you've generated from other syntaxes such as CoffeeScript.

Settings

Using JSAssets::List, you can specify, for example in the initializer, which assets will be available via the asset_path helper, and which should be excluded. By default, "*.html" is included.

You can configure this in application.rb or create an initialiser file like config/intializers/js_assets.rb and configure it there.

To add a file pattern to the list, use:

JsAssets::List.allow << '*.png'

To exclude:

JsAssets::List.exclude << '*.png'

Initially, the list is taken asset falling within the filter app/config/environments/production.rb

config.assets.precompile += ['*.html']

The default settings are:

JsAssets::List.exclude = ["application.js"]
JsAssets::List.allow = ["*.html"]

And remember to delete tmp/cache.

Be careful! If the list of available JsAssets::List.allow get a file that is inserted directive require app_assets, recursion will occur as sprockets will calculate the md5-based content. Generally, if you are using files like "application.js" with a list of "require" directives, you should exclude them using the .exclude setting above.

To determine if filename will be used with md5 hashes, js_assets will use the Rails config:

# Generate digests for assets URLs.
config.assets.digest = true

You can use the directive Rails asset_host by setting ActionController::Base.asset_host in the application configuration, typically in config/environments/production.rb:

config.action_controller.asset_host = "assets.example.com"

License

Copyright © 2013 Zaur Abasmirzoev <[email protected]>

JsAssets is distributed under an MIT-style license. See LICENSE for details.

js_assets's People

Contributors

guillaume-winddle avatar izaurio avatar kavkaz avatar mahemoff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

js_assets's Issues

Incompatible with Sprockets 3.x

We are using this gem inside one of our rails apps and I just updated to the latest release of the sass-rails gem, which in turn bumped the sprockets version in our Gemfile.lock to 3.0.3, and then when I went to reboot the server I got this message:

/usr/local/lib/ruby/gems/2.1.0/gems/js_assets-0.0.9/lib/js_assets/list.rb:12:in `block in fetch': undefined method `logical_path_for_filename' for #<Sprockets::Environment:0x007f84100e06f0> (NoMethodError)
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/paths.rb:73:in `block (2 levels) in each_file'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/path_utils.rb:223:in `block in stat_tree'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/path_utils.rb:207:in `block in stat_directory'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/path_utils.rb:204:in `each'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/path_utils.rb:204:in `stat_directory'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/path_utils.rb:222:in `stat_tree'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/paths.rb:71:in `each'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/paths.rb:71:in `block in each_file'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/paths.rb:70:in `each'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/paths.rb:70:in `each_file'
    from /usr/local/lib/ruby/gems/2.1.0/gems/js_assets-0.0.9/lib/js_assets/list.rb:11:in `fetch'
    from /usr/local/lib/ruby/gems/2.1.0/gems/js_assets-0.0.9/app/assets/javascripts/app_assets.js.erb:1:in `_evaluate_template'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/erb_processor.rb:26:in `call'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/erb_processor.rb:13:in `call'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:75:in `call_processor'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:56:in `reverse_each'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:56:in `call_processors'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:87:in `load_asset_by_uri'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:45:in `block in load'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:157:in `fetch_asset_from_dependency_cache'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:38:in `load'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/cached_environment.rb:20:in `block in initialize'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/cached_environment.rb:47:in `yield'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/cached_environment.rb:47:in `load'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/bundle.rb:23:in `block in call'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/utils.rb:183:in `dfs'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/bundle.rb:24:in `call'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:75:in `call_processor'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:56:in `reverse_each'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/processor_utils.rb:56:in `call_processors'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:87:in `load_asset_by_uri'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:45:in `block in load'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:157:in `fetch_asset_from_dependency_cache'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/loader.rb:38:in `load'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/cached_environment.rb:20:in `block in initialize'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/cached_environment.rb:47:in `yield'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/cached_environment.rb:47:in `load'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/base.rb:63:in `find_asset'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/environment.rb:30:in `find_asset'
    from /usr/local/lib/ruby/gems/2.1.0/gems/sprockets-3.0.3/lib/sprockets/base.rb:89:in `[]'
    from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/rails/railtie.rb:68:in `block (3 levels) in <class:Railtie>'
    from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/rails/railtie.rb:67:in `map'
    from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/rails/railtie.rb:67:in `block (2 levels) in <class:Railtie>'
    from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/renderer.rb:73:in `call'
    from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/renderer.rb:73:in `setup_combined_js'
    from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/renderer.rb:78:in `reset_combined_js!'
    from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/renderer.rb:20:in `setup!'
    from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/rails/railtie.rb:74:in `block (2 levels) in <class:Railtie>'
    from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/rails/railtie.rb:78:in `call'
    from /home/vagrant/.bundler/ruby/2.1.0/react-rails-99a1b38b28db/lib/react/rails/railtie.rb:78:in `block in <class:Railtie>'
    from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.9/lib/active_support/lazy_load_hooks.rb:36:in `call'
    from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.9/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
    from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.9/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
    from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.9/lib/active_support/lazy_load_hooks.rb:44:in `each'
    from /usr/local/lib/ruby/gems/2.1.0/gems/activesupport-4.1.9/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
    from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/application/finisher.rb:64:in `block in <module:Finisher>'
    from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/initializable.rb:30:in `instance_exec'
    from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/initializable.rb:30:in `run'
    from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /usr/local/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
    from /usr/local/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /usr/local/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
    from /usr/local/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /usr/local/lib/ruby/2.1.0/tsort.rb:345:in `each'
    from /usr/local/lib/ruby/2.1.0/tsort.rb:345:in `call'
    from /usr/local/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
    from /usr/local/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
    from /usr/local/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
    from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/initializable.rb:54:in `run_initializers'
    from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/application.rb:300:in `initialize!'
    from /vagrant/config/environment.rb:5:in `<top (required)>'
    from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/application.rb:276:in `require'
    from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/application.rb:276:in `require_environment!'
    from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:147:in `require_application_and_environment!'
    from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /usr/local/lib/ruby/gems/2.1.0/gems/railties-4.1.9/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:8:in `require'
    from bin/rails:8:in `<main>'

Correct me if I'm wrong, but I believe this gem is the one invoking sprockets. At any rate, limiting the max sprockets version to 2.x on our Gemfile stopped these errors from being raised.

The issue appears to be that the method in question, logical_path_for_filename, existed on Sprockets 2.x but was removed at some point prior to the release of Sprockets 3.

I just did some digging... here is the commit moving it of the Sprockets::Environment class.

Here's a commit making it private..

Here's a commit where the method is renamed to normalize_logical_path

At any rate I can't keep tracking it but it looks like for Sprockets 3 the appropriate method to call to find the path for something is Sprockets::Environment#resolve from the Sprockets::Resolve module.

For now I would release a v0.10 of this gem with a gem dependency limiter for sprockets 2.x and then look into making a new release to fix the feature. Sorry I can't be of more help but I am not very familiar with the Sprockets gem / the internals of the asset pipeline.

config.assets.digest=false ignored? (Rails 5)

After Rails upgrade, I notice the assets always have digests, even if config.assets.digest = false is set in development.rb.

The decision is here:

if env.file_digest(filename)

I'm not sure why, but I think that's true even if digest is off. So I guess the condition could be changed to check the config directly.

Custom environments

I have a staging environments. But in code env-s only production and development. In my environment i dont have any paths

Not finding my asset

I am using a custom RAILS_ENV=staging for my QA environment, but I think this issue still exists if I set RAILS_ENV=production. I looked through your code for specific references to environment names and found that they had been removed, so I'm guessing this should work. (Maybe it's something addressed by the open pull request. Not sure.)

I have a little red "X" png that is part of my image assets, and it needs to be included in some javascript code that adds nested attributes as part of a workflow for file attachments. The file x_16x16.png is in the asset pipeline.

$ RAILS_ENV=staging rails c
Loading staging environment (Rails 4.2.7.1)
2.3.0 :001 > ActionController::Base.helpers.asset_path('x_16x16.png')
 => "/assets/x_16x16-7d1b5578d7288518ff6184d04063cf116faf9ab5a52d7f19898af57280de5303.png"

However, I include this snippet:

content += "<td><a href='javascript: void(0);' onClick='remove_attachment(this," + i + ");'><img src='" + asset_path('x_16x16.png') + "'></a></td></tr>";

and it comes out like:

<a href="javascript: void(0);" onclick="remove_attachment(this,0);"><img src="/assets/x_16x16.png"></a>

Can you help? I have found no other way to include asset paths in javascript cleanly.

NoMethodError: undefined method `logical_paths' for nil:NilClass

I get this error during precompilation:

NoMethodError: undefined method `logical_paths' for nil:NilClass
/usr/local/rvm/gems/ruby-2.2.2/gems/js_assets-0.1.0/lib/js_assets/list.rb:10:in `fetch'
/usr/local/rvm/gems/ruby-2.2.2/gems/js_assets-0.1.0/app/assets/javascripts/app_assets.js.erb:1:in `_evaluate_template'

This is the relevant line:

::Rails.application.assets.logical_paths do |logical_path, filename|

It's legal for Rails.application.assets to be nil in production:

https://github.com/rails/sprockets-rails

config.assets.compile

Enables Sprockets compile environment. If disabled, Rails.application.assets will be nil to prevent inadvertent compilation calls. View helpers will depend on assets being precompiled to public/assets in order to link to them. Initializers expecting Rails.application.assets during boot should be accessing the environment in a config.assets.configure block. See below.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.