title: How can I serialize JavaScript code to a file using Rhino shell?
body: <p>I am using Rhino as part of an Ant build process to bundle and minify JavaScript. In addition to that, I would also like pre-compile client-side templates, i.e. compile them from markup to JavaScript. At a glance, I thought Rhino's serialize() method would do it but that does not seem to be the case.</p>
  
  <pre><code>// Load templating engine
  load( "engine.js" );
  
  // Read template file
  var template = readFile( "foo.template" ),
  
  // Compile template
  compiled = engine.compile( template );
  
  // Write compiled template to file
  serialize( compiled, "compiledFoo.js" );
  </code></pre>
  
  <p>This results in a binary file being written. What I want is a text file which contains the compiled template.</p>
  
  <p>If using serialize() is not the answer, then what is? Since it's Rhino, it would be possible to import Java classes as well. Offhand, I can't figure out a way to do it.</p>
  
  <p>I know this can be done in Node but I'm not in a position to migrate the build process from Ant-Rhino to Grunt-Node right now.</p>