Skip to content

Instantly share code, notes, and snippets.

@steida
Created January 6, 2016 22:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steida/fd95f34a4deaa317bd06 to your computer and use it in GitHub Desktop.
Save steida/fd95f34a4deaa317bd06 to your computer and use it in GitHub Desktop.
Gulp task for static rendering for Este
gulp.task('static-render', done => {
args.production = true;
const pages = {
'/': 'index.html',
'/foo-bla-bar': '404.html'
};
const fetch = path => new Promise((resolve, reject) => {
require('http').get({host: 'localhost', path, port: 8000}, res => {
// Explicitly treat incoming data as utf8 (avoids issues with multi-byte).
res.setEncoding('utf8');
let body = '';
res.on('data', data => body += data);
res.on('end', () => resolve(body));
}).on('error', reject);
});
const staticRender = () => {
const promises = Object.keys(pages)
.map(path => fetch(path).then(html => {
require('fs')
.writeFile('build/' + pages[path], html.replace(/_assets\//g, ''));
}));
return Promise.all(promises);
};
runSequence('clean', 'build', () => {
const proc = require('child_process').spawn('node', ['./src/server']);
proc.stdout.on('data', data => {
if (data.indexOf('Server started') === -1) return;
staticRender().then(() => {
done();
proc.kill();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment