mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-22 15:17:53 -05:00
Set up static view rendering
This commit is contained in:
parent
0ee2234c08
commit
3bdb95d40d
3 changed files with 52 additions and 12 deletions
|
@ -1,22 +1,12 @@
|
|||
var crypto = require('crypto');
|
||||
var defaults = require('lodash.defaults');
|
||||
var fs = require('fs');
|
||||
var mustache = require('mustache');
|
||||
var path = require('path');
|
||||
|
||||
var config = require('./config');
|
||||
var render = require('./render.js');
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
function Handler (route) {
|
||||
// Route definition
|
||||
defaults(route, config);
|
||||
|
||||
// Render template
|
||||
var location = path.resolve(__dirname, './template.html');
|
||||
var template = fs.readFileSync(location, 'utf8');
|
||||
var output = mustache.render(template, route);
|
||||
var output = render(route);
|
||||
var checksum = crypto.createHash('md5').update(output).digest('hex');
|
||||
|
||||
return function (req, res) {
|
||||
|
|
16
server/render.js
Normal file
16
server/render.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
var defaults = require('lodash.defaults');
|
||||
var fs = require('fs');
|
||||
var mustache = require('mustache');
|
||||
var path = require('path');
|
||||
|
||||
var config = require('./config');
|
||||
|
||||
module.exports = function (route) {
|
||||
// Route definition
|
||||
defaults(route, config);
|
||||
|
||||
// Render template
|
||||
var location = path.resolve(__dirname, './template.html');
|
||||
var template = fs.readFileSync(location, 'utf8');
|
||||
return mustache.render(template, route);
|
||||
};
|
|
@ -5,6 +5,36 @@ var webpack = require('webpack');
|
|||
|
||||
var routes = require('./server/routes.json');
|
||||
|
||||
|
||||
function HtmlGeneratorPlugin (options) {
|
||||
this.options = options;
|
||||
return this;
|
||||
}
|
||||
|
||||
HtmlGeneratorPlugin.prototype.apply = function (compiler) {
|
||||
var render = this.options.render;
|
||||
var routes = this.options.routes;
|
||||
|
||||
compiler.plugin('emit', function (compilation, callback) {
|
||||
var outputRoutes = {};
|
||||
routes.forEach(function (route) {
|
||||
var filename = route.view + '.html';
|
||||
var content = render(route);
|
||||
outputRoutes[route.pattern] = filename;
|
||||
compilation.assets[filename] = {
|
||||
source: function () {return content;},
|
||||
size: function () {return content.length;}
|
||||
};
|
||||
});
|
||||
var routeJson = JSON.stringify(outputRoutes);
|
||||
compilation.assets['routes.json'] = {
|
||||
source: function () {return routeJson;},
|
||||
size: function () {return routeJson.length;}
|
||||
};
|
||||
callback();
|
||||
});
|
||||
};
|
||||
|
||||
// Prepare all entry points
|
||||
var entry = {
|
||||
init: './src/init.js'
|
||||
|
@ -56,6 +86,10 @@ module.exports = {
|
|||
fs: 'empty'
|
||||
},
|
||||
plugins: [
|
||||
new HtmlGeneratorPlugin({
|
||||
render: require('./server/render.js'),
|
||||
routes: routes
|
||||
}),
|
||||
new CopyWebpackPlugin([
|
||||
{from: 'static'},
|
||||
{from: 'intl', to: 'js'}
|
||||
|
|
Loading…
Reference in a new issue