Completed
Pull Request — develop (#217)
by
unknown
33s
created

html.js ➔ ... ➔ JSON.stringify   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
dl 0
loc 7
rs 9.4285
nop 2
1
const fs = require('fs');
2
3
// stringify functions https://gist.github.com/cowboy/3749767
4
var stringify = function (obj, prop) {
5
  var placeholder = '____PLACEHOLDER____';
6
  var fns = [];
7
  var json = JSON.stringify(obj, function (key, value) {
8
    if (typeof value === 'function') {
9
      fns.push(value);
10
      return placeholder;
11
    }
12
    return value;
13
  }, 2);
14
  json = json.replace(new RegExp('"' + placeholder + '"', 'g'), function () {
15
    return fns.shift();
16
  });
17
  return 'this["' + prop + '"] = ' + json + ';';
18
};
19
20
module.exports = function (gulp, plugins, config, env) {
21
  return function html() {
22
    return gulp.src(env.production() ? config.build + '/*.html' : 'html/*.html')
23
      .pipe(plugins.inject(gulp.src(['config.js']), {
24
        starttag: '<!-- inject:config -->',
25
        transform: function () {
26
          delete require.cache[require.resolve('../../config.default')];
27
          delete require.cache[require.resolve('../../config')];
28
          var buildConfig = Object.assign({}, require('../../config.default')(), require('../../config')());
29
          return '<script>window.config =' +
30
            stringify(buildConfig)
31
              .replace('<!-- inject:cache-breaker -->',
32
                Math.random().toString(12).substring(7)) +
33
            ';</script>';
34
        }
35
      }))
36
      .pipe(env.production(plugins.kyhInlineSource({ compress: false })))
37
      .pipe(plugins.realFavicon.injectFaviconMarkups(JSON.parse(fs.readFileSync(config.faviconData)).favicon.html_code))
38
      .pipe(plugins.cacheBust({
39
        type: 'timestamp'
40
      }))
41
      .pipe(plugins.htmlmin({
42
        removeComments: true,
43
        collapseWhitespace: true,
44
        minifyJS: true
45
      }))
46
      .pipe(gulp.dest(config.build));
47
  };
48
};
49