You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
2.1 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getImportSource = getImportSource;
  6. exports.createDynamicImportTransform = createDynamicImportTransform;
  7. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
  8. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
  9. function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  10. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  11. function getImportSource(t, callNode) {
  12. var importArguments = callNode.arguments;
  13. var _importArguments = _slicedToArray(importArguments, 1),
  14. importPath = _importArguments[0];
  15. var isString = t.isStringLiteral(importPath) || t.isTemplateLiteral(importPath);
  16. if (isString) {
  17. t.removeComments(importPath);
  18. return importPath;
  19. }
  20. return t.templateLiteral([t.templateElement({
  21. raw: '',
  22. cooked: ''
  23. }), t.templateElement({
  24. raw: '',
  25. cooked: ''
  26. }, true)], importArguments);
  27. }
  28. function createDynamicImportTransform(_ref) {
  29. var template = _ref.template,
  30. t = _ref.types;
  31. var buildImport = template('Promise.resolve().then(() => MODULE)');
  32. return function (context, path) {
  33. var requireCall = t.callExpression(t.identifier('require'), [getImportSource(t, path.parent)]);
  34. var _context$opts$noInter = context.opts.noInterop,
  35. noInterop = _context$opts$noInter === undefined ? false : _context$opts$noInter;
  36. var MODULE = noInterop === true ? requireCall : t.callExpression(context.addHelper('interopRequireWildcard'), [requireCall]);
  37. var newImport = buildImport({
  38. MODULE: MODULE
  39. });
  40. path.parentPath.replaceWith(newImport);
  41. };
  42. }