-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jason Chung
committed
Nov 11, 2018
0 parents
commit 90951e8
Showing
10 changed files
with
3,161 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
dist | ||
node_modules | ||
.DS_Store | ||
*.log |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
*.log | ||
.travis.yml | ||
index.js | ||
package-lock.json | ||
rollup.config.js | ||
test.js | ||
yarn.lock |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
language: node_js | ||
node_js: | ||
- "node" | ||
jobs: | ||
include: | ||
- script: yarn test | ||
- stage: publish | ||
if: tag IS present | ||
node_js: "node" | ||
script: yarn build | ||
deploy: | ||
provider: npm | ||
email: "$NPM_EMAIL" | ||
api_key: "$NPM_TOKEN" | ||
skip_cleanup: true | ||
on: | ||
tags: true |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2018 Jason Chung | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# @shinin/load-script · [](https://github.com/shiningjason/load-script/blob/master/LICENSE) [](https://www.npmjs.com/package/@shinin/load-script) [](https://travis-ci.org/shiningjason/load-script) | ||
|
||
`@shinin/load-script` is a simple async script loader for modern browsers. | ||
|
||
### 3 seconds quick view | ||
|
||
```js | ||
import loadScript from '@shinin/load-script' | ||
|
||
export default async function loadSDK() { | ||
await loadScript('http://foo.bar/sdk.js') | ||
// Now, you can do what you want after SDK.js has loaded... | ||
} | ||
``` | ||
|
||
## Installation | ||
``` | ||
npm install @shinin/load-script --save | ||
``` | ||
|
||
Also you can use `@shinin/load-script` as a `<script>` tag from a [CDN](https://unpkg.com/@shinin/load-script). | ||
|
||
```html | ||
<!-- Use promise-polyfill only if you want to support old browsers. --> | ||
<!-- <script src="https://unpkg.com/promise-polyfill"></script> --> | ||
<script src="https://unpkg.com/@shinin/load-script"></script> | ||
``` | ||
|
||
## Browser Support | ||
|
||
- Chrome | ||
- Firefox | ||
- Safari 7.1+ | ||
- Safari 5.1+ (Use [promises-polyfill](https://github.com/taylorhakes/promise-polyfill)) | ||
- IE 8+ (Use [promises-polyfill](https://github.com/taylorhakes/promise-polyfill)) | ||
|
||
## License | ||
|
||
[MIT](https://github.com/shiningjason/load-script/blob/master/LICENSE) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const promises = {} | ||
|
||
export default src => { | ||
const doc = document | ||
if (promises[src]) return promises[src] | ||
|
||
let onLoadSuccess, onLoadError | ||
promises[src] = new Promise((resolve, reject) => { | ||
onLoadSuccess = resolve | ||
onLoadError = reject | ||
}) | ||
|
||
let loaded = false | ||
const tag = 'script' | ||
const script = doc.createElement(tag) | ||
script.src = src | ||
script.async = 1 | ||
script.onreadystatechange = script.onload = () => { | ||
const { readyState } = script | ||
if (readyState && !['complete', 'loaded'].includes(readyState)) return | ||
if (loaded) return | ||
loaded = true | ||
onLoadSuccess() | ||
} | ||
script.onerror = onLoadError | ||
|
||
const firstScript = doc.querySelector(tag) | ||
firstScript.parentNode.insertBefore(script, firstScript) | ||
|
||
return promises[src] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
{ | ||
"name": "@shinin/load-script", | ||
"version": "1.0.0", | ||
"description": "Load script asynchronously.", | ||
"main": "dist/load-script.cjs.js", | ||
"module": "dist/load-script.esm.js", | ||
"browser": "dist/load-script.min.js", | ||
"repository": "https://github.com/shiningjason/load-script", | ||
"author": "Jason Chung <shiningjason1989@gmail.com>", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"keywords": [ | ||
"browser", | ||
"script", | ||
"load", | ||
"load-js", | ||
"async", | ||
"promise" | ||
], | ||
"scripts": { | ||
"format": "prettier --single-quote --no-semi --write *.js", | ||
"lint": "eslint *.js", | ||
"build": "rollup --config", | ||
"test": "NODE_ENV=test mocha test --require @babel/register" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.1.5", | ||
"@babel/preset-env": "^7.1.5", | ||
"@babel/register": "^7.0.0", | ||
"eslint": "^5.8.0", | ||
"eslint-config-prettier": "^3.1.0", | ||
"eslint-config-standard": "^12.0.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-node": "^8.0.0", | ||
"eslint-plugin-promise": "^4.0.1", | ||
"eslint-plugin-standard": "^4.0.0", | ||
"jsdom": "^13.0.0", | ||
"mocha": "^5.2.0", | ||
"prettier": "^1.15.1", | ||
"rollup": "^0.67.0", | ||
"rollup-plugin-babel": "^4.0.3", | ||
"rollup-plugin-uglify": "^6.0.0" | ||
}, | ||
"babel": { | ||
"env": { | ||
"test": { | ||
"presets": [ | ||
"@babel/env" | ||
] | ||
} | ||
} | ||
}, | ||
"eslintConfig": { | ||
"extends": [ | ||
"standard", | ||
"prettier", | ||
"prettier/standard" | ||
] | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import babel from 'rollup-plugin-babel' | ||
import { uglify } from 'rollup-plugin-uglify' | ||
import pkg from './package.json' | ||
|
||
const input = './index.js' | ||
const babelPlugin = babel({ | ||
babelrc: false, | ||
exclude: 'node_modules/**', | ||
presets: [['@babel/env', { modules: false }]] | ||
}) | ||
|
||
export default [ | ||
{ | ||
input, | ||
output: [ | ||
{ | ||
file: pkg.main, | ||
format: 'cjs', | ||
interop: false | ||
}, | ||
{ | ||
file: pkg.module, | ||
format: 'esm' | ||
} | ||
], | ||
plugins: [babelPlugin] | ||
}, | ||
{ | ||
input, | ||
output: { | ||
file: pkg.browser, | ||
format: 'iife', | ||
name: 'loadScript' | ||
}, | ||
plugins: [babelPlugin, uglify()] | ||
} | ||
] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* global beforeEach describe it */ | ||
import assert from 'assert' | ||
import { JSDOM } from 'jsdom' | ||
|
||
const testScript = 'https://unpkg.com/fetch' | ||
const htmlTemplate = '<!DOCTYPE html><body></body></html>' | ||
|
||
function reRequire(module) { | ||
delete require.cache[require.resolve(module)] | ||
return require(module) | ||
} | ||
|
||
describe('loadScript', () => { | ||
const jsdom = new JSDOM(htmlTemplate) | ||
global.document = jsdom.window.document | ||
let loadScript | ||
|
||
beforeEach(() => { | ||
global.document.body.innerHTML = '<script></script>' | ||
loadScript = reRequire('./') | ||
}) | ||
|
||
it('should return promise', () => { | ||
const promise = loadScript(testScript) | ||
assert(promise instanceof Promise) | ||
}) | ||
|
||
it('should load script successfully', done => { | ||
loadScript(testScript).then(done) | ||
const element = document.querySelector('[src]') | ||
element.onload() | ||
}) | ||
|
||
it('should handle loading error', done => { | ||
loadScript(testScript).then(() => {}, done) | ||
const element = document.querySelector('[src]') | ||
element.onerror() | ||
}) | ||
}) |
Oops, something went wrong.