resolve-tsconfig-DRWFAQ3v.mjs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { n as __toESM, t as require_binding } from "./binding-B4m_2rFW.mjs";
  2. import { a as bindingifySourcemap, n as normalizeBindingError } from "./error-Bj1xBdEY.mjs";
  3. //#region src/utils/minify.ts
  4. var import_binding = /* @__PURE__ */ __toESM(require_binding(), 1);
  5. /**
  6. * Minify asynchronously.
  7. *
  8. * Note: This function can be slower than {@linkcode minifySync} due to the overhead of spawning a thread.
  9. *
  10. * @category Utilities
  11. * @experimental
  12. */
  13. async function minify(filename, sourceText, options) {
  14. const inputMap = bindingifySourcemap(options?.inputMap);
  15. const result = await (0, import_binding.minify)(filename, sourceText, options);
  16. if (result.map && inputMap) result.map = {
  17. version: 3,
  18. ...(0, import_binding.collapseSourcemaps)([inputMap, bindingifySourcemap(result.map)])
  19. };
  20. return result;
  21. }
  22. /**
  23. * Minify synchronously.
  24. *
  25. * @category Utilities
  26. * @experimental
  27. */
  28. function minifySync(filename, sourceText, options) {
  29. const inputMap = bindingifySourcemap(options?.inputMap);
  30. const result = (0, import_binding.minifySync)(filename, sourceText, options);
  31. if (result.map && inputMap) result.map = {
  32. version: 3,
  33. ...(0, import_binding.collapseSourcemaps)([inputMap, bindingifySourcemap(result.map)])
  34. };
  35. return result;
  36. }
  37. //#endregion
  38. //#region src/utils/transform.ts
  39. const yarnPnp$1 = typeof process === "object" && !!process.versions?.pnp;
  40. function normalizeBindingWarning(warning) {
  41. if (warning.type === "JsError") return warning.field0;
  42. return {
  43. code: warning.field0.kind,
  44. message: warning.field0.message,
  45. id: warning.field0.id,
  46. exporter: warning.field0.exporter,
  47. loc: warning.field0.loc,
  48. pos: warning.field0.pos
  49. };
  50. }
  51. /**
  52. * Transpile a JavaScript or TypeScript into a target ECMAScript version, asynchronously.
  53. *
  54. * Note: This function can be slower than `transformSync` due to the overhead of spawning a thread.
  55. *
  56. * @param filename The name of the file being transformed. If this is a
  57. * relative path, consider setting the {@linkcode TransformOptions#cwd} option.
  58. * @param sourceText The source code to transform.
  59. * @param options The transform options including tsconfig and inputMap. See {@linkcode TransformOptions} for more information.
  60. * @param cache Optional tsconfig cache for reusing resolved tsconfig across multiple transforms.
  61. * Used when `options.tsconfig` is `true` or a path. For a path, construct the
  62. * cache with the same path.
  63. *
  64. * @returns a promise that resolves to an object containing the transformed code,
  65. * source maps, and any errors that occurred during parsing or transformation.
  66. *
  67. * @category Utilities
  68. * @experimental
  69. */
  70. async function transform(filename, sourceText, options, cache) {
  71. const result = await (0, import_binding.enhancedTransform)(filename, sourceText, options, cache, yarnPnp$1);
  72. return {
  73. ...result,
  74. errors: result.errors.map(normalizeBindingError),
  75. warnings: result.warnings.map(normalizeBindingWarning)
  76. };
  77. }
  78. /**
  79. * Transpile a JavaScript or TypeScript into a target ECMAScript version.
  80. *
  81. * @param filename The name of the file being transformed. If this is a
  82. * relative path, consider setting the {@linkcode TransformOptions#cwd} option.
  83. * @param sourceText The source code to transform.
  84. * @param options The transform options including tsconfig and inputMap. See {@linkcode TransformOptions} for more information.
  85. * @param cache Optional tsconfig cache for reusing resolved tsconfig across multiple transforms.
  86. * Used when `options.tsconfig` is `true` or a path. For a path, construct the
  87. * cache with the same path.
  88. *
  89. * @returns an object containing the transformed code, source maps, and any errors
  90. * that occurred during parsing or transformation.
  91. *
  92. * @category Utilities
  93. * @experimental
  94. */
  95. function transformSync(filename, sourceText, options, cache) {
  96. const result = (0, import_binding.enhancedTransformSync)(filename, sourceText, options, cache, yarnPnp$1);
  97. return {
  98. ...result,
  99. errors: result.errors.map(normalizeBindingError),
  100. warnings: result.warnings.map(normalizeBindingWarning)
  101. };
  102. }
  103. //#endregion
  104. //#region src/utils/resolve-tsconfig.ts
  105. const yarnPnp = typeof process === "object" && !!process.versions?.pnp;
  106. /**
  107. * Cache for tsconfig resolution to avoid redundant file system operations.
  108. *
  109. * The cache stores resolved tsconfig configurations keyed by their file paths.
  110. * When transforming multiple files in the same project, tsconfig lookups are
  111. * deduplicated, improving performance.
  112. * Pass a tsconfig path to use that configuration for every lookup. When the
  113. * path is omitted, the nearest tsconfig is discovered automatically.
  114. *
  115. * @category Utilities
  116. * @experimental
  117. */
  118. var TsconfigCache = class extends import_binding.TsconfigCache {
  119. constructor(pathToTsconfig) {
  120. super(yarnPnp, pathToTsconfig);
  121. }
  122. };
  123. /** @hidden This is only expected to be used by Vite */
  124. function resolveTsconfig(filename, cache) {
  125. return (0, import_binding.resolveTsconfig)(filename, cache, yarnPnp);
  126. }
  127. //#endregion
  128. export { minify as a, transformSync as i, resolveTsconfig as n, minifySync as o, transform as r, TsconfigCache as t };