Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Chung committed Nov 11, 2018
0 parents commit 90951e8
Show file tree
Hide file tree
Showing 10 changed files with 3,161 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist
node_modules
.DS_Store
*.log
7 changes: 7 additions & 0 deletions .npmignore
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
17 changes: 17 additions & 0 deletions .travis.yml
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
9 changes: 9 additions & 0 deletions LICENSE
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.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# @shinin/load-script · [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/shiningjason/load-script/blob/master/LICENSE) [![npm](https://img.shields.io/npm/v/@shinin/load-script.svg)](https://www.npmjs.com/package/@shinin/load-script) [![Build Status](https://travis-ci.org/shiningjason/load-script.svg?branch=master)](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)
31 changes: 31 additions & 0 deletions index.js
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]
}
62 changes: 62 additions & 0 deletions package.json
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"
]
}
}
37 changes: 37 additions & 0 deletions rollup.config.js
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()]
}
]
39 changes: 39 additions & 0 deletions test.js
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()
})
})
Loading

0 comments on commit 90951e8

Please sign in to comment.