loaders.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.loaders = void 0;
  6. var _url = require("url");
  7. /* eslint-disable @typescript-eslint/no-require-imports */
  8. let importFresh;
  9. const loadJsSync = function loadJsSync(filepath) {
  10. if (importFresh === undefined) {
  11. importFresh = require('import-fresh');
  12. }
  13. const result = importFresh(filepath);
  14. return result;
  15. };
  16. const loadJs = async function loadJs(filepath) {
  17. try {
  18. const {
  19. href
  20. } = (0, _url.pathToFileURL)(filepath);
  21. return (await import(href)).default;
  22. } catch (error) {
  23. return loadJsSync(filepath, '');
  24. }
  25. };
  26. let parseJson;
  27. const loadJson = function loadJson(filepath, content) {
  28. if (parseJson === undefined) {
  29. parseJson = require('parse-json');
  30. }
  31. try {
  32. const result = parseJson(content);
  33. return result;
  34. } catch (error) {
  35. error.message = `JSON Error in ${filepath}:\n${error.message}`;
  36. throw error;
  37. }
  38. };
  39. let yaml;
  40. const loadYaml = function loadYaml(filepath, content) {
  41. if (yaml === undefined) {
  42. yaml = require('js-yaml');
  43. }
  44. try {
  45. const result = yaml.load(content);
  46. return result;
  47. } catch (error) {
  48. error.message = `YAML Error in ${filepath}:\n${error.message}`;
  49. throw error;
  50. }
  51. };
  52. const loaders = {
  53. loadJs,
  54. loadJsSync,
  55. loadJson,
  56. loadYaml
  57. };
  58. exports.loaders = loaders;
  59. //# sourceMappingURL=loaders.js.map