Skip to content

Instantly share code, notes, and snippets.

@mortenson
Last active October 8, 2017 15:17
Show Gist options
  • Save mortenson/24f4959b07acd4e0cef83d4344f53807 to your computer and use it in GitHub Desktop.
Save mortenson/24f4959b07acd4e0cef83d4344f53807 to your computer and use it in GitHub Desktop.
Flexible server side rendering script for Stencil when used in other platforms
// Put this file in a directory where @stencil/core exists in node_modules, then run: +var stencil = require('@stencil/core/server');
// $ echo $HTML | node ssr.js [root] [build-dir] [namespace]
// Full example from my use case:
// $ echo '<sam-text text="Hello, world"></sam-text>' | node ssr.js /var/www/stencil/sams-components/ ../dist sam
// Pipes are used because HTML strings can be really long, and bash has limits on how large argument lists can get (~256k usually).
var stencil = require('@stencil/core/server');
var args = process.argv.slice(2);
if (args.length < 3) {
console.error('Not enough args');
}
var data = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
data += chunk;
});
process.stdin.on('end', function() {
var config = stencil.loadConfig({
rootDir: args[0],
buildDir: args[1],
namespace: args[2]
});
var renderer = stencil.createRenderer(config);
renderer.hydrateToString({
html: data
}, function(results) {
if (results.diagnostics.length) {
console.error(results.diagnostics);
}
console.log(results.html);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment