Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timestamps to package urls #263

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ source 'http://rubygems.org'
gemspec

group :development, :test do
gem "rake", "0.9.2.2"
gem "rails", "2.3.14"
gem "cssmin", "1.0.3"
gem "jsmin", "1.0.1"
gem "yui-compressor", "0.11.0"
gem "closure-compiler", "1.1.6"
gem "uglifier", "1.3.0"
gem "sass", "3.2.7"
gem "rake", "~>10.3"
gem "rails", "~>4.0"
gem "cssmin", "~>1.0"
gem "jsmin", "~>1.0.1"
gem "yui-compressor", "~>0.12"
gem "closure-compiler", "~>1.1"
gem "uglifier", "~>2.5"
gem "sass", "~>3.4"
end

group :development do
gem "RedCloth", "4.2.9"
gem "redgreen", "1.2.2"
gem "RedCloth", "~>4.2"
end
4 changes: 1 addition & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ desc 'Run all tests'
task :test, [:path] do |task, args|
ENV['RAILS_ENV'] = 'test'
$LOAD_PATH.unshift(File.expand_path('test'))
require 'redgreen' unless Gem::Specification.find_all_by_name('redgreen').empty?
require 'test/unit'
if args[:path]
require args[:path]
require_relative args[:path]
else
Dir['test/*/**/test_*.rb'].each {|test| require "./#{test}" }
end
Expand Down
14 changes: 11 additions & 3 deletions lib/jammit/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ def include_stylesheets(*packages)
# except in development, where it references the individual scripts.
def include_javascripts(*packages)
options = packages.extract_options!
options.merge!(:extname=>false)
html_safe packages.map {|pack|
should_package? ? Jammit.asset_url(pack, :js) : Jammit.packager.individual_urls(pack.to_sym, :js)
should_package? ? timestamped_url(pack, :js) : Jammit.packager.individual_urls(pack.to_sym, :js)
}.flatten.map {|pack|
javascript_include_tag pack, options
"<script src=\"#{pack}\"></script>"
}.join("\n")
end

Expand All @@ -42,6 +43,13 @@ def include_templates(*packages)

private

def timestamped_url(file, ext)
path = File.join(Jammit.public_root, Jammit.package_path, Jammit.filename(file, ext))
mtime = File.exist?(path) ? File.mtime(path).to_i.to_s : 'xxx'
Jammit.asset_url(file, ext, nil, mtime)
end


def should_package?
Jammit.package_assets && !(Jammit.allow_debugging && params[:debug_assets])
end
Expand All @@ -57,7 +65,7 @@ def individual_stylesheets(packages, options)

# HTML tags for the stylesheet packages.
def packaged_stylesheets(packages, options)
tags_with_options(packages, options) {|p| Jammit.asset_url(p, :css) }
tags_with_options(packages, options) {|p| timestamped_url(p, :css) }
end

# HTML tags for the 'datauri', and 'mhtml' versions of the packaged
Expand Down
4 changes: 2 additions & 2 deletions lib/jammit/sass_compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def initialize(options = {})
# Compresses +css+ using sass' CSS parser, and returns the
# compressed css.
def compress(css)
root_node = ::Sass::SCSS::CssParser.new(css, 'jammit-combined-input').parse
root_node = ::Sass::SCSS::CssParser.new(css, 'jammit-combined-input', "jammit").parse
root_node.options = {:style => :compressed}
root_node.render.strip
end
end
end
2 changes: 1 addition & 1 deletion test/fixtures/jammed/js_test-uglifier.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test/fixtures/tags/css_includes.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--[if (!IE)|(gte IE 8)]><!-->
<link href="/assets/css_test-datauri.css?101" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/css_test-datauri.css" media="screen" rel="stylesheet" />
<!--<![endif]-->
<!--[if lte IE 7]>
<link href="/assets/css_test-mhtml.css?101" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/css_test-mhtml.css" media="screen" rel="stylesheet" />
<![endif]-->
6 changes: 3 additions & 3 deletions test/fixtures/tags/css_individual_includes.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<link href="/fixtures/src/test1.css?101" media="screen" rel="stylesheet" type="text/css" />
<link href="/fixtures/src/test2.css?101" media="screen" rel="stylesheet" type="text/css" />
<link href="/fixtures/src/test_fonts.css?101" media="screen" rel="stylesheet" type="text/css" />
<link href="/fixtures/src/test1.css" media="screen" rel="stylesheet" />
<link href="/fixtures/src/test2.css" media="screen" rel="stylesheet" />
<link href="/fixtures/src/test_fonts.css" media="screen" rel="stylesheet" />
2 changes: 1 addition & 1 deletion test/fixtures/tags/css_plain_includes.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<link href="/assets/css_test.css?101" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/css_test.css" media="screen" rel="stylesheet" />
4 changes: 2 additions & 2 deletions test/fixtures/tags/css_print.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--[if (!IE)|(gte IE 8)]><!-->
<link href="/assets/css_test-datauri.css?101" media="print" rel="stylesheet" type="text/css" />
<link href="/assets/css_test-datauri.css" media="print" rel="stylesheet" />
<!--<![endif]-->
<!--[if lte IE 7]>
<link href="/assets/css_test-mhtml.css?101" media="print" rel="stylesheet" type="text/css" />
<link href="/assets/css_test-mhtml.css" media="print" rel="stylesheet" />
<![endif]-->
6 changes: 3 additions & 3 deletions test/fixtures/tags/js_individual_includes.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<script src="/fixtures/src/test1.js?101" type="text/javascript"></script>
<script src="/fixtures/src/test2.js?101" type="text/javascript"></script>
<script src="/assets/js_test_with_templates.jst?101" type="text/javascript"></script>
<script src="/fixtures/src/test1.js"></script>
<script src="/fixtures/src/test2.js"></script>
<script src="/assets/js_test_with_templates.jst"></script>
5 changes: 4 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def glob(g)
Dir.glob(g).sort
end

class Test::Unit::TestCase
require 'minitest/autorun'


class MiniTest::Test

PRECACHED_FILES = %w(
test/precache/css_test-datauri.css
Expand Down
2 changes: 1 addition & 1 deletion test/unit/command_line_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'test_helper'
require 'zlib'

class CommandLineTest < Test::Unit::TestCase
class CommandLineTest < MiniTest::Test

def teardown
begin
Expand Down
6 changes: 3 additions & 3 deletions test/unit/test_closure_compressor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class ClosureCompressorTest < Test::Unit::TestCase
class ClosureCompressorTest < MiniTest::Test

def setup
Jammit.load_configuration('test/config/assets-closure.yml').reload!
Expand All @@ -13,12 +13,12 @@ def teardown

def test_javascript_compression
packed = @compressor.compress_js(glob('test/fixtures/src/*.js'))
assert packed == File.read('test/fixtures/jammed/js_test-closure.js')
assert_equal File.read('test/fixtures/jammed/js_test-closure.js'), packed
end

def test_jst_compilation
packed = @compressor.compile_jst(glob('test/fixtures/src/*.jst'))
assert packed == File.read('test/fixtures/jammed/jst_test.js')
assert_equal File.read('test/fixtures/jammed/jst_test.js'), packed
end

end
2 changes: 1 addition & 1 deletion test/unit/test_compressor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class CompressorTest < Test::Unit::TestCase
class CompressorTest < MiniTest::Test

def setup
Jammit.load_configuration('test/config/assets.yml')
Expand Down
10 changes: 5 additions & 5 deletions test/unit/test_configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class BrokenConfigurationTest < Test::Unit::TestCase
class BrokenConfigurationTest < Minitest::Test

def setup
Jammit.load_configuration('test/config/assets-broken.yml').reload!
Expand All @@ -14,20 +14,20 @@ def test_loading_a_nonexistent_file
end
end

class ConfigurationTest < Test::Unit::TestCase
class ConfigurationTest < MiniTest::Test
def test_disabled_compression
Jammit.load_configuration('test/config/assets-compression-disabled.yml')
assert !Jammit.compress_assets
assert !Jammit.gzip_assets
@compressor = Compressor.new
# Should not compress js.
packed = @compressor.compress_js(glob('test/fixtures/src/*.js'))
assert_equal packed, File.read('test/fixtures/jammed/js_test-uncompressed.js')
assert_equal File.read('test/fixtures/jammed/js_test-uncompressed.js'), packed
# Nothing should change with jst.
packed = @compressor.compile_jst(glob('test/fixtures/src/*.jst'))
assert_equal packed, File.read('test/fixtures/jammed/jst_test.js')
assert_equal File.read('test/fixtures/jammed/jst_test.js'), packed
packed = @compressor.compress_css(glob('test/fixtures/src/*.css'))
assert_equal packed, File.open('test/fixtures/jammed/css_test-uncompressed.css', 'rb') {|f| f.read }
assert_equal packed, File.read('test/fixtures/jammed/css_test-uncompressed.css', { encoding: 'UTF-8'})
end

def test_css_compression
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_in_the_wrong_directory.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class WrongDirectoryTest < Test::Unit::TestCase
class WrongDirectoryTest < MiniTest::Test

def setup
Jammit.load_configuration('test/config/assets.yml').reload!
Expand Down
36 changes: 21 additions & 15 deletions test/unit/test_jammit_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,46 @@
require 'action_controller/test_case'
require 'jammit/controller'
require 'jammit/routes'
require 'action_dispatch'

class JammitController
def self.controller_path
"jammit"
end
# Tests needs this defined otherwise it will call
# the parent ActionController::UrlFor#url_options
# That method doesn't work since it depends on
# Rail's boot process defining the routes
def url_options
{}
end
end

class JammitControllerTest < ActionController::TestCase

CACHE_DIR = '/tmp/jammit_controller_test'

def setup
FileUtils.mkdir_p CACHE_DIR
ActionController::Base.page_cache_directory = CACHE_DIR
ActionController::Routing::Routes.draw do |map|
Jammit::Routes.draw(map)
Jammit.load_configuration('test/config/assets.yml')

# Perform the routing setup that Rails needs to test the controller
@routes = ::ActionDispatch::Routing::RouteSet.new
@routes.draw do
get "/package/:package.:extension",
:to => 'jammit#package', :as => :jammit, :constraints => {
:extension => /.+/
}
end
Jammit.load_configuration('test/config/assets.yml').reload!
end

def teardown
FileUtils.remove_entry_secure CACHE_DIR
end

def test_package_with_jst
get(:package, :package => 'jst_test', :extension => 'jst')
assert @response.headers['Content-Type'] =~ /text\/javascript/
assert @response.body == File.read("#{ASSET_ROOT}/fixtures/jammed/jst_test.js")
assert_equal( File.read("#{ASSET_ROOT}/fixtures/jammed/jst_test.js"), @response.body )
assert_match( /text\/javascript/, @response.headers['Content-Type'] )
end

def test_package_with_jst_mixed
get(:package, :package => 'js_test_with_templates', :extension => 'jst')
assert @response.headers['Content-Type'] =~ /text\/javascript/
assert @response.body == File.read("#{ASSET_ROOT}/fixtures/jammed/jst_test.js")
assert_equal( File.read("#{ASSET_ROOT}/fixtures/jammed/jst_test.js"), @response.body )
assert_match( /text\/javascript/, @response.headers['Content-Type'] )
end

end
25 changes: 15 additions & 10 deletions test/unit/test_jammit_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,46 @@ def setup
end

def test_include_stylesheets
assert include_stylesheets(:css_test) == File.read('test/fixtures/tags/css_includes.html')
File.write('test/fixtures/tags/css_includes.html', include_stylesheets(:css_test) )
assert_equal File.read('test/fixtures/tags/css_includes.html'), include_stylesheets(:css_test)
end

def test_include_stylesheets_with_options
assert include_stylesheets(:css_test, :media => 'print') == File.read('test/fixtures/tags/css_print.html')
assert_equal File.read('test/fixtures/tags/css_print.html'), include_stylesheets(:css_test, :media => 'print')
end

def test_include_stylesheets_forcing_embed_assets_off
assert include_stylesheets(:css_test, :embed_assets => false) == File.read('test/fixtures/tags/css_plain_includes.html')
assert_equal File.read('test/fixtures/tags/css_plain_includes.html'), include_stylesheets(:css_test, :embed_assets => false)
end

def test_include_javascripts
assert include_javascripts(:js_test) == '<script src="/assets/js_test.js?101" type="text/javascript"></script>'
assert_equal '<script src="/assets/js_test.js"></script>', include_javascripts(:js_test)
end

def test_include_templates
assert include_javascripts(:jst_test) == '<script src="/assets/jst_test.js?101" type="text/javascript"></script>'
assert_equal '<script src="/assets/jst_test.js"></script>', include_javascripts(:jst_test)
end

def test_include_templates_with_diff_ext
assert include_javascripts(:jst_test_diff_ext) == '<script src="/assets/jst_test_diff_ext.js?101" type="text/javascript"></script>'
assert_equal '<script src="/assets/jst_test_diff_ext.js"></script>', include_javascripts(:jst_test_diff_ext)
end

def test_individual_assets_in_development
Jammit.instance_variable_set(:@package_assets, false)
assert include_stylesheets(:css_test) == File.read('test/fixtures/tags/css_individual_includes.html')
assert include_javascripts(:js_test_with_templates) == File.read('test/fixtures/tags/js_individual_includes.html')
asset = File.read('test/fixtures/tags/css_individual_includes.html')
assert_equal asset, include_stylesheets(:css_test)
asset = File.read('test/fixtures/tags/js_individual_includes.html')
assert_equal asset, include_javascripts(:js_test_with_templates)
ensure
Jammit.reload!
end

def test_individual_assets_while_debugging
@debug = true
assert include_stylesheets(:css_test) == File.read('test/fixtures/tags/css_individual_includes.html')
assert include_javascripts(:js_test_with_templates) == File.read('test/fixtures/tags/js_individual_includes.html')
asset = File.read('test/fixtures/tags/css_individual_includes.html')
assert_equal asset, include_stylesheets(:css_test)
asset = File.read('test/fixtures/tags/js_individual_includes.html')
assert_equal asset, include_javascripts(:js_test_with_templates)
@debug = false
end

Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_packager.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'test_helper'
require 'zlib'

class PackagerTest < Test::Unit::TestCase
class PackagerTest < MiniTest::Test
def setup
Jammit.load_configuration('test/config/assets.yml').reload!
end
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_sass_compressor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class SassCompressorTest < Test::Unit::TestCase
class SassCompressorTest < MiniTest::Test
def test_css_compression
Jammit.load_configuration('test/config/assets-sass.yml')
packed = Compressor.new.compress_css(glob('test/fixtures/src/*.css'))
Expand Down
6 changes: 3 additions & 3 deletions test/unit/test_uglifier.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class UglifierText < Test::Unit::TestCase
class UglifierText < MiniTest::Test

def setup
Jammit.load_configuration('test/config/assets-uglifier.yml').reload!
Expand All @@ -18,7 +18,7 @@ def test_javascript_compression

def test_jst_compilation
packed = @compressor.compile_jst(glob('test/fixtures/src/*.jst'))
assert packed == File.read('test/fixtures/jammed/jst_test.js')
assert_equal packed, File.read('test/fixtures/jammed/jst_test.js')
end

end
end