meshviewer/gulp/tasks/html.js

61 lines
2.2 KiB
JavaScript
Raw Normal View History

const fs = require('fs');
2017-11-10 19:23:20 +00:00
// stringify functions https://gist.github.com/cowboy/3749767
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();
});
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')
.pipe(plugins.realFavicon.injectFaviconMarkups(JSON.parse(fs.readFileSync(config.faviconData)).favicon.html_code))
2017-11-10 19:23:20 +00:00
.pipe(plugins.inject(gulp.src(['config.js']), {
removeTags: true,
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(plugins.inject(gulp.src(['config.js']), {
removeTags: true,
starttag: '<!-- inject:title -->',
transform: function () {
delete require.cache[require.resolve('../../config.default')];
delete require.cache[require.resolve('../../config')];
var buildConfig = Object.assign({}, require('../../config.default')(), require('../../config')());
return buildConfig.siteName;
}
}))
2017-03-22 22:13:37 +00:00
.pipe(env.production(plugins.kyhInlineSource({ compress: false })))
.pipe(plugins.cacheBust({
type: 'timestamp'
}))
.pipe(plugins.htmlmin({
removeComments: true,
collapseWhitespace: true,
minifyJS: true
}))
.pipe(gulp.dest(config.build));
};
};