var loaderUtils = require('loader-utils');
var fontgen = require('webfonts-generator');
var path = require('path');
var glob = require('glob');

var mimeTypes = {
  'eot': 'application/vnd.ms-fontobject',
  'svg': 'image/svg+xml',
  'ttf': 'application/x-font-ttf',
  'woff': 'application/font-woff',
  'woff2': 'font/woff2'
};

function getFilesAndDeps (patterns, context) {
  var files = [];
  var filesDeps = [];
  var directoryDeps = [];

  function addFile (file) {
    filesDeps.push(file);
    files.push(path.resolve(context, file));
  }

  function addByGlob (globExp) {
    var globOptions = {cwd: context};

    var foundFiles = glob.sync(globExp, globOptions);
    files = files.concat(foundFiles.map(file => {
      return path.resolve(context, file);
    }));

    var globDirs = glob.sync(path.dirname(globExp) + '/', globOptions);
    directoryDeps = directoryDeps.concat(globDirs.map(file => {
      return path.resolve(context, file);
    }));
  }

  // Re-work the files array.
  patterns.forEach(function (pattern) {
    if (glob.hasMagic(pattern)) {
      addByGlob(pattern);
    } else {
      addFile(pattern);
    }
  });

  return {
    files: files,
    dependencies: {
      directories: directoryDeps,
      files: filesDeps
    }
  };
}

module.exports = function (content) {
  this.cacheable();
  var params = loaderUtils.parseQuery(this.query);
  var config;
  try {
    config = JSON.parse(content);
  } catch (ex) {
    config = this.exec(content, this.resourcePath);
  }

  config.__dirname = path.dirname(this.resourcePath);

  // Sanity check
  /*
   if(typeof config.fontName != "string" || typeof config.files != "array") {
   this.reportError("Typemismatch in your config. Verify your config for correct types.");
   return false;
   }
   */
  var filesAndDeps = getFilesAndDeps(config.files, this.context);
  filesAndDeps.dependencies.files.forEach(this.addDependency.bind(this));
  filesAndDeps.dependencies.directories.forEach(this.addContextDependency.bind(this));
  config.files = filesAndDeps.files;

  // With everything set up, let's make an ACTUAL config.
  var formats = config.types || ['eot', 'woff', 'woff2', 'ttf', 'svg'];
  if (formats.constructor !== Array) {
    formats = [formats];
  }

  var fontconf = {
    files: config.files,
    fontName: config.fontName,
    types: formats,
    order: formats,
    fontHeight: config.fontHeight || 1000, // Fixes conversion issues with small svgs,
    codepoints: config.codepoints || {},
    templateOptions: {
      baseSelector: config.baseSelector || '.icon',
      classPrefix: 'classPrefix' in config ? config.classPrefix : 'icon-'
    },
    dest: '',
    writeFiles: false,
    formatOptions: config.formatOptions || {}
  };

  // This originally was in the object notation itself.
  // Unfortunately that actually broke my editor's syntax-highlighting...
  // ... what a shame.
  if (typeof config.rename === 'function') {
    fontconf.rename = config.rename;
  } else {
    fontconf.rename = function (f) {
      return path.basename(f, '.svg');
    };
  }

  if (config.cssTemplate) {
    fontconf.cssTemplate = path.resolve(this.context, config.cssTemplate);
  }

  if (config.cssFontsPath) {
    fontconf.cssFontsPath = path.resolve(this.context, config.cssFontsPath);
  }

  for (var option in config.templateOptions) {
    if (config.templateOptions.hasOwnProperty(option)) {
      fontconf.templateOptions[option] = config.templateOptions[option];
    }
  }

  // svgicons2svgfont stuff
  var keys = [
    'fixedWidth',
    'centerHorizontally',
    'normalize',
    'fontHeight',
    'round',
    'descent'
  ];
  for (var x in keys) {
    if (typeof config[keys[x]] !== 'undefined') {
      fontconf[keys[x]] = config[keys[x]];
    }
  }

  var cb = this.async();
  var self = this;
  var opts = this.options;
  var pub = (
    (opts.output && opts.output.publicPath) || '/'
  );
  var embed = !!params.embed;

  if (fontconf.cssTemplate) {
    this.addDependency(fontconf.cssTemplate);
  }

  if (fontconf.cssFontsPath) {
    this.addDependency(fontconf.cssFontsPath);
  }

  fontgen(fontconf, function (err, res) {
    if (err) {
      return cb(err);
    }
    var urls = {};
    for (var i in formats) {
      var format = formats[i];
      if (!embed) {
        var filename = config.fileName || params.fileName || '[hash]-[fontname].[ext]';
        filename = filename
          .replace('[fontname]', fontconf.fontName)
          .replace('[ext]', format);
        var url = loaderUtils.interpolateName(this,
          filename,
          {
            context: self.options.context || this.context,
            content: res[format]
          }
        );
        urls[format] = path.join(pub, url).replace(/\\/g, '/');
        self.emitFile(url, res[format]);
      } else {
        urls[format] = 'data:' +
          mimeTypes[format] +
          ';charset=utf-8;base64,' +
          (new Buffer(res[format]).toString('base64'));
      }
    }
    cb(null, res.generateCss(urls));
  });
};

@@ -1503,283 +1503,8 @@
  %7D%0A%0A
-  config.__dirname = path.dirname(this.resourcePath);%0A%0A  // Sanity check%0A  /*%0A   if(typeof config.fontName != %22string%22 %7C%7C typeof config.files != %22array%22) %7B%0A   this.reportError(%22Typemismatch in your config. Verify your config for correct types.%22);%0A   return false;%0A   %7D%0A   */%0A
   va
@@ -1969,24 +1969,38 @@
 %0A%0A  var 
-fontconf
+generatorConfiguration
  = %7B%0A   
@@ -2663,32 +2663,46 @@
 ion') %7B%0A    
-fontconf
+generatorConfiguration
 .rename = co
@@ -2729,24 +2729,38 @@
 e %7B%0A    
-fontconf
+generatorConfiguration
 .rename 
@@ -2855,32 +2855,46 @@
 late) %7B%0A    
-fontconf
+generatorConfiguration
 .cssTemplate
@@ -2982,24 +2982,38 @@
 ) %7B%0A    
-fontconf
+generatorConfiguration
 .cssFont
@@ -3184,24 +3184,38 @@
 %7B%0A      
-fontconf
+generatorConfiguration
 .templat
@@ -3510,24 +3510,38 @@
 %7B%0A      
-fontconf
+generatorConfiguration
 %5Bkeys%5Bx%5D
@@ -3746,32 +3746,46 @@
 bed;%0A%0A  if (
-fontconf
+generatorConfiguration
 .cssTemplate
@@ -3807,32 +3807,46 @@
 dDependency(
-fontconf
+generatorConfiguration
 .cssTemplate
@@ -3859,24 +3859,38 @@
 %0A%0A  if (
-fontconf
+generatorConfiguration
 .cssFont
@@ -3921,24 +3921,38 @@
 endency(
-fontconf
+generatorConfiguration
 .cssFont
@@ -3974,24 +3974,38 @@
 fontgen(
-fontconf
+generatorConfiguration
 , functi
@@ -4315,16 +4315,30 @@
 %5D', 
-fontconf
+generatorConfiguration
 .fon

Rename some variables