<commit_before>var ts = require('typescript');

exports.translate = function(load) {
  const result = ts.transpileModule(load.source, {
    fileName: load.name.slice(0, load.name.indexOf('!')),
    compilerOptions: {
      module: ts.ModuleKind.CommonJS,
      sourceMap: true
    }
  });

  var sourceMap = JSON.parse(result.sourceMapText);
  load.source = result.outputText + '\n//# sourceMappingURL=' + formatSourceMap(sourceMap, load.source);
};

// convert source map from V3 format to base64
// source map formatting taken from
// https://github.com/ilyavf/steal-coffee/blob/master/main.js#L27-L38
function formatSourceMap(sourceMap, orig){
  sourceMap.sourcesContent = [orig];
  sourceMap = JSON.stringify(sourceMap);
  return 'data:application/json;base64,' + btoa(unescape(encodeURIComponent(sourceMap)));
}
<commit_msg>Handling TypeScript files importing other TypeScript files<commit_after>var ts = require('typescript');
var loader = require('@loader');

// translate hook will transpile TypeScript to JavaScript
// will only be called for .ts files
exports.translate = function(load) {
  const result = ts.transpileModule(load.source, {
    fileName: load.name.slice(0, load.name.indexOf('!')),
    compilerOptions: {
      module: ts.ModuleKind.CommonJS,
      sourceMap: true
    }
  });

  var sourceMap = JSON.parse(result.sourceMapText);
  load.source = result.outputText + '\n//# sourceMappingURL=' + formatSourceMap(sourceMap, load.source);
};

// convert source map from V3 format to base64
// source map formatting taken from
// https://github.com/ilyavf/steal-coffee/blob/master/main.js#L27-L38
function formatSourceMap(sourceMap, orig){
  sourceMap.sourcesContent = [orig];
  sourceMap = JSON.stringify(sourceMap);
  return 'data:application/json;base64,' + btoa(unescape(encodeURIComponent(sourceMap)));
}


// handle TypeScript files importing other Typescript files
var _normalize = loader.normalize;
loader.normalize = function(moduleIdentifier, parentName) {
  var parentParts = parentName && parentName.split('!');
  var parentPath = parentParts && parentParts[0].slice(0, parentParts[0].lastIndexOf('/') + 1);
  var parentIsTypeScript = parentParts && parentParts[1] && parentParts[1].indexOf('steal-typescript') === 0;

  return _normalize
    .apply(this, arguments)
    .then(function(moduleName) {

      // modules with !steal-typescript plugin in their moduleName seem to be normalized incorrectly
      return parentIsTypeScript ?
        moduleName.split('#')[0] + '#' + parentPath + moduleName.split('#')[1] :
        // if the parent isn't typescript, treat it normally
        moduleName
    });
};

// Try to fetch files as `.ts` before `.js`
var _fetch = loader.fetch;
loader.fetch = function(load) {
  var l = this;
  var args = arguments;

  var tsLoad = Object.assign({}, load, {
    address: load.address.split('.')[0] + '.ts'
  });

  return _fetch
    .apply(this, [ tsLoad ])
    .catch(function(err) {
      return _fetch.apply(l, args);
    });
};

{'fraction_non_alphanumeric': 0.13962012662445852, 'fraction_numerical': 0.009996667777407531, 'mean_word_length': 4.114139693356048, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2535304', 'n_tokens_mistral': 939, 'n_tokens_neox': 918, 'n_words': 215}