UNPKG

babel-plugin-webpack-alias

Version:
121 lines (88 loc) 5.07 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); exports.default = function (_ref) { let t = _ref.types; return { visitor: { CallExpression(path, _ref2) { let filename = _ref2.file.opts.filename; var _ref2$opts = _ref2.opts; _ref2$opts = _ref2$opts === undefined ? {} : _ref2$opts; var _ref2$opts$config = _ref2$opts.config; let configPath = _ref2$opts$config === undefined ? 'webpack.config.js' : _ref2$opts$config; var _ref2$opts$findConfig = _ref2$opts.findConfig; let findConfig = _ref2$opts$findConfig === undefined ? false : _ref2$opts$findConfig; // Get webpack config const conf = getConfig(configPath, findConfig); // If the config comes back as null, we didn't find it, so throw an exception. if (conf === null) { throw new Error('Cannot find configuration file: ' + configPath); } // exit if there's no alias config if (!conf.resolve || !conf.resolve.alias) { return; } // Get the webpack alias config const aliasConf = conf.resolve.alias; var _path$node = path.node; const calleeName = _path$node.callee.name; const args = _path$node.arguments; // Exit if it's not a require statement if (calleeName !== 'require' || !args.length || !t.isStringLiteral(args[0])) { return; } // Get the path of the StringLiteral var _args = _slicedToArray(args, 1); const filePath = _args[0].value; for (let aliasFrom in aliasConf) { if (aliasConf.hasOwnProperty(aliasFrom)) { let aliasTo = aliasConf[aliasFrom]; // If the filepath is not absolute, make it absolute if (!(0, _path.isAbsolute)(aliasTo)) { aliasTo = (0, _path.join)(process.cwd(), aliasTo); } let regex = new RegExp(`^${ aliasFrom }(\/|$)`); // If the regex matches, replace by the right config if (regex.test(filePath)) { let relativeFilePath = (0, _path.relative)((0, _path.dirname)(filename), aliasTo).replace(/\\/g, '/'); // In case the file path is the root of the alias, need to put a dot to avoid having an absolute path if (relativeFilePath.length === 0) { relativeFilePath = '.'; } let requiredFilePath = filePath.replace(aliasFrom, relativeFilePath); // In the unfortunate case of a file requiring the current directory which is the alias, we need to add // an extra slash if (requiredFilePath === '.') { requiredFilePath = './'; } // In the case of a file requiring a child directory of the current directory, we need to add a dot slash if (['.', '/'].indexOf(requiredFilePath[0]) === -1) { requiredFilePath = './' + requiredFilePath; } path.node.arguments = [(0, _babelTypes.StringLiteral)(requiredFilePath)]; return; } } } } } }; }; var _path = require('path'); var _babelTypes = require('babel-types'); var _findUp = require('find-up'); var _findUp2 = _interopRequireDefault(_findUp); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function getConfig(configPath, findConfig) { var conf; if (!findConfig) { // Get webpack config conf = require((0, _path.resolve)(process.cwd(), configPath)); } else { conf = require(_findUp2.default.sync(configPath)); } return conf; }