This repository contains multiple tools to allow us to handle Soy or officially known as Closure Templates based files for a NodeJS based application. It contains a compiler utility, watcher which checks for file changes and recompiles, and it also contains loaders for Webpack to allow for integration between Webpack and Closure Templates.
Note: This is just a layer on top of the original Soy Compiler provided by Google. You still need Java and the Soy compiler to run this.
There are multiple ways to use this tool, examples of each are provided in the example directory.
- Using NodeJS class directly
const SoyBuilder = require("soy-serve").SoyBuilder;
const runExample = async () => {
const sourceFiles = [
"/absoulte/path/to/source/file1.soy",
"/absoulte/path/to/source/file2.soy"
];
const builder = new SoyBuilder({
srcs: sourceFiles.join(","),
locales: "en",
workingDirectory: process.cwd(),
messageFilePathFormat: path.join(__dirname, "translations", "{LOCALE}.xlf"),
outputPathFormat: "./translations_output/{LOCALE}.js"
});
await builder.start();
await builder.stop();
}
runExample()
- Using the CLI
node ./example/example_cli.js --outputPathFormat="./translations_output/{LOCALE}.js" --srcs="/absoulte/path/to/source/file1.soy,/absoulte/path/to/source/file2.soy" --locales=en
- Using Webpack loaders
const path = require("path");
const SoyCompileLoader = require("soy-serve").SoyCompileLoader;
const SoyPostProcessorLoader = require("soy-serve").SoyPostProcessorLoader;
const SoyPreProcessorLoader = require("soy-serve").SoyPreProcessorLoader;
module.exports = {
entry: ['sourceFiles0.js', './sourceFile1.js'],
output: {
path: path.join(__dirname, "..", "dist"),
filename: 'main.js'
},
mode: 'production',
module: {
rules: [
{
test: /\.soy/,
use: [
{
loader: SoyPostProcessorLoader,
},
{
loader: SoyCompileLoader,
options: {
locale: "en",
tempDir: path.join(process.cwd(), "tmp"),
workingDirectory: path.join(process.cwd()),
localeFiles: path.join(process.cwd(), "translations", "{LOCALE}.xlf")
}
},
{
loader: SoyPreProcessorLoader
}
]
}
]
}
}
Accepts the following arguments:
watch
(boolean) - start watching for changes and recompile filesignore
(RegExp) - regex for files and directories to ignore - will compare against absolute pathssoyFilesGlob
(string) - glob of the files you will be watching in the working directoryworkingDirectory
(string) - absolute path to the working directoryverbose
(boolean) - print extra infocustomCompileJarPath
(string) - absolute path to a custom jar file for compiling
In addition to all the arguments accepted by the Soy compiler provided by Google Closure Templates see here
Note: To use the following arguments in the CLI please use the following syntax node ./fileWithPassingArgsToCompiler.js --locales="en" --messageFilePathFormat="/path/something"
- SoyPreProcessorLoader - For processing Soy files before they are compiled into JS:
soyDoc
(boolean) - add SoyDoc comments before templates in Soy file that don't have them to conform to the Soy v2 requirementremoveLeadingEmptySpaces
(boolean) removes empty whitespace before template tags and SoyDoc to conform with Soy v2
- SoyCompileLoader - For compiling Soy files into JS:
workingDirectory
(string) - absolute path to the working directoryverbos
(boolean) - print more info in the consolelocale
(string) - what local do you want to compile forlocaleFiles
(string) - absolute path to the translation xlf files (can use Closure templating string)customCompileJarPath
(string) - absolute path to a different jar file for compiling the Soy filestempDir
(string) - relative path to a temporary directory to hold build artifacts
- SoyPostProcessorLoader - For processing compiled JS files to be friendly to newer syntax:
removeDebug
(boolean) - removes goog.DEBUG based statements from the compiled Soy filesuseProvide
(boolean) - addgoog.provide("soy.namespace")
type statement in the compiled file - cannot be used with modulesmodular
(boolean) - convert the file into goog modules, export all the functions instead of defining them on a global namespaceuseModule
(boolean) - add agoog.module("soy.namespace")
statementdeclareLegacyNamespace
(boolean) - add agoog.module.declareLegacyNamespace()
statement for global access
This is a tool I developed for learning, it is not meant for production usage. Please feel free to add PRs or open issues for any improvements.