utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.nodeResolvePath = nodeResolvePath;
  4. exports.isRelativePath = isRelativePath;
  5. exports.toPosixPath = toPosixPath;
  6. exports.toLocalPath = toLocalPath;
  7. exports.stripExtension = stripExtension;
  8. exports.replaceExtension = replaceExtension;
  9. exports.matchesPattern = matchesPattern;
  10. exports.mapPathString = mapPathString;
  11. exports.isImportCall = isImportCall;
  12. exports.escapeRegExp = escapeRegExp;
  13. var _path = _interopRequireDefault(require("path"));
  14. var _resolve = _interopRequireDefault(require("resolve"));
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. function nodeResolvePath(modulePath, basedir, extensions) {
  17. try {
  18. return _resolve.default.sync(modulePath, {
  19. basedir,
  20. extensions
  21. });
  22. } catch (e) {
  23. return null;
  24. }
  25. }
  26. function isRelativePath(nodePath) {
  27. return nodePath.match(/^\.?\.\//);
  28. }
  29. function toPosixPath(modulePath) {
  30. return modulePath.replace(/\\/g, '/');
  31. }
  32. function toLocalPath(modulePath) {
  33. let localPath = modulePath.replace(/\/index$/, ''); // remove trailing /index
  34. if (!isRelativePath(localPath)) {
  35. localPath = `./${localPath}`; // insert `./` to make it a relative path
  36. }
  37. return localPath;
  38. }
  39. function stripExtension(modulePath, stripExtensions) {
  40. let name = _path.default.basename(modulePath);
  41. stripExtensions.some(extension => {
  42. if (name.endsWith(extension)) {
  43. name = name.slice(0, name.length - extension.length);
  44. return true;
  45. }
  46. return false;
  47. });
  48. return name;
  49. }
  50. function replaceExtension(modulePath, opts) {
  51. const filename = stripExtension(modulePath, opts.stripExtensions);
  52. return _path.default.join(_path.default.dirname(modulePath), filename);
  53. }
  54. function matchesPattern(types, calleePath, pattern) {
  55. const node = calleePath.node;
  56. if (types.isMemberExpression(node)) {
  57. return calleePath.matchesPattern(pattern);
  58. }
  59. if (!types.isIdentifier(node) || pattern.includes('.')) {
  60. return false;
  61. }
  62. const name = pattern.split('.')[0];
  63. return node.name === name;
  64. }
  65. function mapPathString(nodePath, state) {
  66. if (!state.types.isStringLiteral(nodePath)) {
  67. return;
  68. }
  69. const sourcePath = nodePath.node.value;
  70. const currentFile = state.file.opts.filename;
  71. const modulePath = state.normalizedOpts.resolvePath(sourcePath, currentFile, state.opts);
  72. if (modulePath) {
  73. if (nodePath.node.pathResolved) {
  74. return;
  75. }
  76. nodePath.replaceWith(state.types.stringLiteral(modulePath));
  77. nodePath.node.pathResolved = true;
  78. }
  79. }
  80. function isImportCall(types, calleePath) {
  81. return types.isImport(calleePath.node.callee);
  82. }
  83. function escapeRegExp(string) {
  84. return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  85. }