不在Rails/Rack应用程序中时,获取链轮以最小化CSS/JS [英] Getting Sprockets to minify CSS/JS while not in a Rails/Rack app
问题描述
我正在尝试使用Sprockets在Rails和Rack上下文之外组合并最小化我的JavaScript和CSS文件.到目前为止,我已经可以将它们组合成一个文件,但是现在我试图在这些文件上运行JS压缩器和CSS压缩器.
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.
我遵循了Sprockets自述文件的说明( https://github.com/sstephenson/sprockets#minifying -assets ),但出现此错误:
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
这是我完整的rakefile:
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):
-
env.js_compressor = :uglifier
-
env.js_compressor = Uglifier
-
env.js_compressor = Uglify
env.js_compressor = :uglifier
env.js_compressor = Uglifier
env.js_compressor = Uglify
我仍然得到了每个compress
的NoMethodError
.
启用JS压缩的正确方法是什么?那CSS呢? (我在这里遇到了类似的问题.)
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:
对于任何好奇的人,您都应该实例化您的压缩器,并确保其响应compress
:
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
推荐答案
我尝试了一下(下面的示例),它对我有用.我会怀疑您的Sprockets版本已经过时,因为您要使用的速记符符号是在2.7.0版中引入的.我会做一个gem upgrade
并希望能解决它.
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.
Rakefile示例(在运行rake compile
之后,在包含未压缩的non-min.js
文件的目录中生成了一个压缩的min.js
文件):
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
更新:要验证我的回答,我安装了旧版本的Sprockets(v2.6.0),并收到了与您相同的错误消息.
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.
这篇关于不在Rails/Rack应用程序中时,获取链轮以最小化CSS/JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!