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: af213da70643
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2c96846fd425
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Nov 23, 2014

  1. Fix failing preload spec

    adambeynon committed Nov 23, 2014
    Copy the full SHA
    2d713f2 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
    2c96846 View commit details
Showing with 74 additions and 88 deletions.
  1. +17 −88 lib/opal/builder.rb
  2. +25 −0 lib/opal/builder/cache_store.rb
  3. +32 −0 lib/opal/builder/cached_asset.rb
105 changes: 17 additions & 88 deletions lib/opal/builder.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,18 @@
require 'opal/path_reader'
require 'opal/builder_processors'
require 'opal/builder/cached_asset'
require 'opal/builder/cache_store'
require 'set'

module Opal
class Builder
class CachedAsset
def initialize(data)
@data = data
end

def requires
@data[:requires]
end

def to_s
@data[:contents]
end

def source_map
""
end
end

class CacheStore
attr_reader :environment

def initialize(environment)
@environment = environment
end

def store(key, contents, requires)
environment.cache_set("opal/#{key}.cache", {
:contents => contents, :requires => requires})
end

def retrieve(key)
if obj = environment.cache_get("opal/#{key}.cache")
return CachedAsset.new(obj)
else
nil
end
end
end

include BuilderProcessors

attr_accessor :cache_store

# A set of paths which have been processed already.
attr_reader :processed

def initialize(options = nil)
(options || {}).each_pair do |k,v|
public_send("#{k}=", v)
@@ -60,7 +26,7 @@ def initialize(options = nil)
@prerequired ||= []
@path_reader ||= PathReader.new

@processed = []
@processed = Set.new

@assets = []
end
@@ -71,26 +37,24 @@ def self.build(*args, &block)

def build(path, options = {})
source = read path
process_string source, path, options
build_str source, path, options
self
end

def build_str(source, filename, options = {})
process_string(source, filename, options)
end

def build_require(path, options = {})
process_require(path, options)
end

def process_string(source, filename, options)
fname = path_reader.expand(filename).to_s
asset = processor_for(source, filename, fname, requirable: false)

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

process_requires asset, fname, options
@assets << asset
end

def build_require(path, options = {})
process_require(path, options)
end

def process_require(filename, options)
return if prerequired.include?(filename)
return if processed.include? filename
@@ -102,6 +66,9 @@ def process_require(filename, options)
process_requires asset, path, options

@assets << asset

# TODO: cache asset (should check for cache_store first)
# cache_store.store filename, asset.to_s, asset.requires
end

def process_requires(asset, path, options)
@@ -186,45 +153,7 @@ def read(path)
raise ArgumentError, "can't find file: #{path.inspect} in #{path_reader.paths.inspect}"
end

def process_require_OLD(filename, options)
return if prerequired.include?(filename)
return if already_processed.include?(filename)
already_processed << filename

source = stub?(filename) ? '' : read(filename)

if source.nil?
message = "can't find file: #{filename.inspect}"
case @compiler_options[:dynamic_require_severity]
when :error then raise LoadError, message
when :warning then warn "can't find file: #{filename.inspect}"
end
end

if cached = cache_store.retrieve(filename)
puts "CACHED: #{filename}"
process_requires cached.requires, options
processed << cached

return
end

puts ">>>> COMPILE #{filename}"
path = path_reader.expand(filename).to_s unless stub?(filename)
asset = processor_for(source, filename, path, options.merge(requirable: true))
process_requires(asset.requires+tree_requires(asset, path), options)
processed << asset

cache_store.store filename, asset.to_s, asset.requires
end

# A set of paths which have already been processed (so we do not need
# to process them again).
def processed
@processed ||= Set.new
end

def stub? filename
def stub?(filename)
stubs.include?(filename)
end

25 changes: 25 additions & 0 deletions lib/opal/builder/cache_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Opal
class Builder
# Sprockets compatible CacheStore
class CacheStore
attr_reader :environment

def initialize(environment)
@environment = environment
end

def store(key, contents, requires)
environment.cache_set("opal/#{key}.cache", {
:contents => contents, :requires => requires})
end

def retrieve(key)
if obj = environment.cache_get("opal/#{key}.cache")
return CachedAsset.new(obj)
else
nil
end
end
end
end
end
32 changes: 32 additions & 0 deletions lib/opal/builder/cached_asset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +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
def initialize(data)
@data = data
end

def requires
@data[:requires]
end

def to_s
@data[:contents]
end

def source_map
""
end
end
end
end