Created
January 6, 2016 22:39
-
-
Save steida/fd95f34a4deaa317bd06 to your computer and use it in GitHub Desktop.
Gulp task for static rendering for Este
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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