All files index.js

100% Statements 58/58
92.86% Branches 13/14
100% Functions 8/8
100% Lines 55/55
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125    1x     1x   1x   1x   1x   1x   1x   1x   1x 1x 1x 1x   1x                 1x   1x               1x 16x 16x     1x 8x 8x 7x 7x     1x     1x 9x 9x     1x 7x 7x 7x 7x 7x   7x     1x 1x   1x     15x   15x   6x   6x         6x   6x         6x   6x   3x   3x   3x   3x     6x   6x   3x   3x   3x   3x        
"use strict";
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getNewPath = exports.getModuleType = exports.traverseExpression = exports.isBuckleScriptFile = undefined;
 
var _path = require("path");
 
var _debug = require("debug");
 
var _debug2 = _interopRequireDefault(_debug);
 
var _child_process = require("child_process");
 
var _babelTypes = require("babel-types");
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
var debug = (0, _debug2.default)("babel-plugin-bucklescript");
var globalPath = process.cwd();
var compileDir = "lib";
var fileRegex = /\.(re|ml)$/;
 
var bsb = void 0;
 
/* istanbul ignore next */
try {
  bsb = require.resolve("bs-platform/bin/bsb.exe");
} catch (_) {
  bsb = "bsb";
}
 
debug("Spawning bsb watch process");
 
var watcher = (0, _child_process.spawn)(bsb, ["-w"], { stdio: ["ignore", "pipe", "pipe"] });
 
/* istanbul ignore next */
process.on("exit", function () {
  debug("Terminating bsb watch process");
  watcher.kill();
});
 
var isBuckleScriptFile = exports.isBuckleScriptFile = function isBuckleScriptFile(path) {
  debug("Checking if the file is a bs file: \" " + path + "\"");
  return fileRegex.test(path);
};
 
var traverseExpression = exports.traverseExpression = function traverseExpression(t, arg) {
  debug("Traversing the AST expression");
  if (t.isStringLiteral(arg)) {
    debug("AST expression is a string");
    return arg;
  }
 
  return null;
};
 
var getModuleType = exports.getModuleType = function getModuleType(state) {
  debug("Checking configured module type");
  return state.opts.module || "js";
};
 
var getNewPath = exports.getNewPath = function getNewPath(path, state) {
  debug("Generating new path");
  var sourcePath = (0, _path.dirname)(state.file.opts.filenameRelative);
  var requirePath = (0, _path.resolve)(sourcePath, path);
  var rootPath = requirePath.replace(globalPath, "");
  var newPath = (0, _path.join)(globalPath, compileDir, getModuleType(state), rootPath);
 
  return newPath.replace(fileRegex, ".js");
};
 
exports.default = function (_ref) {
  var t = _ref.types;
 
  return {
    visitor: {
      CallExpression: function CallExpression(path, state) {
        debug("Evaluating call expression");
 
        if (path.node.callee.name !== "require") return;
 
        debug("Call expression is a require call");
 
        var args = path.node.arguments;
 
        /* istanbul ignore next */
        if (!args.length) return;
 
        debug("Call expression has at least one argument");
 
        var firstArg = traverseExpression(t, args[0]);
 
        /* istanbul ignore next */
        if (!firstArg) return;
 
        debug("Call expression has a valid first argument");
 
        if (!isBuckleScriptFile(firstArg.value)) return;
 
        debug("Path is a bucklescript file");
 
        var newPath = getNewPath(firstArg.value, state);
 
        debug("Setting new path: \"" + newPath + "\"");
 
        path.node.arguments = [t.stringLiteral(newPath)];
      },
      ImportDeclaration: function ImportDeclaration(path, state) {
        debug("Evaluating import declaration");
 
        if (!isBuckleScriptFile(path.node.source.value)) return;
 
        debug("Path is a bucklescript file");
 
        var newPath = getNewPath(path.node.source.value, state);
 
        debug("Setting new path: \"" + newPath + "\"");
 
        path.node.source.value = t.stringLiteral(newPath);
      }
    }
  };
};