lib/webpack_rails/processor.rb in webpack_rails-0.3.0 vs lib/webpack_rails/processor.rb in webpack_rails-1.0.0
- old
+ new
@@ -1,24 +1,43 @@
require 'tilt'
-require_relative './sprockets_index_webpack'
-require_relative './task'
-
module WebpackRails
class Processor < Tilt::Template
+ def self.configure(webpack_task_config)
+ Class.new(Processor) do
+ self.config = webpack_task_config
+ end
+ end
+
+ def self.config=(new_config)
+ @config = new_config
+ end
+
+ def self.config
+ @config
+ end
+
def prepare
end
+ def rewrite_asset_paths(contents, context)
+ contents.gsub(/['"]\$asset_path\/([^'"]+?)['"]/) {|s| "'#{context.asset_path($1)}'" }
+ end
+
def evaluate(context, locals)
return data unless context.pathname.to_s.include?('.bundle')
- # wait til webpack is done before loading
- result = WebpackRails::Task.run_webpack
+ file_contents = nil
+ if self.class.config[:watch]
+ result = WebpackRails::Task.run_webpack(self.class.config)
- result[:modules].map{|m| context.depend_on m}
+ result[:modules].map{|m| context.depend_on m}
- bundle_contents = context.pathname.open.read
- # rewrite $asset_paths in strings
- bundle_contents.gsub(/['"]\$asset_path\/([^'"]+?)['"]/) {|s| "'#{context.asset_path($1)}'" }
+ file_contents = context.pathname.open.read # reload file contents after build
+ else
+ file_contents = data
+ end
+
+ rewrite_asset_paths(file_contents, context)
end
end
end