var path = require('path');
var DtsCreator = require('typed-css-modules');
var loaderUtils = require('loader-utils');

module.exports = function(source, map) {
  this.cacheable && this.cacheable();
  this.addDependency(this.resourcePath);
  var callback = this.async();

  // Pass on query parameters as an options object to the DtsCreator. This lets
  // you change the default options of the DtsCreator and e.g. use a different
  // output folder.
  var options = loaderUtils.getOptions(this) || {};
  var context = options.context || this.context || this.rootContext;
  var emitFile = !options.noEmit;
  
  // Make sure to not modify options object directly
  var creatorOptions = JSON.parse(JSON.stringify(options));
  delete creatorOptions.noEmit;

  var creator = new DtsCreator(creatorOptions);

  // creator.create(..., source) tells the module to operate on the
  // source variable. Check API for more details.
  creator.create(this.resourcePath, source).then(content => {
    if (emitFile) {
        // Emit the created content as well
        this.emitFile(path.relative(context, content.outputFilePath), content.contents || [''], map);
    }
    content.writeFile().then(_ => {
      callback(null, source, map);
    });
  });
};

var path = require('path');
var DtsCreator = require('typed-css-modules');
var loaderUtils = require('loader-utils');

module.exports = function(source, map) {
  this.cacheable && this.cacheable();
  this.addDependency(this.resourcePath);
  var callback = this.async();

  // Pass on query parameters as an options object to the DtsCreator. This lets
  // you change the default options of the DtsCreator and e.g. use a different
  // output folder.
  var options = loaderUtils.getOptions(this) || {};
  var context = options.context || this.context || this.rootContext;
  var emitFile = !options.noEmit;
  
  // Make sure to not modify options object directly
  var creatorOptions = Object.assign({}, options);
  delete creatorOptions.noEmit;

  var creator = new DtsCreator(creatorOptions);

  // creator.create(..., source) tells the module to operate on the
  // source variable. Check API for more details.
  creator.create(this.resourcePath, source).then(content => {
    if (emitFile) {
        // Emit the created content as well
        this.emitFile(path.relative(context, content.outputFilePath), content.contents || [''], map);
    }
    content.writeFile().then(_ => {
      callback(null, source, map);
    });
  });
};

Use Object.assign for better performance