meshviewer/gulp/tasks/html.js

49 lines
1.7 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, prop) {
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 'this["' + prop + '"] = ' + json + ';';
};
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-05 17:23:40 +00:00
return '<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 })))
.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));
};
};