LoginSignup
6
7

More than 5 years have passed since last update.

Railsデフォルトのjqueryは圧縮されてない

Last updated at Posted at 2012-07-21

注記(7/21追記)

  • production環境ではassets precompileによって圧縮されるため、下記の設定は不要です。
  • assets pipelineを理解する上での参考程度にしてもらえればと思います。

app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require_tree .
[1] pry(main)> puts Rails.application.config.assets.paths
/Users/naoty/workspace/sample/app/assets/images
/Users/naoty/workspace/sample/app/assets/javascripts
/Users/naoty/workspace/sample/app/assets/stylesheets
/Users/naoty/workspace/sample/vendor/assets/javascripts
/Users/naoty/workspace/sample/vendor/assets/stylesheets
/Users/naoty/workspace/sample/vendor/bundle/ruby/1.9.1/gems/jquery-rails-1.0.14/vendor/assets/javascripts
  • rails newで生成されるapplication.jsでロードするjqueryは、上記のコマンドで確認できる通り、jquery-railsというgemに同梱されているjquery.js。ちなみに現時点では最新のv1.7.2。
  • なので、圧縮されてないものがデフォルトではロードされてしまう。これは、けっこう気づきにくい罠だと思う。
$ cd `bundle show jquery-rails`
$ tree vendor/
vendor/
└── assets
    └── javascripts
        ├── jquery-ui.js
        ├── jquery-ui.min.js
        ├── jquery.js
        ├── jquery.min.js
        └── jquery_ujs.js
  • jquery-railsの中を見てみると、jquery.min.jsがある。
  • 圧縮版あるんだから、こっち使う。
  • jquery-ui.minもあるから、jquery-ui使う場合も同じ。まあ、jquery-uiは必要なコンポーネントだけに絞るべきなので、jquery-rails内のjquery-uiは使うことないと思う。
app/assets/javascripts/application.js
//= require jquery.min
//= require jquery_ujs
//= require_tree .
  • これで圧縮版のjqueryをロードする。
6
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7