2017-03-27 19:34:02 +00:00
|
|
|
const fs = require('fs');
|
2017-11-10 19:23:20 +00:00
|
|
|
|
|
|
|
// stringify functions https://gist.github.com/cowboy/3749767
|
2017-11-21 08:36:09 +00:00
|
|
|
var stringify = function (obj) {
|
2017-11-10 19:23:20 +00:00
|
|
|
var placeholder = '____PLACEHOLDER____';
|
|
|
|
var fns = [];
|
|
|
|
var json = JSON.stringify(obj, function (key, value) {
|
|
|
|
if (typeof value === 'function') {
|
|
|
|
fns.push(value);
|
|
|
|
return placeholder;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}, 2);
|
|
|
|
json = json.replace(new RegExp('"' + placeholder + '"', 'g'), function () {
|
|
|
|
return fns.shift();
|
|
|
|
});
|
2017-11-21 08:36:09 +00:00
|
|
|
return json;
|
2017-11-10 19:23:20 +00:00
|
|
|
};
|
|
|
|
|
2017-03-22 22:13:37 +00:00
|
|
|
module.exports = function (gulp, plugins, config, env) {
|
|
|
|
return function html() {
|
|
|
|
return gulp.src(env.production() ? config.build + '/*.html' : 'html/*.html')
|
2017-11-10 19:23:20 +00:00
|
|
|
.pipe(plugins.inject(gulp.src(['config.js']), {
|
2017-03-22 22:13:37 +00:00
|
|
|
starttag: '<!-- inject:config -->',
|
2017-11-10 19:23:20 +00:00
|
|
|
transform: function () {
|
|
|
|
delete require.cache[require.resolve('../../config.default')];
|
|
|
|
delete require.cache[require.resolve('../../config')];
|
|
|
|
var buildConfig = Object.assign({}, require('../../config.default')(), require('../../config')());
|
2017-11-11 23:54:50 +00:00
|
|
|
return '<title>' + buildConfig.siteName + ' - loading...</title>' +
|
|
|
|
'<script>window.config =' +
|
2017-11-10 19:23:20 +00:00
|
|
|
stringify(buildConfig)
|
2017-03-22 22:13:37 +00:00
|
|
|
.replace('<!-- inject:cache-breaker -->',
|
|
|
|
Math.random().toString(12).substring(7)) +
|
2017-11-10 19:23:20 +00:00
|
|
|
';</script>';
|
2017-03-22 22:13:37 +00:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
.pipe(env.production(plugins.kyhInlineSource({ compress: false })))
|
2017-03-27 19:34:02 +00:00
|
|
|
.pipe(plugins.realFavicon.injectFaviconMarkups(JSON.parse(fs.readFileSync(config.faviconData)).favicon.html_code))
|
2017-03-22 22:13:37 +00:00
|
|
|
.pipe(plugins.cacheBust({
|
|
|
|
type: 'timestamp'
|
|
|
|
}))
|
|
|
|
.pipe(plugins.htmlmin({
|
|
|
|
removeComments: true,
|
|
|
|
collapseWhitespace: true,
|
|
|
|
minifyJS: true
|
|
|
|
}))
|
|
|
|
.pipe(gulp.dest(config.build));
|
|
|
|
};
|
|
|
|
};
|