Last active
June 27, 2019 20:05
-
-
Save dmitrizzle/1042037c268ae85cdbf6c5704a6580c2 to your computer and use it in GitHub Desktop.
Dynamically install polyfills for outdated browsers with Babel 7 and core-js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"presets": [ | |
[ | |
"@babel/preset-env", | |
{ | |
"useBuiltIns": "entry", | |
"corejs": 3 | |
} | |
] | |
], | |
"plugins": ["@babel/plugin-syntax-dynamic-import"] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Promise from "es6-promise"; | |
// es6-promise is a light-weight promise we require to resolve Promise below | |
Promise.polyfill(); | |
new Promise(resolve => { | |
// run compatibility tests | |
if ( | |
"startsWith" in String.prototype && | |
"endsWith" in String.prototype && | |
"includes" in Array.prototype && | |
"assign" in Object && | |
"keys" in Object | |
) | |
// new browsers that implement above features are exused | |
return resolve({ status: "skipped" }); | |
// older browsers get named import from `payload.js` | |
return import(/* webpackChunkName: "polyfill-payload" */ "./payload") | |
.then(() => resolve({ status: "polyfills installed" })) | |
.catch(() => resolve({ status: "failed payload import" })); | |
}).then(({ status }) => { | |
// polyfills are conditionally loaded | |
console.log(status); | |
/** | |
* | |
* MOUNT YOUR APP HERE | |
* | |
**/ | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"browserslist": "ie >= 10, chrome >=41, firefox >=35, samsung >= 5, opera >=37, android >=7", | |
"devDependencies": { | |
"@babel/core": "^7.4.5", | |
"@babel/plugin-syntax-dynamic-import": "^7.2.0", | |
"@babel/preset-env": "^7.4.5", | |
"babel-loader": "^8.0.6", | |
"core-js": "^3.1.4", | |
"es6-promise": "^4.2.8", | |
"formdata-polyfill": "^3.0.18", | |
"raf": "^3.4.1", | |
"regenerator-runtime": "^0.13.2", | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a list of all of the polyfill libraries which will be installed on older browsers: | |
import('core-js'); | |
import('raf/polyfill'); | |
import('formdata-polyfill'); | |
import('regenerator-runtime/runtime'); | |
import('event-propagation-path'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment