Skip to content

Instantly share code, notes, and snippets.

@ane
Created November 30, 2014 10:45
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 ane/1f5edc39d2f5a27e4dd7 to your computer and use it in GitHub Desktop.
Save ane/1f5edc39d2f5a27e4dd7 to your computer and use it in GitHub Desktop.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>asdf asdf asdf asdf</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- { body: cont } from gulpfile.js:41 is rendered here -->
<%= body %>
<!--[if lt IE 8]>
<p class="browsehappy">You are using an <strong>outdated</strong> browser.
Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve
your experience.</p>
<![endif]-->
<script src="app.js"></script>
<!-- Google Analytics -->
<script>
(function (b, o, i, l, e, r) {
b.GoogleAnalyticsObject = l;
b[l] || (b[l] =
function () {
(b[l].q = b[l].q || []).push(arguments)
});
b[l].l = +new Date;
e = o.createElement(i);
r = o.getElementsByTagName(i)[0];
e.src = '//www.google-analytics.com/analytics.js';
r.parentNode.insertBefore(e, r)
}(window, document, 'script', 'ga'));
ga('createDeveloper', 'UA-XXXXX-X');
ga('send', 'pageview');
</script>
</body>
</html>
// relevant requires:
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var fs = require('fs');
var path = require('path');
var through2 = require('through2');
var ReactRouter = require('react-router');
var ReactTools = require('react-tools');
var React = require('react');
var _ = require('lodash');
// constants omitted e.g. GOOGLE_ANALYTICS_ID
gulp.task('pages', function() {
src.pages = ['src/pages/**/*.jsx'];
var renderJsxToHtml = function(options) {
var stream = through2.obj(function (file, enc, cb) {
var relativePath = path.basename(file.path, '.jsx').toLowerCase();
var templ = fs.readFileSync(options.template, {encoding:'utf8'});
// ES6 JSX -> JS
var routesJS = fs.readFileSync('./src/routes.js');
var routesTransformed = ReactTools.transform(routesJS.toString('utf-8'), { harmony: true });
var mod = new module.constructor();
mod.id = './src/routes.js';
mod.filename = './src/routes.js';
mod.paths = module.paths.slice();
mod.paths.push(path.join(__dirname, 'src'));
mod._compile(routesTransformed, './src/routes.js');
var routes = mod.exports;
var self = this;
var filename = relativePath;
if (relativePath == 'index')
relativePath = '';
ReactRouter.run(routes, '/' + relativePath, function (Handler) {
var html = React.renderToString(React.createElement(Handler, null));
var cont = new Buffer(html);
var rendered = _.template(templ, { body: cont });
file.path = path.join(path.dirname(file.path), filename + ".html");
file.contents = new Buffer(rendered);
self.push(file);
return cb();
});
});
return stream;
};
return gulp.src(src.pages)
.pipe($.changed(DEST, {extension: '.html'}))
.pipe($.if('*.jsx', renderJsxToHtml({template: './src/pages/_template.html'})))
.pipe($.replace('UA-XXXXX-X', GOOGLE_ANALYTICS_ID))
.pipe($.if(RELEASE, $.htmlmin({
removeComments: true,
collapseWhitespace: true,
minifyJS: true
}), $.jsbeautifier()))
.pipe(gulp.dest(DEST))
.pipe($.size({title: 'pages'}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment