Getting Sprockets to minify CSS/JS while not in a Rails/Rack app

百般思念 提交于 2019-12-10 11:07:37

问题


I'm trying to use Sprockets to combine and minify my JavaScript and CSS files outside of the Rails and Rack context. So far, I'm able to combine them into a single file, but now I'm trying to run the JS compressor and the CSS compressor on those files.

I followed the Sprockets README's instructions (https://github.com/sstephenson/sprockets#minifying-assets), but I'm getting this error:

NoMethodError: undefined method `compress' for :uglify:Symbol

Here is my complete rakefile:

require 'rubygems'
require 'bundler'
require 'pathname'
require 'logger'
require 'fileutils'
require 'uglifier'

Bundler.require

ROOT        = Pathname(File.dirname(__FILE__))
LOGGER      = Logger.new(STDOUT)
BUNDLES     = %w( combined.css combined.js )
BUILD_DIR   = ROOT.join("dist")
SOURCE_DIR  = ROOT.join("src")

task :compile do
  sprockets = Sprockets::Environment.new(ROOT) do |env|
    env.logger = LOGGER
    env.append_path SOURCE_DIR.join('javascripts').to_s
    env.append_path SOURCE_DIR.join('stylesheets').to_s
    env.js_compressor = :uglify
  end

  BUNDLES.each do |bundle|
    assets = sprockets.find_asset(bundle)
    prefix, basename = assets.pathname.to_s.split('/')[-2..-1]
    FileUtils.mkpath BUILD_DIR.join(prefix)

    assets.write_to(BUILD_DIR.join(prefix, basename))
  end
end

I've tried the following (spoiler alert: they don't work):

  1. env.js_compressor = :uglifier
  2. env.js_compressor = Uglifier
  3. env.js_compressor = Uglify

And I get still get a NoMethodError for compress for each one.

What is the correct way to enable JS compression? What about CSS? (I am experiencing the similar issues there.)

====== In addition to the answer marked below, please note this:

For anyone who is curious, you'd have instantiate your compressor and make sure it responds to compress:

env.js_compressor = YUI::JavaScriptCompressor.new  
env.css_compressor = YUI::CssCompressor.new

回答1:


I tried it out (example below), and it worked for me. I would suspect that you have an outdated version of Sprockets, since the shorthand minifier notation you want to use was introduced quite recently in version 2.7.0. I would do a gem upgrade and hopefully that fixes it.


Example Rakefile (in a directory containing an unminified non-min.js file, after running rake compile a minified min.js file was generated):

require 'sprockets'

ROOT = Pathname(File.dirname(__FILE__))

task :compile do
  sprockets = Sprockets::Environment.new(ROOT) do |env|
    env.append_path './'
    env.js_compressor = :uglify
  end

  assets = sprockets.find_asset("non-min.js")
  assets.write_to("./min.js")
end

Update: To verify my answer above, I installed an older version of Sprockets (v2.6.0) and got the same error message as you did.



来源:https://stackoverflow.com/questions/27576034/getting-sprockets-to-minify-css-js-while-not-in-a-rails-rack-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!