dynamic-import.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.buildDynamicImport = buildDynamicImport;
  6. var _core = require("@babel/core");
  7. {
  8. exports.getDynamicImportSource = function getDynamicImportSource(node) {
  9. const [source] = node.arguments;
  10. return _core.types.isStringLiteral(source) || _core.types.isTemplateLiteral(source) ? source : _core.template.expression.ast`\`\${${source}}\``;
  11. };
  12. }
  13. function buildDynamicImport(node, deferToThen, wrapWithPromise, builder) {
  14. const specifier = _core.types.isCallExpression(node) ? node.arguments[0] : node.source;
  15. if (_core.types.isStringLiteral(specifier) || _core.types.isTemplateLiteral(specifier) && specifier.quasis.length === 0) {
  16. if (deferToThen) {
  17. return _core.template.expression.ast`
  18. Promise.resolve().then(() => ${builder(specifier)})
  19. `;
  20. } else return builder(specifier);
  21. }
  22. const specifierToString = _core.types.isTemplateLiteral(specifier) ? _core.types.identifier("specifier") : _core.types.templateLiteral([_core.types.templateElement({
  23. raw: ""
  24. }), _core.types.templateElement({
  25. raw: ""
  26. })], [_core.types.identifier("specifier")]);
  27. if (deferToThen) {
  28. return _core.template.expression.ast`
  29. (specifier =>
  30. new Promise(r => r(${specifierToString}))
  31. .then(s => ${builder(_core.types.identifier("s"))})
  32. )(${specifier})
  33. `;
  34. } else if (wrapWithPromise) {
  35. return _core.template.expression.ast`
  36. (specifier =>
  37. new Promise(r => r(${builder(specifierToString)}))
  38. )(${specifier})
  39. `;
  40. } else {
  41. return _core.template.expression.ast`
  42. (specifier => ${builder(specifierToString)})(${specifier})
  43. `;
  44. }
  45. }
  46. //# sourceMappingURL=dynamic-import.js.map