-
Notifications
You must be signed in to change notification settings - Fork 3
Google Closure includes a templating language called Soy that can generate both .js and .java output. Googlyscript supports the version that creates .js with the Googly::Soy middleware. Note that this has nothing to do with .erb and .haml templates, which are Ruby.
The arguments that Googly::Soy accepts are the same as SoyToJsSrcCompiler.jar uses. Run java -jar SoyToJsSrcCompiler.jar to see the supported arguments.
Googly::Soy adds support for directory globbing to the file list. It also watches the modification times of your sources and won't compile unless changes are detected. Note that it must compile once at server start because it is difficult to determine the output files.
config.ru
use Googly::Soy, %w{
--shouldProvideRequireSoyNamespaces
--cssHandlingScheme goog
--shouldGenerateJsdoc
--outputPathFormat {INPUT_DIRECTORY}{INPUT_FILE_NAME_NO_EXT}.js
src/script/**/*.soy
src/script/**/*.sy
}
Googly.script '/dev/goog', :goog
Googly.script '/dev/myapp', 'src/script'
# Script server must be run after Soy
use Googly::Middleware
Errors are persisted on the rack environment. You do not need to be in the request that fired the compilation it order to read the Soy logs. Just look in env[Googly::Soy::ENV_ERRORS] to see how your compilations went. It will be an array if you have multiple Soy middlewares installed.
This compilation template will report Soy errors to the javascript console and abort compilation if any are found.
compile.js.erb
<%
soy_errors = [env[Googly::Soy::ENV_ERRORS]].flatten.compact
if !soy_errors.empty? -%>
try{console.error('Soy Compiler: <%= soy_errors.size %> error(s)', "\n\n", <%= soy_errors.join("\n").dump %>)}catch(err){}
<% else
@response = goog.compile(%w{
--compilation_level ADVANCED_OPTIMIZATIONS
--js_output_file js.out
--summary_detail_level 3
--define goog.DEBUG=false
--ns googly.demos.compiler
}).to_response_with_console
end -%>