# soynode

> Utility for working with Closure Templates, aka Soy, from within a node.js application.

Latest version **2.1.0** (published 2017-01-20) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install soynode
pnpm add soynode
yarn add soynode
bun add soynode
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2017-01-20 |
| First published | 2012-05-24 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 91 |
| Author | Daniel Pupius |
| Maintainers | antonkovalyov, dpup, medium, nicks |
| Keywords | node, soy, templates, closure |

## Links

- npm: https://www.npmjs.com/package/soynode
- Repository: https://github.com/Medium/soynode
- Issues: https://github.com/Medium/soynode/issues
- npm.io page: https://npm.io/package/soynode

## Dependencies (4)

- [q](https://npm.io/package/q.md) 1.4.1
- [fs-extra](https://npm.io/package/fs-extra.md) 1.0.0
- [closure-templates](https://npm.io/package/closure-templates.md) ^20160825.0.0
- [obvious-closure-library](https://npm.io/package/obvious-closure-library.md) ^20161024.0.0

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2017-01-20
- 2.0.1 — 2016-12-20
- 2.0.0 — 2016-12-05
- 1.1.0 — 2015-10-09
- 1.0.1 — 2015-06-10
- 1.0.0 — 2015-06-05
- 0.7.6 — 2015-05-19
- 0.7.5 — 2015-03-11
- 0.7.4 — 2015-03-03
- 0.7.3 — 2015-02-25
- 0.7.2 — 2014-10-29
- 0.7.1 — 2014-10-24
- 0.7.0 — 2014-10-22
- 0.6.1 — 2014-08-21
- 0.6.0 — 2014-08-19
- … 18 more at https://npm.io/package/soynode/versions

## README

soynode
=======

[![Build Status](https://travis-ci.org/Medium/soynode.svg)](https://travis-ci.org/Medium/soynode)

Utility for working with [Closure Templates](https://developers.google.com/closure/templates/),
aka Soy, from within a node.js application.  Supports dynamic recompilation and loading for fast
iteration during development.

Installing
----------

Either:

1. Fork, clone or download the source from GitHub, or
2. Install via NPM using `npm install soynode`


Usage
-----

```js
var soynode = require('../lib/soynode')

soynode.setOptions({
    outputDir: '/tmp/soynode-example'
  , allowDynamicRecompile: true
})

soynode.compileTemplates(__dirname, function (err) {
  if (err) throw err
  // Templates are now ready to use.
  console.log(soynode.render('example.message.hello', {
      name: process.env.USER
    , date: new Date().toLocaleTimeString()
  }))
})
```

Also, see `examples/example.js`.

`soynode.get(templatename)` - Returns a JS function corresponding to the template name.

`soynode.render(templatename, data)` - Returns a string that results from executing a template.

`soynode.setOptions(opts)` - Change the options, see section below.

`soynode.compileTemplates(dir, callback)` - Compiles and loads all `.soy` files in the directory.

`soynode.compileTemplateFiles(files, callback)` - Compiles and loads all `.soy` files.

`soynode.loadCompiledTemplates(dir, callback)` - Loads already compiled templates.

`soynode.loadCompiledTemplateFiles(files, callback)` - Loads already compiled templates.

Where "template name" is referred to, it means the namespace + template name as defined in the Soy
file, and the full JS name that the Soy Compiler generates, for example `project.section.screen`.
See the [Hello World JS](https://developers.google.com/closure/templates/docs/helloworld_js) doc on
the Closure site for more background.

Options
-------

Options can be set via `soynode.setOptions(options)`. Most of these mirror 
[the command-line arguments](https://developers.google.com/closure/templates/docs/javascript_usage)
for `SoyToJsSrcCompiler`. The keys can contain the following:

- `tmpDir` {string} Path to a directory where temporary files will be written during compilation. {Deprecated: use outputDir}
  [Default: /tmp/soynode]
- `inputDir` {string} Optional path to a directory where files will be read. When compiled from a directory, this option will be overwritten with the caller inputDir. [Default: process.cwd()]
- `outputDir` {string} Path to a directory where files will be written. [Default: null]
- `uniqueDir` {boolean} Determines whether the compiled files will be placed in a unique directory. [Default: true]
- `allowDynamicRecompile` {boolean} Whether to watch for changes to the templates. [Default: false]
- `loadCompiledTemplates` {boolean} Whether or not to load the compiled templates. Relevant when you only need to build templates. [Default: true]
- `eraseTemporaryFiles` {boolean} Whether to erase temporary files after a compilation. This option does nothing if allowDynamicRecompile is on, because allowDynamicRecompile reuses the files.
[Default: false]
- `concatOutput` {boolean} Whether the compiled soy.js files should be joined into a single file. This is helpful for loading templates in a browser and simplest to use when `outputDir` is explicitly set and `uniqueDir` is false. [Default: false]
- `concatFileName` {string} File name used for concatenated files, only relevant when concatOutput is true, ".soy.concat.js" is appended, so don't include ".js" yourself. [Default: compiled]
- `locales` {Array.<string>} List of locales to translate the templates to.
- `messageFilePathFormat` {string} Path to the translation file to use, which can contain any of the placeholders allowed on the --messageFilePathFormat option of SoyToJsSrcCompiler.jar.
- `cssHandlingScheme` {string} Processing options for the `css` command. [More info](https://developers.google.com/closure/templates/docs/commands#css)
- `useClosureStyle` {boolean} Whether or not to use goog.provide and goog.require for JS functions and Soy namespaces.
- `shouldGenerateJsdoc` {boolean} Whether or not to generate JSDoc on each template function, with type info for the Closure Compiler. [More info](https://developers.google.com/closure/templates/docs/javascript_usage)
- `shouldProvideRequireSoyNamespaces` {boolean} Use this option to enable usage with Closure Library. [More info](https://developers.google.com/closure/templates/docs/javascript_usage)
- `shouldProvideRequireJsFunctions` {boolean} Use this option to enable usage with Closure Library. [More info](https://developers.google.com/closure/templates/docs/javascript_usage)
- `precompiledDir` {string} A directory of precompiled soy.js files.
  Soynode will check these first and use them if available.
  You can set this to the same value as outputDir to re-use results from previous runs.

**NOTE: Options should be set before templates are loaded or compiled.**

Internationalizion
--------------------

To take advantage of soy's [translation](https://developers.google.com/closure/templates/docs/translation) features through soynode, you should set the `locales` and `messageFilePathFormat` options, like in the example below:

```js
var soynode = require('../lib/soynode')

soynode.setOptions({
    locales: ['pt-BR', 'es'],
    messageFilePathFormat: '/tmp/soynode-example/translations.xlf',
    outputDir: '/tmp/soynode-example'
})

soynode.compileTemplates(__dirname, function (err) {
  if (err) throw err
  // Templates are now ready to use, render specifying the desired locale.
  console.log(soynode.render('example.message.hello', {}, {}, 'pt-BR'))
  console.log(soynode.render('example.message.hello', {}, {}, 'es'))
})
```

Implementation Notes
--------------------

The templates are loaded using Node's [VM Module](http://nodejs.org/api/vm.html).  This allows us to
execute the generated `.soy.js` files as is without a post processing step and without leaking the
template functions into the global scope.

Calling `soynode.get` executes code which returns a reference to the template function within the
VM Context.  The reference is cached, providing a 10x speed up over fetching the template function
each time, or evaluating it in place and returning the template output over the VM boundary.

Contributing
------------

Questions, comments, bug reports, and pull requests are all welcome. Submit them at
[the project on GitHub](https://github.com/Obvious/soynode/).

Bug reports that include steps-to-reproduce (including code) are the best. Even better, make them in
the form of pull requests.

Author
------

[Dan Pupius](https://github.com/dpup)
([personal website](http://pupius.co.uk/about/)), supported by
[The Obvious Corporation](http://obvious.com/).

License
-------

Copyright 2012 [The Obvious Corporation](http://obvious.com/).

Licensed under the Apache License, Version 2.0.
See the top-level file `LICENSE.txt` and
(http://www.apache.org/licenses/LICENSE-2.0).

---
_Source: https://npm.io/package/soynode · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
