最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Selective Rails 3.2 asset pipeline compression per file - Stack Overflow

programmeradmin4浏览0评论

I'd like to selectively press some of the javascript files in a Rails 3.2 app, but still have all js assets served from a single bundled file in the production environment.

Syntax like this, inside of the app/assets/javascripts/application.js file, using the made-up :press => false option passed to the last 3 sprockets require directives I hope explains what I'm trying to achieve.

// Contents of app/assets/javascripts/application.js
//
//= require jquery
//= require jquery_ujs
//= require angular-1.0.1/angular, :press => false
//= require angular-1.0.1/angular-resource, :press => false
//= require products, :press => false

So jquery.js and jquery_ujs.js files will be pressed (by Rails asset pilation, which uses UglifierJS by default), and the remaining 3 files will not be pressed, but they will be bundled into the application.js bundle.

Is there any way available to do this?

The motivation is that the products.js file contains an angularjs controller that makes use of angular's dependency injection which requires specific variable names such as $scope and $http are not altered.

I'd like to selectively press some of the javascript files in a Rails 3.2 app, but still have all js assets served from a single bundled file in the production environment.

Syntax like this, inside of the app/assets/javascripts/application.js file, using the made-up :press => false option passed to the last 3 sprockets require directives I hope explains what I'm trying to achieve.

// Contents of app/assets/javascripts/application.js
//
//= require jquery
//= require jquery_ujs
//= require angular-1.0.1/angular, :press => false
//= require angular-1.0.1/angular-resource, :press => false
//= require products, :press => false

So jquery.js and jquery_ujs.js files will be pressed (by Rails asset pilation, which uses UglifierJS by default), and the remaining 3 files will not be pressed, but they will be bundled into the application.js bundle.

Is there any way available to do this?

The motivation is that the products.js file contains an angularjs controller that makes use of angular's dependency injection which requires specific variable names such as $scope and $http are not altered.

Share Improve this question asked Aug 12, 2012 at 13:54 Eliot SykesEliot Sykes 10.1k6 gold badges55 silver badges64 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

I'm using this line in my config/environment/production.rb file

config.assets.js_pressor = Sprockets::LazyCompressor.new { Uglifier.new(:mangle => false) }

It press my controllers but it doesn't change method signatures so DI still works as expected.

To get this to work with Rails 4 since it uses a newer version of sprockets I used:

config.assets.js_pressor = Uglifier.new(mangle: false) if defined? Uglifier

in environments/production.rb

Note that for this specific reason there's an option to specify the injected services with strings rather than variable names. See the line under the controller.

var MyController = function(renamed$scope, renamedGreeter) {
  ...
}
MyController.$inject = ['$scope', 'greeter'];

Example taken from http://docs.angularjs/guide/di

发布评论

评论列表(0)

  1. 暂无评论