Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require babel-polyfill and babel-core statements are preceded by asycn runtime code. plugin[babel-plugin-transform-async-to-generator] #4487

Closed
devarsh opened this issue Sep 9, 2016 · 2 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@devarsh
Copy link

devarsh commented Sep 9, 2016

Input Code

require("babel-core/register")
require("babel-polyfill")

var fetch = require('isomorphic-fetch')

function fetchReddit(topic) {
  const redditUrl= `https://www.reddit.com/r/${topic}.json`
  return new Promise((resolve,reject) => {
    fetch(redditUrl).then(response => response.json()).then(result => resolve(result.data.children))
  })
}

async function read(topic) {
  var json = await fetchReddit(topic)
  console.log(json)
}

read('home')

Babel Configuration (.bablerc, package.json, cli command)

.babelrc

{
  "presets": ["es2015", "es2016", "es2017", "stage-2"],
}

package.json

devDependencies": {
   "babel": "^6.5.2",
    "babel-cli": "^6.11.4",
    "babel-core": "^6.10.4",
    "babel-eslint": "^6.1.0",
    "babel-plugin-transform-async-to-generator": "^6.8.0",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-es2016": "^6.11.3",
    "babel-preset-es2017": "^6.14.0",
    "babel-preset-stage-0": "^6.5.0",
    "babel-register": "^6.11.6",
    "isomorphic-fetch": "^2.2.1",
}

Current Behavior

The read function preceeds the require statement, which will result in undefined regeneratorRuntime

"use strict";

var read = function () {
  var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(topic) {
    var json;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return fetchReddit(topic);

          case 2:
            json = _context.sent;

            console.log(json);

          case 4:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function read(_x) {
    return _ref.apply(this, arguments);
  };
}();

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }

require("babel-core/register");
require("babel-polyfill");

var fetch = require('isomorphic-fetch');

function fetchReddit(topic) {
  var redditUrl = "https://www.reddit.com/r/" + topic + ".json";
  return new Promise(function (resolve, reject) {
    fetch(redditUrl).then(function (response) {
      return response.json();
    }).then(function (result) {
      return resolve(result.data.children);
    });
  });
}

read('home');

Expected Behavior

"use strict";


require("babel-core/register");
require("babel-polyfill");
/* everthing else after the require statement */
var read = function () {
  var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(topic) {
    var json;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return fetchReddit(topic);

          case 2:
            json = _context.sent;

            console.log(json);

          case 4:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, this);
  }));

  return function read(_x) {
    return _ref.apply(this, arguments);
  };
}();

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }

var fetch = require('isomorphic-fetch');

function fetchReddit(topic) {
  var redditUrl = "https://www.reddit.com/r/" + topic + ".json";
  return new Promise(function (resolve, reject) {
    fetch(redditUrl).then(function (response) {
      return response.json();
    }).then(function (result) {
      return resolve(result.data.children);
    });
  });
}

read('home');
software version
Babel 6.5.2
node 4.3.0
npm 3.7.2
Operating System OS X EL Capitan
@devarsh devarsh changed the title require babel-polyfill and babel-core statements are preceded by asycn runtime code babel-plugin-transform-async-to-generator: require babel-polyfill and babel-core statements are preceded by asycn runtime code Sep 9, 2016
@devarsh devarsh changed the title babel-plugin-transform-async-to-generator: require babel-polyfill and babel-core statements are preceded by asycn runtime code plugin: babel-plugin-transform-async-to-generator require babel-polyfill and babel-core statements are preceded by asycn runtime code Sep 9, 2016
@devarsh devarsh changed the title plugin: babel-plugin-transform-async-to-generator require babel-polyfill and babel-core statements are preceded by asycn runtime code plugin: babel-plugin-transform-async-to-generator, require babel-polyfill and babel-core statements are preceded by asycn runtime code Sep 9, 2016
@devarsh devarsh changed the title plugin: babel-plugin-transform-async-to-generator, require babel-polyfill and babel-core statements are preceded by asycn runtime code require babel-polyfill and babel-core statements are preceded by asycn runtime code [plugin]babel-plugin-transform-async-to-generator, Sep 9, 2016
@devarsh devarsh changed the title require babel-polyfill and babel-core statements are preceded by asycn runtime code [plugin]babel-plugin-transform-async-to-generator, require babel-polyfill and babel-core statements are preceded by asycn runtime code. plugin[babel-plugin-transform-async-to-generator] Sep 9, 2016
@loganfsmyth
Copy link
Member

The issue here is that you're using a function declaration, which because of hoisting is technically supposed to be at the top of the file. Babel doesn't know anything about require statements, so it has no way of knowing it is moving that logic above the code that it depends on.

Two options

  1. Make a separate entry point, e.g.

    require("babel-core/register")
    require("babel-polyfill")
    
    require('./app') // load the app explicitly after the polyfill
    
  2. Pass them when you start node, e.g.

    node -r babel-polyfill -r babel-core/register app.js

@devarsh devarsh closed this as completed Sep 9, 2016
@devarsh
Copy link
Author

devarsh commented Sep 9, 2016

@loganfsmyth thanks 👍

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 6, 2018
@lock lock bot locked as resolved and limited conversation to collaborators May 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

2 participants