inject.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. Object.defineProperty(exports, "__esModule", {
  2. value: true
  3. });
  4. exports['default'] = injectDynamicImport;
  5. /* eslint-disable no-underscore-dangle */
  6. var DynamicImportKey = exports.DynamicImportKey = 'Import';
  7. function injectDynamicImport(acorn) {
  8. var tt = acorn.tokTypes;
  9. // NOTE: This allows `yield import()` to parse correctly.
  10. tt._import.startsExpr = true;
  11. function parseDynamicImport() {
  12. var node = this.startNode();
  13. this.next();
  14. if (this.type !== tt.parenL) {
  15. this.unexpected();
  16. }
  17. return this.finishNode(node, DynamicImportKey);
  18. }
  19. function peekNext() {
  20. return this.input[this.pos];
  21. }
  22. // eslint-disable-next-line no-param-reassign
  23. acorn.plugins.dynamicImport = function () {
  24. function dynamicImportPlugin(instance) {
  25. instance.extend('parseStatement', function (nextMethod) {
  26. return function () {
  27. function parseStatement() {
  28. var node = this.startNode();
  29. if (this.type === tt._import) {
  30. var nextToken = peekNext.call(this);
  31. if (nextToken === tt.parenL.label) {
  32. var expr = this.parseExpression();
  33. return this.parseExpressionStatement(node, expr);
  34. }
  35. }
  36. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  37. args[_key] = arguments[_key];
  38. }
  39. return nextMethod.apply(this, args);
  40. }
  41. return parseStatement;
  42. }();
  43. });
  44. instance.extend('parseExprAtom', function (nextMethod) {
  45. return function () {
  46. function parseExprAtom(refDestructuringErrors) {
  47. if (this.type === tt._import) {
  48. return parseDynamicImport.call(this);
  49. }
  50. return nextMethod.call(this, refDestructuringErrors);
  51. }
  52. return parseExprAtom;
  53. }();
  54. });
  55. }
  56. return dynamicImportPlugin;
  57. }();
  58. return acorn;
  59. }