Skip to content

Commit 23ca11c

Browse files
authored
Merge pull request #9 from willstocks-tech/develop
v0.0.4 release!
2 parents 1e8be28 + 783b573 commit 23ca11c

File tree

4 files changed

+141
-49
lines changed

4 files changed

+141
-49
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
A little script that allows you to only polyfill a feature when absolutely necessary - no wasted requests on browsers that have native support! 😆🤓
1212

13-
This script is ~3.51KB **un**minified _(1.29KB **un**minified and gzipped)_ or ~1.8KB minified _(891B minified and gzipped)_, so it's _fairly_ light. :smile:
13+
This script is ~2.64KB **un**minified _(1KB **un**minified and gzipped)_ or ~1.55KB minified _(762B minified and gzipped)_, so it's _fairly_ light. :smile:
1414

1515
## Getting Started
1616

@@ -23,28 +23,29 @@ Make sure you know what features your script is reliant on and polyfill those no
2323
## Deployment
2424

2525
### Loading from CDN:
26-
1. Add a <script></script> tag linking to this script
26+
1. Add a `<script></script>` tag linking to this script
2727
1. Example: `<script src='https://cdn.jsdelivr.net/gh/willstocks-tech/dynamically-polyfill-features-for-a-script@master/dynamicpolyfill.min.js'></script>`
2828
2. Add an `onLoad` attribute to the tag calling the `dynamicPolyfill()` function and passing your parameters
2929
1. Note: the first parameter is the feature polyfills you want to pass. This is expected as an array.
30-
1. Note: the second paramter is the URL of the script you want to use. This is expected as a string.
30+
1. Note: the second paramter is the URL of the script you want to use. This is expected as a string, but can be blank (`''`) or `null` if you're not loading a third party script.
3131
1. Note: the third parameter is the function that you would run once the script has loaded. This is expected as a string.
32-
1. Note: the 4th parameter is a `true`/`false` flag. If using a CDN you need to set the flag to `false`.
32+
1. Note: the 4th parameter has now been deprecated.
3333

3434
#### Full CDN example script tag:
3535
```
3636
<script
3737
type='text/javascript'
3838
src='https://cdn.jsdelivr.net/gh/willstocks-tech/dynamically-polyfill-features-for-a-script@master/dynamicpolyfill.min.js'
39-
onload='dynamicPolyfill( ["IntersectionObserver"], 'https://cdn.jsdelivr.net/npm/quicklink@1.0.0/dist/quicklink.umd.js', 'quicklink();', false )'>
39+
onload='dynamicPolyfill( ["IntersectionObserver", "Object.assign"], 'https://cdn.jsdelivr.net/npm/quicklink@1.0.0/dist/quicklink.umd.js', 'quicklink();')'>
4040
</script>
4141
```
4242

43-
Note: You need to ensure that before you call the `dynamicPolyfill()` function that the actual script itself has loaded. If you're going to host the script yourself (rather than calling out to a CDN), make sure you include the script code first, then call the function. You can do this in the same manner as above, but replace the CDN URL with the path to your own JS file!
43+
Note: You need to ensure that before you call the `dynamicPolyfill()` function that the actual script itself has loaded. If you're going to host the script yourself (rather than calling out to a CDN), make sure you include the script code first, then call the function. You can do this in the same manner as above, but replace the CDN URL with the path to your own JS file! An example of this would be: `dynamicPolyfill(["IntersectionObserver", "Object.assign"], 'https://cdn.jsdelivr.net/npm/quicklink@1.0.0/dist/quicklink.umd.js', 'quicklink();');`.
4444

4545
## Built With
4646

4747
* Vanilla Javascript - no framework dependencies!
48+
* [Polyfill.io](https://github.com/Financial-Times/polyfill-library) - for the actual polyfills!
4849

4950
## Versioning
5051

@@ -67,4 +68,5 @@ This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md
6768
## Acknowledgments
6869

6970
* [PurpleBooth](https://gist.github.com/PurpleBooth) for this awesome README template!
71+
* [Polyfill.io](https://github.com/Financial-Times/polyfill-library) for the awesome polyfill service!
7072

dynamicpolyfill-consolemessages.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//This file includes a bunch of console.log()/console.error()/console.warn() messages. Aides with troubleshooting, but is also a little fun if someone happens to come across them. They're not necessary at all tho'!
2+
function dynamicPolyfill (features, scriptURL, initFunction) {
3+
var polyfillFeatures = features;
4+
var scriptToPolyfill = scriptURL;
5+
var functionToRunonLoad = initFunction;
6+
return pageLoaded(polyfillFeatures, scriptToPolyfill, functionToRunonLoad);
7+
}
8+
9+
function pageLoaded(polyfillFeatures, scriptToPolyfill, functionToRunonLoad) {
10+
Promise.all([checkNativeSupport(polyfillFeatures)])
11+
.then(
12+
function() {
13+
loadMyScript(scriptToPolyfill)
14+
.then(
15+
console.log("As the script is ready, let's initialise it...");
16+
initialiseMyScript(functionToRunonLoad)
17+
).catch(function(error){return error})
18+
}
19+
).catch(function(error){return error})
20+
,function () {
21+
console.error("There was an issue polyfilling",mayneedpolyfill," which means that I can't preload future pages for you. Sorry! :(");
22+
console.warn("If you want this to work, I'd recommend upgrading to a browser that supports",mayneedpolyfill,"natively. You can find out which browsers do by visting: https://caniuse.com/");
23+
}
24+
}
25+
26+
function checkNativeSupport(tocheck) {
27+
var num = tocheck.length; //cache value out of the for loop
28+
var polyfillNeeded = [];
29+
for (var i = 0; i < num; i++) {
30+
var pol = tocheck[i];
31+
var splitChars = '.';
32+
var split = pol.split(splitChars);
33+
var firstWord = window[split[0]];
34+
var lastWord = new Object(split[split.length - 1]);
35+
if (typeof (window.pol) !== 'undefined' || pol in window || (pol.indexOf(splitChars) >= 1 && lastWord in firstWord) || pol in this) {
36+
console.log(pol,'has native support');
37+
} else {
38+
console.warn("Ahhh, your browser doesn't support",pol,". I'm gonna have to polyfill it so stuff works. Hang on one sec!");
39+
polyfillNeeded.push(pol);
40+
}
41+
}
42+
if (polyfillNeeded.length > 0) {
43+
return loadPolyfill(polyfillNeeded);
44+
}
45+
}
46+
47+
function loadMyScript(url) {
48+
if(url !== null && url !== '') {
49+
return new Promise(
50+
function(resolve, reject) {
51+
var thescript = document.createElement('script');
52+
thescript.src = encodeURI(url);
53+
document.getElementsByTagName('body')[0].appendChild(thescript);
54+
console.log('Loading ',thescript.src,'!');
55+
thescript.onerror = function(response) {
56+
console.error ('Loading the script failed!');
57+
return reject("Loading the script failed!", response);
58+
}
59+
thescript.onload = function() {
60+
console.log("Script setup and ready to load!");
61+
return resolve("Script setup and ready to load!");
62+
}
63+
}
64+
)
65+
} else {
66+
return new Promise(
67+
function(resolve, reject) {
68+
console.log("No script to load!");
69+
return resolve ("No script to load");
70+
}
71+
)
72+
}
73+
}
74+
75+
function initialiseMyScript(functionToRunonLoad) {
76+
console.log("The following script will now be initialised:", functionToRunonLoad);
77+
return new Function(functionToRunonLoad);
78+
}
79+
80+
function loadPolyfill(url) {
81+
return new Promise(
82+
function(resolve, reject) {
83+
var polyfill = document.createElement('script');
84+
polyfill.src = ('https://polyfill.io/v3/polyfill.min.js?features='+encodeURIComponent(url));
85+
document.getElementsByTagName('body')[0].appendChild(polyfill);
86+
console.log('Grabbing',url,'polyfill from: ', polyfill.src);
87+
polyfill.onerror = function(response) {
88+
console.error ('Loading the polyfill(s) failed!');
89+
return reject("Loading the polyfill(s) failed!", response);
90+
}
91+
polyfill.onload = function() {
92+
console.log("Polyfill(s) loaded!");
93+
return resolve("Polyfill(s) loaded!");
94+
}
95+
}
96+
)
97+
}

dynamicpolyfill.js

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
1-
var staticScript; //CDN usage passes a "false". Set to true if you want direct control via the below variables.
2-
3-
function dynamicPolyfill (features, scriptURL, initFunction, staticScript) {
4-
if(staticScript == false) {
5-
var polyfillFeatures = features;
6-
var scriptToPolyfill = scriptURL;
7-
function initialiseMyScript() {initFunction}
8-
return pageLoaded(polyfillFeatures, scriptToPolyfill);
9-
} else {
10-
var polyfillFeatures = ["Example.Feature1","ExampleFeature2"];
11-
var scriptToPolyfill = "https://cdn.example.com/packagename/version/scriptname.min.js";
12-
function initialiseMyScript() {RENAMETOYOURFUNCTION();}
13-
window.onload = pageLoaded(polyfillFeatures, scriptToPolyfill);
14-
}
1+
function dynamicPolyfill (features, scriptURL, initFunction) {
2+
var polyfillFeatures = features;
3+
var scriptToPolyfill = scriptURL;
4+
var functionToRunonLoad = initFunction;
5+
return pageLoaded(polyfillFeatures, scriptToPolyfill, functionToRunonLoad);
156
}
167

17-
function pageLoaded(polyfillFeatures, scriptToPolyfill) {
8+
function pageLoaded(polyfillFeatures, scriptToPolyfill, functionToRunonLoad) {
189
Promise.all([checkNativeSupport(polyfillFeatures)])
1910
.then(
2011
function() {
2112
loadMyScript(scriptToPolyfill)
2213
.then(
23-
function() {
24-
console.log("As the script is ready, let's initialise it...");
25-
initialiseMyScript();
26-
console.log("Et voila! The script is running - wooooo!");
27-
}
14+
initialiseMyScript(functionToRunonLoad)
2815
).catch(function(error){return error})
2916
}
3017
).catch(function(error){return error})
@@ -43,11 +30,10 @@ function checkNativeSupport(tocheck) {
4330
var split = pol.split(splitChars);
4431
var firstWord = window[split[0]];
4532
var lastWord = new Object(split[split.length - 1]);
46-
if (typeof (window.pol) !== 'undefined' || pol in window || (pol.indexOf(splitChars) >= 1 && lastWord in firstWord)) {
33+
if (typeof (window.pol) !== 'undefined' || pol in window || (pol.indexOf(splitChars) >= 1 && lastWord in firstWord) || pol in this) {
4734
console.log(pol,'has native support');
4835
} else {
49-
console.warn("Ahhh, your browser doesn't support",tocheck[i],". I'm gonna have to polyfill it so stuff works. Hang on one sec!");
50-
polyfillNeeded.push(tocheck[i]);
36+
polyfillNeeded.push(pol);
5137
}
5238
}
5339
if (polyfillNeeded.length > 0) {
@@ -56,22 +42,32 @@ function checkNativeSupport(tocheck) {
5642
}
5743

5844
function loadMyScript(url) {
59-
return new Promise(
60-
function(resolve, reject) {
61-
var thescript = document.createElement('script');
62-
thescript.src = encodeURI(url);
63-
document.getElementsByTagName('body')[0].appendChild(thescript);
64-
console.log('Loading ',thescript.src,'!');
65-
thescript.onerror = function(response) {
66-
console.error ('Loading the script failed!');
67-
return reject("Loading the script failed!", response);
68-
}
69-
thescript.onload = function() {
70-
console.log("Script setup and ready to load!");
71-
return resolve("Script setup and ready to load!");
72-
}
73-
}
74-
)
45+
if(url !== null && url !== '') {
46+
return new Promise(
47+
function(resolve, reject) {
48+
var thescript = document.createElement('script');
49+
thescript.src = encodeURI(url);
50+
document.getElementsByTagName('body')[0].appendChild(thescript);
51+
thescript.onerror = function(response) {
52+
return reject("Loading the script failed!", response);
53+
}
54+
thescript.onload = function() {
55+
return resolve("Script setup and ready to load!");
56+
}
57+
}
58+
)
59+
} else {
60+
return new Promise(
61+
function(resolve, reject) {
62+
return resolve ("No script to load");
63+
}
64+
)
65+
}
66+
}
67+
68+
function initialiseMyScript(functionToRunonLoad) {
69+
console.log("The following script will now be initialised:", functionToRunonLoad);
70+
return new Function(functionToRunonLoad);
7571
}
7672

7773
function loadPolyfill(url) {
@@ -80,13 +76,10 @@ function loadPolyfill(url) {
8076
var polyfill = document.createElement('script');
8177
polyfill.src = ('https://polyfill.io/v3/polyfill.min.js?features='+encodeURIComponent(url));
8278
document.getElementsByTagName('body')[0].appendChild(polyfill);
83-
console.log('Grabbing',url,'polyfill from: ', polyfill.src);
8479
polyfill.onerror = function(response) {
85-
console.error ('Loading the polyfill(s) failed!');
8680
return reject("Loading the polyfill(s) failed!", response);
8781
}
8882
polyfill.onload = function() {
89-
console.log("Polyfill(s) loaded!");
9083
return resolve("Polyfill(s) loaded!");
9184
}
9285
}

dynamicpolyfill.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)