Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intl.js polyfill/shim with webpack?

I'm using React-Intl with webpack and I need the Intl shim to support Safari and IE, but I don't want to load it for browsers that already support the Intl spec.

The polyfill is pretty huge (900kb), how can I ensure it only gets loaded in browsers that don't support it already?

like image 372
Jamund Ferguson Avatar asked Apr 22 '15 17:04

Jamund Ferguson


People also ask

Does Babel have polyfill?

Babel includes a polyfill that includes a custom regenerator runtime and core-js. This will emulate a full ES2015+ environment (no < Stage 4 proposals) and is intended to be used in an application rather than a library/tool. (this polyfill is automatically loaded when using babel-node ).

Do you need Babel polyfill?

So long story short, just using babel is not enough for your application to work because all the latest Javascript features are not supported in all browsers. So to fix this problem, we need to use a polyfill.

What is the use of Babel polyfill?

Babel Polyfill adds support to the web browsers for features, which are not available. Babel compiles the code from recent ecma version to the one, which we want. It changes the syntax as per the preset, but cannot do anything for the objects or methods used.


1 Answers

An simple (non-webpack) approach would be to do something like this in your HTML:

<script>
 if (!window.Intl) { document.write('<script src="/js/Intl.min.js"><\/script>'); }
</script>

Though document.write is not considered a best practice, this would provide a blocking approach to solving the problem, resulting in considerably less code in your the application. This solution does not use webpack, but may be a suitable solution for many people in this situation.

like image 81
Jamund Ferguson Avatar answered Oct 17 '22 11:10

Jamund Ferguson