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

Adding a polyfill for ES6 Array.from() #4014

Closed
lloydh opened this issue Feb 13, 2018 · 18 comments
Closed

Adding a polyfill for ES6 Array.from() #4014

lloydh opened this issue Feb 13, 2018 · 18 comments

Comments

@lloydh
Copy link
Contributor

lloydh commented Feb 13, 2018

I need IE11 compatibility for a website but am getting errors from ES6 Array.from() used by emotion's createEmotion constructor.

Given gatsby supports the same browser targets as React I assumed the default config would handle this however I'm now trying to polyfill Array.from() specifically.

Looking for the correct way to polyfill gatsby I found 2177 and 3314 but not a clearly recommended approach.

I've tried importing core-js and polyfilling in an onClientEntry hook within gatsby-browser.js but it seems that createEmotion might be executing before the polyfill.

Help much appreciated.

Environment

Gatsby version: 1.9.192
Node.js version: 9.5.0
Operating System: OS X 10.13.3

@m-allanson
Copy link
Contributor

This isn't a fix, but for context it looks like you're running into #3780.

@lloydh
Copy link
Contributor Author

lloydh commented Feb 14, 2018

@m-allanson yup, looks like that's the problem.

It sounds like ES6 transpilling/minification is being looked at for gatsby 2 but in the meantime is polyfilling still my best bet, and if so where's the best place to do it?

@m-allanson
Copy link
Contributor

@lloydh I think just adding import 'babel-polyfill to the top of your gatsby-browser.js should do it. Have a look at this thread / comment for more info.

Feel free to drop another note here if that doesn't work out.

@lloydh
Copy link
Contributor Author

lloydh commented Feb 14, 2018

Thanks but I get the same result from babel-polyfill as with core-js — IE11 throws on emotion's ES6 methods before any code in my gatsby-browser.js runs.

This is gatsby-plugin-emotion's gatsby-browser.js which it looks like is getting first dibs:

/* globals window */
import { hydrate } from "emotion"

exports.onClientEntry = () => {
  if (
    typeof window !== `undefined` &&
    typeof window.__EMOTION_CRITICAL_CSS_IDS__ !== `undefined`
  ) {
    hydrate(window.__EMOTION_CRITICAL_CSS_IDS__)
  }
}

Polyfills need to be ahead of this.

@lloydh
Copy link
Contributor Author

lloydh commented Feb 14, 2018

I solved my polyfill problem by creating gatsby-plugin-core-js.

Adding this as the first plugin in gatsby-config.js ensures the polyfill is executed before other plugin code utilising unsupported ES features.

@fk
Copy link
Contributor

fk commented Feb 14, 2018

Nice @lloydh! Would love to see your plugin added to the list of Community Plugins!

@lloydh
Copy link
Contributor Author

lloydh commented Feb 15, 2018

@fk that was the first place I looked so I'm happy if it can save other users from headaches!

Honestly I still don't know if this is the most idiomatic way of polyfilling gatsby but gatsyby-plugin-core-js allowed me to ship!

@lloydh lloydh closed this as completed Feb 15, 2018
@grgur
Copy link

grgur commented Mar 5, 2018

I don't recommend bloating your codebase with the entire core-js. If all you need is Array.from then add core-js as a dependency (npm i core-js) and require parts you need.

One way of doing that would be from gatsby-browser.js

exports.onClientEntry = () => {
  require('core-js/fn/array/from');
};

@lloydh
Copy link
Contributor Author

lloydh commented Mar 5, 2018

@grgur thanks for the recommendation but using the .onClientEntry hook was my first attempt.

The problem is that gatsby-plugin-emotion is using Array.from in .onClientEntry and plugins run before anything in gatsby-browser.js can.

I agree about selectively importing features to polyfill but a better goal is having this all taken care of at build based on the browser targets.

@KyleAMathews
Copy link
Contributor

FWIW, Gatsby v2 will take advantage of Babel 7's new auto-polyfilling which will handle problems like this more elegantly https://github.com/gatsbyjs/gatsby/blob/v2/docs/docs/browser-support.md#polyfills

@robwierzbowski
Copy link

@KyleAMathews Somewhat of a hijack of this issue -- I don't want to add a new issue without some new information.

Will it be possible to turn off auto-polyfilling for all browser code in 2.x? We use Financial Time's Polyfill.io to only serve polyfills to browsers that need them. It's a good performance strategy for smaller file sizes, less JS processing, and more native code usage.

@KyleAMathews
Copy link
Contributor

@robwierzbowski yeah, we could add that — though the new method might be faster for y'all. Especially if we can get two JS builds working — one targeted at newer browsers and one targeted at older browsers.

@robwierzbowski
Copy link

In my experience serving minimal polyfill code per UA string is fastest, especially when serving a wide range of browser capabilities. No code to the browsers that don’t need it.

Allowing a user to choose their polyfill strategy (useBuiltIns, runtime, or external) is probably the safest option for wide developer happiness, as each comes with their own considerations for perf, complexity, and stability. It’s my experience these can be real sticking points for a team.

@robwierzbowski
Copy link

Thanks for responding so quickly!

@vai0
Copy link

vai0 commented Nov 19, 2018

After updating to v2, imported 3rd-party modules i.e., react-slick that rely on Array.from don't seem to be polyfilled 😞. Does gatsby support polyfilling 3rd party modules? I'm running v2.0.52.

@Dogtiti
Copy link

Dogtiti commented Dec 21, 2018

@vai0 same issue, I use the react-slick too. And it doesn't support IE9, so have you found some ways to solve it?

@vai0
Copy link

vai0 commented Jan 7, 2019

@Dogtiti I ended up using this: https://www.gatsbyjs.org/packages/gatsby-plugin-polyfill-io/

@Dogtiti
Copy link

Dogtiti commented Jan 8, 2019

@vai0 ok, thanks I'll try

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