modern-module-resolution.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
  3. // See LICENSE in the project root for license information.
  4. Object.defineProperty(exports, "__esModule", { value: true });
  5. // This is a workaround for https://github.com/eslint/eslint/issues/3458
  6. //
  7. // To correct how ESLint searches for plugin packages, add this line to the top of your project's .eslintrc.js file:
  8. //
  9. // require("@rushstack/eslint-patch/modern-module-resolution");
  10. //
  11. const _patch_base_1 = require("./_patch-base");
  12. // error: "The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ''"
  13. const isInvalidImporterPath = (ex) => (ex === null || ex === void 0 ? void 0 : ex.code) === 'ERR_INVALID_ARG_VALUE';
  14. if (!_patch_base_1.configArrayFactory.__loadPluginPatched) {
  15. _patch_base_1.configArrayFactory.__loadPluginPatched = true;
  16. // eslint-disable-next-line @typescript-eslint/typedef
  17. const originalLoadPlugin = _patch_base_1.configArrayFactory.prototype._loadPlugin;
  18. if (_patch_base_1.ESLINT_MAJOR_VERSION === 6) {
  19. // ESLint 6.x
  20. // https://github.com/eslint/eslint/blob/9738f8cc864d769988ccf42bb70f524444df1349/lib/cli-engine/config-array-factory.js#L915
  21. _patch_base_1.configArrayFactory.prototype._loadPlugin = function (name, importerPath, importerName) {
  22. const originalResolve = _patch_base_1.ModuleResolver.resolve;
  23. try {
  24. _patch_base_1.ModuleResolver.resolve = function (moduleName, relativeToPath) {
  25. try {
  26. // resolve using importerPath instead of relativeToPath
  27. return originalResolve.call(this, moduleName, importerPath);
  28. }
  29. catch (e) {
  30. if ((0, _patch_base_1.isModuleResolutionError)(e) || isInvalidImporterPath(e)) {
  31. return originalResolve.call(this, moduleName, relativeToPath);
  32. }
  33. throw e;
  34. }
  35. };
  36. return originalLoadPlugin.apply(this, arguments);
  37. }
  38. finally {
  39. _patch_base_1.ModuleResolver.resolve = originalResolve;
  40. }
  41. };
  42. }
  43. else {
  44. // ESLint 7.x || 8.x
  45. // https://github.com/eslint/eslintrc/blob/242d569020dfe4f561e4503787b99ec016337457/lib/config-array-factory.js#L1023
  46. _patch_base_1.configArrayFactory.prototype._loadPlugin = function (name, ctx) {
  47. const originalResolve = _patch_base_1.ModuleResolver.resolve;
  48. try {
  49. _patch_base_1.ModuleResolver.resolve = function (moduleName, relativeToPath) {
  50. try {
  51. // resolve using ctx.filePath instead of relativeToPath
  52. return originalResolve.call(this, moduleName, ctx.filePath);
  53. }
  54. catch (e) {
  55. if ((0, _patch_base_1.isModuleResolutionError)(e) || isInvalidImporterPath(e)) {
  56. return originalResolve.call(this, moduleName, relativeToPath);
  57. }
  58. throw e;
  59. }
  60. };
  61. return originalLoadPlugin.apply(this, arguments);
  62. }
  63. finally {
  64. _patch_base_1.ModuleResolver.resolve = originalResolve;
  65. }
  66. };
  67. }
  68. }
  69. //# sourceMappingURL=modern-module-resolution.js.map