import-helper.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. var _url = _interopRequireDefault(require("url"));
  3. function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
  4. async function supportsDynamicImport() {
  5. try {
  6. // imports are cached.
  7. // no need to worry about perf here.
  8. // Don't remove .js: extension must be included for ESM imports!
  9. await import('./dummy-file.js');
  10. return true;
  11. } catch (e) {
  12. return false;
  13. }
  14. }
  15. /**
  16. * Imports a JSON, CommonJS or ESM module
  17. * based on feature detection.
  18. *
  19. * @param modulePath path to the module to import
  20. * @returns {Promise<unknown>} the imported module.
  21. */
  22. async function importModule(modulePath) {
  23. // JSON modules are still behind a flag. Fallback to require for now.
  24. // https://nodejs.org/api/esm.html#json-modules
  25. if (_url.default.pathToFileURL && !modulePath.endsWith('.json') && (await supportsDynamicImport())) {
  26. // 'import' expects a URL. (https://github.com/sequelize/cli/issues/994)
  27. return import(_url.default.pathToFileURL(modulePath));
  28. }
  29. // mimics what `import()` would return for
  30. // cjs modules.
  31. return {
  32. default: require(modulePath)
  33. };
  34. }
  35. module.exports = {
  36. supportsDynamicImport,
  37. importModule
  38. };