"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.addBabelRegisterFiles = addBabelRegisterFiles;
exports.default = _default;

function _react() {
  const data = _interopRequireDefault(require("react"));

  _react = function _react() {
    return data;
  };

  return data;
}

function _path() {
  const data = require("path");

  _path = function _path() {
    return data;
  };

  return data;
}

function _fs() {
  const data = require("fs");

  _fs = function _fs() {
    return data;
  };

  return data;
}

function _registerBabel() {
  const data = _interopRequireDefault(require("af-webpack/registerBabel"));

  _registerBabel = function _registerBabel() {
    return data;
  };

  return data;
}

function _umiUtils() {
  const data = require("umi-utils");

  _umiUtils = function _umiUtils() {
    return data;
  };

  return data;
}

function _getUserConfig() {
  const data = require("umi-core/lib/getUserConfig");

  _getUserConfig = function _getUserConfig() {
    return data;
  };

  return data;
}

function _lodash() {
  const data = require("lodash");

  _lodash = function _lodash() {
    return data;
  };

  return data;
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

let files = [];

function initFiles(cwd) {
  files = (0, _lodash().uniq)(files.concat((0, _getUserConfig().getConfigPaths)(cwd)));
}

function addBabelRegisterFiles(extraFiles, {
  cwd
}) {
  initFiles(cwd);
  files = (0, _lodash().uniq)(files.concat(extraFiles));
}

function _default({
  cwd
}) {
  initFiles(cwd);
  const only = files.map(f => {
    const fullPath = (0, _path().isAbsolute)(f) ? f : (0, _path().join)(cwd, f);
    return (0, _umiUtils().winPath)(fullPath);
  });
  let absSrcPath = (0, _path().join)(cwd, 'src');

  if (!(0, _fs().existsSync)(absSrcPath)) {
    absSrcPath = cwd;
  }

  (0, _registerBabel().default)({
    // only suport glob
    // ref: https://babeljs.io/docs/en/next/babel-core.html#configitem-type
    only,
    babelPreset: [require.resolve('babel-preset-umi'), {
      env: {
        targets: {
          node: 8
        }
      },
      transformRuntime: false
    }],
    babelPlugins: [[require.resolve('babel-plugin-module-resolver'), {
      alias: {
        '@': absSrcPath
      }
    }]]
  });
}