default = function (_ref) {
var t = _ref.types;
return {
manipulateOptions: function manipulateOptions(babelOptions) {
var _this = this;
var findPluginOptions = babelOptions.plugins.find(function (plugin) {
return plugin[0] === _this;
})[1];
findPluginOptions = manipulatePluginOptions(findPluginOptions);
this.customCWD = findPluginOptions.cwd;
},
pre: function pre(file) {
var customCWD = this.plugin.customCWD;
if (customCWD === 'babelrc') {
var startPath = file.opts.filename === 'unknown' ? './' : file.opts.filename;
var _findBabelConfig$sync = _findBabelConfig2.default.sync(startPath),
babelFile = _findBabelConfig$sync.file;
customCWD = babelFile ? _path2.default.dirname(babelFile) : null;
}
this.moduleResolverCWD = customCWD || process.cwd();
},
visitor: {
CallExpression: {
exit: function exit(nodePath, state) {
if (nodePath.node.seen) {
return;
}
(0, _require2.default)(t, nodePath, mapModule, state, this.moduleResolverCWD);
(0, _jest2.default)(t, nodePath, mapModule, state, this.moduleResolverCWD);
(0, _systemImport2.default)(t, nodePath, mapModule, state, this.moduleResolverCWD);
// eslint-disable-next-line no-param-reassign
nodePath.node.seen = true;
}
},
ImportDeclaration: {
exit: function exit(nodePath, state) {
(0, _import2.default)(t, nodePath, mapModule, state, this.moduleResolverCWD);
}
}
}
};
}n/a
function manipulatePluginOptions(pluginOpts) {
if (pluginOpts.root) {
// eslint-disable-next-line no-param-reassign
pluginOpts.root = pluginOpts.root.reduce(function (resolvedDirs, dirPath) {
if (_glob2.default.hasMagic(dirPath)) {
return resolvedDirs.concat(_glob2.default.sync(dirPath).filter(function (p) {
return _fs2.default.lstatSync(p).isDirectory();
}));
}
return resolvedDirs.concat(dirPath);
}, []);
}
return pluginOpts;
}n/a
function mapModule(sourcePath, currentFile, pluginOpts, cwd) {
// Do not map source starting with a dot
if (sourcePath[0] === '.') {
return null;
}
return (0, _getRealPath2.default)(sourcePath, currentFile, {
cwd: cwd,
pluginOpts: pluginOpts,
extensions: pluginOpts.extensions || defaultExtensions
});
}n/a
function getRealPath(sourcePath, currentFile) {
var opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if (sourcePath[0] === '.') {
return sourcePath;
}
// file param is a relative path from the environment current working directory
// (not from cwd param)
var absCurrentFile = _path2.default.resolve(currentFile);
var cwd = opts.cwd,
extensions = opts.extensions,
pluginOpts = opts.pluginOpts;
var rootDirs = pluginOpts.root || [];
var alias = pluginOpts.alias || {};
var sourceFileFromRoot = getRealPathFromRootConfig(sourcePath, absCurrentFile, rootDirs, cwd, extensions);
if (sourceFileFromRoot) {
return sourceFileFromRoot;
}
var sourceFileFromAlias = getRealPathFromAliasConfig(sourcePath, absCurrentFile, alias, cwd);
if (sourceFileFromAlias) {
return sourceFileFromAlias;
}
return sourcePath;
}n/a
function transformImportCall(t, nodePath, mapper, state, cwd) {
var source = nodePath.get('source');
if (source.type === 'StringLiteral') {
var modulePath = mapper(source.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
source.replaceWith(t.stringLiteral(modulePath));
}
}
}n/a
function transformJestCalls(t, nodePath, mapper, state, cwd) {
var calleePath = nodePath.get('callee');
var jestMethods = ['genMockFromModule', 'mock', 'unmock', 'doMock', 'dontMock'];
if (!(t.isMemberExpression(calleePath.node) && t.isIdentifier(calleePath.node.object, { name: 'jest' }) && jestMethods.some(function
(methodName) {
return t.isIdentifier(calleePath.node.property, { name: methodName });
}))) {
return;
}
var args = nodePath.get('arguments');
if (!args.length) {
return;
}
var moduleArg = args[0];
if (moduleArg.node.type === 'StringLiteral') {
var modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
var newArgs = [].concat(_toConsumableArray(args)).map(function (a) {
return a.node;
});
newArgs[0] = t.stringLiteral(modulePath);
nodePath.replaceWith(t.callExpression(calleePath.node, newArgs));
}
}
}n/a
function mapToRelative(cwd, currentFile, module) {
var from = _path2.default.dirname(currentFile);
var to = _path2.default.normalize(module);
from = resolve(cwd, from);
to = resolve(cwd, to);
var moduleMapped = _path2.default.relative(from, to);
return (0, _utils.toPosixPath)(moduleMapped);
}n/a
function transformRequireCall(t, nodePath, mapper, state, cwd) {
var calleePath = nodePath.get('callee');
if (!t.isIdentifier(calleePath.node, { name: 'require' }) && !(t.isMemberExpression(calleePath.node) && t.isIdentifier(calleePath
.node.object, { name: 'require' }))) {
return;
}
var args = nodePath.get('arguments');
if (!args.length) {
return;
}
var moduleArg = args[0];
if (moduleArg.node.type === 'StringLiteral') {
var modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
nodePath.replaceWith(t.callExpression(calleePath.node, [t.stringLiteral(modulePath)]));
}
}
}n/a
function transformSystemImportCall(t, nodePath, mapper, state, cwd) {
var calleePath = nodePath.get('callee');
if (!(t.isMemberExpression(calleePath.node) && t.isIdentifier(calleePath.node.object, { name: 'System' }) && t.isIdentifier(calleePath
.node.property, { name: 'import' }))) {
return;
}
var args = nodePath.get('arguments');
if (!args.length) {
return;
}
var moduleArg = args[0];
if (moduleArg.node.type === 'StringLiteral') {
var modulePath = mapper(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
nodePath.replaceWith(t.callExpression(calleePath.node, [t.stringLiteral(modulePath)]));
}
}
}n/a
function replaceExtension(p, ext) {
var filename = _path2.default.basename(p, _path2.default.extname(p)) + ext;
return _path2.default.join(_path2.default.dirname(p), filename);
}n/a
function toLocalPath(p) {
return p.replace(/\/index$/, '') // remove trailing /index
.replace(/^(?!\.)/, './'); // insert `./` to make it a local path
}n/a
function toPosixPath(modulePath) {
return modulePath.replace(/\\/g, '/');
}n/a