inject.js 1.9 KB

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