orionsoft-react-scripts
Version:
Orionsoft Configuration and scripts for Create React App.
184 lines (145 loc) • 6.22 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.mapModule = mapModule;
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _resolve = require('resolve');
var _resolve2 = _interopRequireDefault(_resolve);
var _glob = require('glob');
var _glob2 = _interopRequireDefault(_glob);
var _findBabelConfig = require('find-babel-config');
var _findBabelConfig2 = _interopRequireDefault(_findBabelConfig);
var _mapToRelative = require('./mapToRelative');
var _mapToRelative2 = _interopRequireDefault(_mapToRelative);
var _utils = require('./utils');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function replaceExt(p, ext) {
var filename = _path2.default.basename(p, _path2.default.extname(p)) + ext;
return _path2.default.join(_path2.default.dirname(p), filename);
}
var defaultBabelExtensions = ['.js', '.jsx', '.es', '.es6'];
function mapModule(source, file, pluginOpts, cwd) {
// Do not map source starting with a dot
if (source[0] === '.') {
return null;
}
// Search the file under the custom root directories
var rootDirs = pluginOpts.root || [];
var extensions = pluginOpts.extensions || defaultBabelExtensions;
var resolvedSourceFile = void 0;
rootDirs.some(function (dir) {
try {
// check if the file exists (will throw if not)
resolvedSourceFile = _resolve2.default.sync('./' + source, { basedir: _path2.default.resolve(cwd, dir), extensions: extensions });
return true;
} catch (e) {
return false;
}
});
if (resolvedSourceFile) {
var realSourceFileExtension = _path2.default.extname(resolvedSourceFile);
var sourceFileExtension = _path2.default.extname(source);
// map the source and keep its extension if the import/require had one
var ext = realSourceFileExtension === sourceFileExtension ? realSourceFileExtension : '';
return (0, _utils.toLocalPath)((0, _utils.toPosixPath)(replaceExt((0, _mapToRelative2.default)(cwd, file, resolvedSourceFile), ext)));
}
// The source file wasn't found in any of the root directories. Lets try the alias
var aliasMapping = pluginOpts.alias || {};
var moduleSplit = source.split('/');
var aliasPath = void 0;
while (moduleSplit.length) {
var m = moduleSplit.join('/');
if ({}.hasOwnProperty.call(aliasMapping, m)) {
aliasPath = aliasMapping[m];
break;
}
moduleSplit.pop();
}
// no alias mapping found
if (!aliasPath) {
return null;
}
// remove legacy "npm:" prefix for npm packages
aliasPath = aliasPath.replace(/^(npm:)/, '');
var newPath = source.replace(moduleSplit.join('/'), aliasPath);
// alias to npm module don't need relative mapping
if (aliasPath[0] !== '.') {
return newPath;
}
// relative alias
return (0, _utils.toLocalPath)((0, _utils.toPosixPath)((0, _mapToRelative2.default)(cwd, file, newPath)));
}
exports.default = function (_ref) {
var t = _ref.types;
function transformRequireCall(nodePath, 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 = mapModule(moduleArg.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
nodePath.replaceWith(t.callExpression(calleePath.node, [t.stringLiteral(modulePath)]));
}
}
}
function transformImportCall(nodePath, state, cwd) {
var source = nodePath.get('source');
if (source.type === 'StringLiteral') {
var modulePath = mapModule(source.node.value, state.file.opts.filename, state.opts, cwd);
if (modulePath) {
source.replaceWith(t.stringLiteral(modulePath));
}
}
}
return {
manipulateOptions: function manipulateOptions(babelOptions) {
var _this = this;
var findPluginOptions = babelOptions.plugins.find(function (plugin) {
return plugin[0] === _this;
})[1];
if (findPluginOptions.root) {
findPluginOptions.root = findPluginOptions.root.reduce(function (resolvedDirs, dirPath) {
if (_glob2.default.hasMagic(dirPath)) {
return resolvedDirs.concat(_glob2.default.sync(dirPath));
}
return resolvedDirs.concat(dirPath);
}, []);
}
this.customCWD = findPluginOptions.cwd;
},
pre: function pre(file) {
if (this.customCWD === 'babelrc') {
var startPath = file.opts.filename === 'unknown' ? './' : file.opts.filename;
var _findBabelConfig$sync = _findBabelConfig2.default.sync(startPath);
var babelFile = _findBabelConfig$sync.file;
this.customCWD = babelFile ? _path2.default.dirname(babelFile) : null;
}
this.moduleResolverCWD = this.customCWD || process.cwd();
},
visitor: {
CallExpression: {
exit: function exit(nodePath, state) {
if (nodePath.node.seen) {
return;
}
transformRequireCall(nodePath, state, this.moduleResolverCWD);
// eslint-disable-next-line no-param-reassign
nodePath.node.seen = true;
}
},
ImportDeclaration: {
exit: function exit(nodePath, state) {
transformImportCall(nodePath, state, this.moduleResolverCWD);
}
}
}
};
};