Skip to content

Commit e25be00

Browse files
author
Antony Jones
committed
initial commit
0 parents  commit e25be00

File tree

8 files changed

+708
-0
lines changed

8 files changed

+708
-0
lines changed

.circleci/config.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
version: 2
2+
jobs:
3+
build:
4+
docker:
5+
- image: circleci/node:10
6+
7+
working_directory: ~/workspace
8+
9+
steps:
10+
- checkout
11+
12+
- restore_cache:
13+
keys:
14+
- v1-dependencies-{{ checksum "package.json" }}
15+
16+
- run: npm install
17+
18+
- save_cache:
19+
paths:
20+
- node_modules
21+
key: v1-dependencies-{{ checksum "package.json" }}
22+
23+
- store_test_results:
24+
path: reports/junit
25+
26+
publish:
27+
docker:
28+
- image: circleci/node:10
29+
30+
steps:
31+
- add_ssh_keys
32+
- checkout
33+
34+
- run:
35+
name: Authorize NPM
36+
command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc
37+
38+
- restore_cache:
39+
keys:
40+
- v1-dependencies-{{ checksum "package.json" }}
41+
42+
- run: npm install
43+
44+
- save_cache:
45+
paths:
46+
- node_modules
47+
key: v1-dependencies-{{ checksum "package.json" }}
48+
49+
- run:
50+
name: Publish to NPM
51+
command: npm publish
52+
53+
workflows:
54+
version: 2
55+
main:
56+
jobs:
57+
- build:
58+
context: org-global
59+
filters:
60+
branches:
61+
only: master
62+
tags:
63+
only: /v.*/
64+
- publish:
65+
context: org-global
66+
requires:
67+
- build
68+
filters:
69+
branches:
70+
ignore: /.*/
71+
tags:
72+
only: /v.*/%

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.MD

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p align="center">
2+
<img width="186" height="90" src="https://user-images.githubusercontent.com/218949/44782765-377e7c80-ab80-11e8-9dd8-fce0e37c235b.png" alt="Beyonk" />
3+
</p>
4+
5+
## Async Script Loader
6+
7+
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com) [![CircleCI](https://circleci.com/gh/beyonk-adventures/async-script-loader.svg?style=shield)](https://circleci.com/gh/beyonk-adventures/async-script-loader)
8+
9+
Allows asynchronous loading of scripts in a Single Page Application (or anything else, in fact):
10+
11+
* Using a test to ensure that the code is only loaded once
12+
* Running a callback once the script is loaded
13+
* Running a callback if the script is already loaded
14+
* Not blocking the main thread
15+
16+
### Reasoning
17+
18+
Having integrated a multitude of third-party SDKs from large, well known providers, I've come to the conclusion that not having a standard interface turns the whole thing into a minefield of callbacks, timers, random library-specific loader modules, and global objects on the window, resulting in XSS risks and all sort of other undesirable behaviour. This module aims to provide a standard way of loading third-party dependencies.
19+
20+
### Usage
21+
22+
You can use the module like so, for a library loaded from example.com, which, when loaded, adds an attribute called PROVIDER to the global window object.
23+
24+
```js
25+
<script>
26+
import loader from '@beyonk/async-script-loader'
27+
28+
const url = '//example.com/sdk/1.0.0/lib.js'
29+
30+
function test () {
31+
return !!window.PROVIDER
32+
}
33+
34+
function callback () {
35+
window.PROVIDER.someFunction()
36+
}
37+
38+
loader(url, test, callback, { async: true, defer: true })
39+
</script>
40+
```
41+
42+
No more tears!

index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default function (url, test, callback, options = { async = true, defer = true }) {
2+
if (!test()) {
3+
const tag = document.createElement('script')
4+
tag.src = url
5+
tag.async = options.async
6+
tag.defer = options.defer
7+
tag.onload = callback
8+
document.body.appendChild(tag)
9+
} else {
10+
callback()
11+
}
12+
}

0 commit comments

Comments
 (0)