Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for a dynamic preamble.

  • Loading branch information...
commit a9294b2bdd820bb40cf4d6f688e4050798d70a01 1 parent 8208fe6
@dblock dblock authored
View
8 README.md
@@ -154,6 +154,14 @@ and `#option!` (to set the value to true):
* `bin_path` – The path to the `node` executable if non-standard and not using `therubyracer`.
* All of the hook methods mentioned below.
+### Custom Preamble
+
+You can generate a custom preamble using a code block. For example, you can replace the location of the original `.coffee` file by a relative one to `Rails.root`.
+
+ Barista.add_preamble do |location|
+ "/* : DO NOT MODIFY - compiled from #{Pathname.new(location).relative_path_from(Rails.root).to_s}\n\n"
+ end
+
## Frameworks
One of the other main features Barista adds (over other tools) is frameworks similar
View
22 lib/barista.rb
@@ -46,6 +46,10 @@ def on_hook(name, *args, &blk)
def invoke_hook(name, *args)
hooks.invoke(name, *args)
end
+
+ def has_hook?(name)
+ hooks.has_hook?(name)
+ end
has_hook_method :on_compilation_error => :compilation_failed,
:on_compilation => :compiled,
@@ -54,14 +58,24 @@ def invoke_hook(name, *args)
:before_full_compilation => :before_full_compilation,
:before_compilation => :before_compilation
-
# Configuration - Tweak how you use Barista.
- has_boolean_options :verbose, :bare, :add_filter, :add_preamble, :exception_on_error, :embedded_interpreter, :auto_compile
+ has_boolean_options :verbose, :bare, :add_filter, :exception_on_error, :embedded_interpreter, :auto_compile, :add_preamble
has_delegate_methods Compiler, :bin_path, :bin_path=, :js_path, :js_path=
has_delegate_methods Framework, :register
has_deprecated_methods :compiler, :compiler=, :compiler_klass, :compiler_klass=
+ def add_preamble(&blk)
+ self.add_preamble = true
+ if block_given?
+ @preamble = blk
+ end
+ end
+
+ def preamble
+ @preamble
+ end
+
def configure
yield self if block_given?
end
@@ -159,10 +173,6 @@ def default_for_add_filter
local_env?
end
- def default_for_add_preamble
- local_env?
- end
-
def default_for_exception_on_error
true
end
View
8 lib/barista/compiler.rb
@@ -114,7 +114,7 @@ def compile!
@compiled_content = preamble(location) + @compiled_content if location != 'inline' && Barista.add_preamble?
@compiled = true
end
-
+
def to_js
compile! unless defined?(@compiled) && @compiled
@compiled_content
@@ -159,7 +159,11 @@ def save(path = @options[:output_path])
def preamble(location)
inner_message = copyable?(location) ? "copied" : "compiled"
- "/* DO NOT MODIFY. This file was #{inner_message} #{Time.now.httpdate} from\n * #{location.strip}\n */\n\n"
+ if Barista.preamble
+ Barista.preamble.call(location)
+ else
+ "/* DO NOT MODIFY. This file was #{inner_message} #{Time.now.httpdate} from\n * #{location.strip}\n */\n\n"
+ end
end
def compilation_error_for(location, message)
View
6 lib/barista/hooks.rb
@@ -16,5 +16,9 @@ def invoke(name, *args)
nil
end
+ def has_hook?(name)
+ @callbacks.has_key?(name)
+ end
+
end
-end
+end
View
2  spec/barista_spec.rb
@@ -12,4 +12,4 @@
context 'compiling all'
-end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.