loaders.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. /* eslint-disable @typescript-eslint/no-require-imports */
  3. var __importDefault = (this && this.__importDefault) || function (mod) {
  4. return (mod && mod.__esModule) ? mod : { "default": mod };
  5. };
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. exports.loadTs = exports.loadTsSync = exports.loadYaml = exports.loadJson = exports.loadJs = exports.loadJsSync = void 0;
  8. const fs_1 = require("fs");
  9. const promises_1 = require("fs/promises");
  10. const path_1 = __importDefault(require("path"));
  11. const url_1 = require("url");
  12. let importFresh;
  13. const loadJsSync = function loadJsSync(filepath) {
  14. if (importFresh === undefined) {
  15. importFresh = require('import-fresh');
  16. }
  17. return importFresh(filepath);
  18. };
  19. exports.loadJsSync = loadJsSync;
  20. const loadJs = async function loadJs(filepath) {
  21. try {
  22. const { href } = (0, url_1.pathToFileURL)(filepath);
  23. return (await import(href)).default;
  24. }
  25. catch (error) {
  26. try {
  27. return (0, exports.loadJsSync)(filepath, '');
  28. }
  29. catch (requireError) {
  30. if (requireError.code === 'ERR_REQUIRE_ESM' ||
  31. (requireError instanceof SyntaxError &&
  32. requireError
  33. .toString()
  34. .includes('Cannot use import statement outside a module'))) {
  35. throw error;
  36. }
  37. throw requireError;
  38. }
  39. }
  40. };
  41. exports.loadJs = loadJs;
  42. let parseJson;
  43. const loadJson = function loadJson(filepath, content) {
  44. if (parseJson === undefined) {
  45. parseJson = require('parse-json');
  46. }
  47. try {
  48. return parseJson(content);
  49. }
  50. catch (error) {
  51. error.message = `JSON Error in ${filepath}:\n${error.message}`;
  52. throw error;
  53. }
  54. };
  55. exports.loadJson = loadJson;
  56. let yaml;
  57. const loadYaml = function loadYaml(filepath, content) {
  58. if (yaml === undefined) {
  59. yaml = require('js-yaml');
  60. }
  61. try {
  62. return yaml.load(content);
  63. }
  64. catch (error) {
  65. error.message = `YAML Error in ${filepath}:\n${error.message}`;
  66. throw error;
  67. }
  68. };
  69. exports.loadYaml = loadYaml;
  70. let typescript;
  71. const loadTsSync = function loadTsSync(filepath, content) {
  72. /* istanbul ignore next -- @preserve */
  73. if (typescript === undefined) {
  74. typescript = require('typescript');
  75. }
  76. const compiledFilepath = `${filepath.slice(0, -2)}cjs`;
  77. try {
  78. const config = resolveTsConfig(path_1.default.dirname(filepath)) ?? {};
  79. config.compilerOptions = {
  80. ...config.compilerOptions,
  81. module: typescript.ModuleKind.NodeNext,
  82. moduleResolution: typescript.ModuleResolutionKind.NodeNext,
  83. target: typescript.ScriptTarget.ES2022,
  84. noEmit: false,
  85. };
  86. content = typescript.transpileModule(content, config).outputText;
  87. (0, fs_1.writeFileSync)(compiledFilepath, content);
  88. return (0, exports.loadJsSync)(compiledFilepath, content).default;
  89. }
  90. catch (error) {
  91. error.message = `TypeScript Error in ${filepath}:\n${error.message}`;
  92. throw error;
  93. }
  94. finally {
  95. if ((0, fs_1.existsSync)(compiledFilepath)) {
  96. (0, fs_1.rmSync)(compiledFilepath);
  97. }
  98. }
  99. };
  100. exports.loadTsSync = loadTsSync;
  101. const loadTs = async function loadTs(filepath, content) {
  102. if (typescript === undefined) {
  103. typescript = (await import('typescript')).default;
  104. }
  105. const compiledFilepath = `${filepath.slice(0, -2)}mjs`;
  106. let transpiledContent;
  107. try {
  108. try {
  109. const config = resolveTsConfig(path_1.default.dirname(filepath)) ?? {};
  110. config.compilerOptions = {
  111. ...config.compilerOptions,
  112. module: typescript.ModuleKind.ES2022,
  113. moduleResolution: typescript.ModuleResolutionKind.Bundler,
  114. target: typescript.ScriptTarget.ES2022,
  115. noEmit: false,
  116. };
  117. transpiledContent = typescript.transpileModule(content, config).outputText;
  118. await (0, promises_1.writeFile)(compiledFilepath, transpiledContent);
  119. }
  120. catch (error) {
  121. error.message = `TypeScript Error in ${filepath}:\n${error.message}`;
  122. throw error;
  123. }
  124. // eslint-disable-next-line @typescript-eslint/return-await
  125. return await (0, exports.loadJs)(compiledFilepath, transpiledContent);
  126. }
  127. finally {
  128. if ((0, fs_1.existsSync)(compiledFilepath)) {
  129. await (0, promises_1.rm)(compiledFilepath);
  130. }
  131. }
  132. };
  133. exports.loadTs = loadTs;
  134. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  135. function resolveTsConfig(directory) {
  136. const filePath = typescript.findConfigFile(directory, (fileName) => {
  137. return typescript.sys.fileExists(fileName);
  138. });
  139. if (filePath !== undefined) {
  140. const { config, error } = typescript.readConfigFile(filePath, (path) => typescript.sys.readFile(path));
  141. if (error) {
  142. throw new Error(`Error in ${filePath}: ${error.messageText.toString()}`);
  143. }
  144. return config;
  145. }
  146. return;
  147. }
  148. //# sourceMappingURL=loaders.js.map