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

ES7 async/await support #341

Closed
kennu opened this issue Jun 27, 2016 · 11 comments
Closed

ES7 async/await support #341

kennu opened this issue Jun 27, 2016 · 11 comments

Comments

@kennu
Copy link
Contributor

kennu commented Jun 27, 2016

Hi. I added async/await support to my Gatsby project by adding this to gatsby-node.js:

exports.modifyWebpackConfig = function(config, env) {
  if (env === 'build-javascript' || env === 'develop') {
    const previous = config.resolve().entry
    config._config.entry = []
    config.merge({
      entry: ['babel-polyfill'].concat(previous)
    })
  }
  return config
}

And by installing the polyfill package:

npm install --save babel-polyfill

I guess this could be some kind of default in Gatsby to avoid manually messing with the entry array order.

(Tweeted about it as @kfalck, I'm also @kennu on Twitter.)

@KyleAMathews
Copy link
Contributor

Hmmm... yeah normally you'd just import something like this at the top of your entry file but since Gatsby controls that you need to do this.

That being said, this is exactly why modifyWebpackConfig exists to make these sorts of customizations. I could be persuaded otherwise but is adding polyfills common enough that we want to add a new option to Gatsby just to support it? I think I'd just rather document this sort of modification really well.

Thoughts @kennu and others?

@kennu
Copy link
Contributor Author

kennu commented Jun 27, 2016

The ugly part seems to be in getting babel-polyfill to the beginning of the entry array. I couldn't figure out a way to do it without manually resetting config._config.entry. If that could be solved nicely, this would be a simple thing to document.

Also, I got some errors if I tweaked config._config.entry in other env stages than build-javascript and develop. Not quite sure why.

@KyleAMathews
Copy link
Contributor

Perhaps the "config as a function" option would be cleaner? https://www.npmjs.com/package/webpack-configurator

But yeah, more examples of doing stuff like this with webpack-configurator would be +++

@benstepp
Copy link
Contributor

benstepp commented Aug 4, 2016

So all you need is babel-polyfill at the beginning of the entry array? Maybe Array.prototype.unshift will help clean up the 'ugly'.

exports.modifyWebpackConfig = function(config, env) {
  if (env === 'build-javascript' || env === 'develop') {
    config._config.entry.unshift('babel-polyfill')
  }
  return config
}

@kennu
Copy link
Contributor Author

kennu commented Aug 4, 2016

I wasn't quite sure if you can just bypass config.resolve() and config.merge() and modify the variables directly.

@peonmodel
Copy link

hi, i am a little confused with how to add babel-polyfill to gatsby v 1

config.loader('js', {
			test: /\.(js|jsx)$/,
			exclude: /(node_modules|bower_components)/,
			loader: 'babel-polyfill',
		});

doesnt work

it gives

Loader <dir>/node_modules/babel-polyfill/lib/index.js?{"presets":["<dir>/node_modules/babel-preset-react/lib/index.js","<dir>/node_modules/babel-preset-es2015/lib/index.js","<dir>/node_modules/babel-preset-stage-1/lib/index.js","<dir>/node_modules/babel-preset-env/lib/index.js","<dir>/node_modules/babel-preset-stage-0/lib/index.js","<dir>/node_modules/babel-preset-react/lib/index.js"],"plugins":["<dir>/node_modules/gatsby/dist/utils/babel-plugin-extract-graphql.js","<dir>/node_modules/babel-plugin-add-module-exports/lib/index.js","<dir>/node_modules/react-hot-loader/babel.js","<dir>/node_modules/babel-plugin-transform-react-jsx-source/lib/index.js","<dir>/node_modules/babel-plugin-add-module-exports/lib/index.js","<dir>/node_modules/babel-plugin-transform-decorators-legacy/lib/index.js","<dir>/node_modules/babel-plugin-add-module-exports/lib/index.js","<dir>/node_modules/babel-plugin-transform-object-assign/lib/index.js"],"cacheDirectory":true} didn't return a function

@zhongqin0820
Copy link

do you solve this problem? @peonmodel

@peonmodel
Copy link

peonmodel commented Dec 18, 2017 via email

@robbyemmert
Copy link

Does Gatsby support modifying the babel settings? I've tried all of the above suggestions to no avail, and I can find no documentation on how to add support for generators or async/await.

Being able to modify babel settings is a dealbreaker for me personally. Are there any plans to allow people to customize these settings, and document them?

Thanks,

@pieh
Copy link
Contributor

pieh commented Apr 19, 2018

Yes, you can modify it in 3 ways:

@tompahoward
Copy link

Similar issue and solution at #3931 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants