Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d47b0c0f2878
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5bdcd52c090b
Choose a head ref
  • 3 commits
  • 5 files changed
  • 1 contributor

Commits on Nov 25, 2014

  1. Copy the full SHA
    a62810e View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    eb9775d View commit details
  3. Disable cache in demo

    adambeynon committed Nov 25, 2014
    Copy the full SHA
    5bdcd52 View commit details
Showing with 42 additions and 73 deletions.
  1. +1 −1 examples/rack/config.ru
  2. +24 −15 lib/opal/builder.rb
  3. +14 −31 lib/opal/builder/{cached_asset.rb → asset.rb}
  4. +0 −23 lib/opal/builder/processors.rb
  5. +3 −3 lib/opal/sprockets/cache_store.rb
2 changes: 1 addition & 1 deletion examples/rack/config.ru
Original file line number Diff line number Diff line change
@@ -7,5 +7,5 @@ run Opal::Server.new { |s|

# use a cache, for example purposes
# s.sprockets.cache = Opal::Sprockets::MemoryStore.new
s.sprockets.cache = ::Sprockets::Cache::FileStore.new('tmp/cache')
# s.sprockets.cache = ::Sprockets::Cache::FileStore.new('tmp/cache')
}
39 changes: 24 additions & 15 deletions lib/opal/builder.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'opal/builder/path_reader'
require 'opal/builder/processors'
require 'opal/builder/cached_asset'
require 'opal/builder/asset'
require 'set'

module Opal
@@ -76,7 +76,8 @@ def build(logical_path, options = {})

def build_str(source, logical_path, options = {})
filename = path_reader.expand(logical_path).to_s
asset = processor_for(source, logical_path, filename, requirable: false)

asset = build_asset(source, logical_path, filename, requirable: false)

preload.each { |path| process_require path, options }

@@ -98,7 +99,6 @@ def process_require(logical_path, options)
asset = find_asset logical_path

process_requires asset, filename, options

@assets << asset
end

@@ -122,13 +122,29 @@ def find_asset(logical_path)

filename = path_reader.expand(logical_path).to_s

asset = processor_for(source, logical_path, filename, requirable: true)
stat = stat(logical_path)
# TODO: fixme - processors should do this
asset.mtime = stat(logical_path).mtime.to_i if stat
build_asset(source, logical_path, filename, requirable: true)
end
end

def build_asset(source, logical_path, filename, options)
extname = File.extname(filename)
processor = processors.fetch(extname) { default_processor }

asset
result = processor.new(source, logical_path, options)

data = {
:source => result.source,
:requires => result.requires,
:required_trees => result.required_trees,
:source_map => result.source_map.as_json,
:logical_path => logical_path,
}

if stat = stat(logical_path)
data[:mtime] = stat.mtime.to_i
end

Asset.new(data)
end

def cached_asset(logical_path)
@@ -182,13 +198,6 @@ def tree_requires(asset, filename)
end
end

def processor_for(source, logical_path, filename, options)
extname = File.extname(filename)
processor = processors.fetch(extname) { default_processor }

processor.new(source, logical_path, compiler_options.merge(options))
end

def read(logical_path)
path_reader.read(logical_path) or
raise ArgumentError, "can't find file: #{logical_path.inspect} in #{path_reader.paths.inspect}"
45 changes: 14 additions & 31 deletions lib/opal/builder/cached_asset.rb → lib/opal/builder/asset.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,32 @@
module Opal
class Builder
# An asset that is loaded from the cache. It exposes the same
# interface as a processor, but is prefilled with compile data
# and requires.
#
# Opal::Builder::CachedAsset.new(
# :contents => "...", :requires => [])
#
# See Also
#
# Opal::Builder::CacheStore
#
class CachedAsset
class Asset
def initialize(data)
@data = data
@data = data
@source = data[:source]
@requires = data[:requires]
@required_trees = data[:required_trees]
@mtime = data[:mtime]
@source_map = ::SourceMap::Map.from_hash(data[:source_map])
end

# When re-encoding a cached asset, we just pass back the initial data.
# A cached asset doesn't usually need to be re-encoded back to the
# cache, as it is already in the cache.
#
def encode
puts "warning: trying to re-encode a cached asset"
@data.dup
end

def to_s
@data['source']
source
end

def requires
@data['requires']
end
attr_reader :source

def required_trees
@data['required_trees']
end
attr_reader :requires

def mtime
@data['mtime']
end
attr_reader :required_trees

def source_map
@source_map ||= ::SourceMap::Map.from_hash(@data['source_map'])
end
attr_reader :mtime

attr_reader :source_map

# Check that this cached asset is fresh. A fresh asset is one
# that has not changed on disk. An asset that is not fresh will
23 changes: 0 additions & 23 deletions lib/opal/builder/processors.rb
Original file line number Diff line number Diff line change
@@ -38,12 +38,6 @@ def initialize(source, logical_path, options = {})
@required_trees = []
end

# FIXME: remove this.
#
# This is currenly used for encoding the asset. This should be removed
# and handled by the cache store instead.
attr_accessor :mtime

# General compiler options.
#
# @returns [Hash]
@@ -71,23 +65,6 @@ def initialize(source, logical_path, options = {})
# @returns [Array] array of strings
attr_reader :required_trees

# Encodes this asset ready for caching. This method simply returns a
# hash to be used for caching; it includes the compiled source, an
# array of requires, the mtime at time of compilation and a generated
# source map.
#
# @returns [Hash]
#
def encode
{
'source' => source,
'requires' => requires,
'required_trees' => required_trees,
'mtime' => mtime,
'source_map' => source_map.as_json
}
end

def to_s
source.to_s
end
6 changes: 3 additions & 3 deletions lib/opal/sprockets/cache_store.rb
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ def initialize(environment)
# Store an asset in the cache.
#
# @param path [String] the key/pathname of asset
# @param asset [Opal::CachedAsset] the asset to cache
# @param asset [Opal::Asset] the asset to cache
def []=(path, asset)
key = cache_key_for_path(path)
environment.cache_set(key, asset.encode)
@@ -27,11 +27,11 @@ def []=(path, asset)
# Retrieve an asset from sprockets cache. Might be nil if
# asset cannot be found in cache.
#
# @return [Opal::CachedAsset]
# @return [Opal::Asset]
def [](path)
key = cache_key_for_path(path)
if hash = environment.cache_get(key)
::Opal::Builder::CachedAsset.new(hash)
::Opal::Builder::Asset.new(hash)
else
nil
end