simple-load-script
Advanced tools
Comparing version 1.0.3 to 2.0.0-beta.0
{ | ||
"name": "simple-load-script", | ||
"version": "1.0.3", | ||
"version": "2.0.0-beta.0", | ||
"description": "Very simple promise based script and JSONP", | ||
@@ -8,38 +8,65 @@ "repository": "tomek-f/simple-load-script", | ||
"script", | ||
"load", | ||
"promise", | ||
"JSONP", | ||
"UMD", | ||
"callback" | ||
"JSONP" | ||
], | ||
"scripts": { | ||
"prepare": "npm run build", | ||
"makedir": "mkdirp ./es5 && mkdirp ./es5-umd", | ||
"browserify": "browserify es6/simpleLoadScript.js -s simpleLoadScript -t babelify -o es5-umd/simpleLoadScript.js", | ||
"umd": "npm run makedir && npm run browserify", | ||
"es5": "babel es6 --out-dir es5", | ||
"es5-watch": "babel es6 --out-dir es5 --watch", | ||
"umd-watch": "watch-run -i -p 'es6/simpleLoadScript.js' npm run umd", | ||
"build": "npm run umd && npm run es5", | ||
"watch": "npm run umd-watch & npm run es5-watch" | ||
"check-all": "npm run lint && npm run format-check && npm run check-types && npm run test:run", | ||
"prepublishOnly": "npm run check-all && npm run build", | ||
"build": "rollup --c ./rollup.config.mjs", | ||
"watch": "npm run build -- -w", | ||
"lint": "eslint --ext .ts . -f tap", | ||
"vite:build-and-preview": "vite build && vite preview", | ||
"test:run": "vite build && vitest run", | ||
"test:ui": "vite build && vitest --ui", | ||
"test:watch": "vite build && vitest", | ||
"format-check": "prettier --check ./**/*.ts", | ||
"format-write": "prettier --write ./**/*.ts", | ||
"check-types": "echo 'checking types…' && tsc && echo '…no type problems'" | ||
}, | ||
"files": [ | ||
"es6", | ||
"es5", | ||
"es5-umd" | ||
"dist" | ||
], | ||
"main": "es5/simpleLoadScript.js", | ||
"main": "./dist/index.umd.js", | ||
"umd:main": "./dist/index.umd.js", | ||
"module": "./dist/index.es.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
"./package.json": "./package.json", | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.es.js", | ||
"require": "./dist/index.cjs.js", | ||
"default": "./dist/index.umd.js" | ||
} | ||
}, | ||
"author": "Tomek Fijoł <tomekfijol@gmail.com> (http://tomekf.pl/)", | ||
"contributors": [ | ||
{ | ||
"name": "Martin Jurča", | ||
"email": "martin.jurca@firma.seznam.cz" | ||
}, | ||
{ | ||
"name": "Tom Conroy", | ||
"url": "https://github.com/tconroy" | ||
} | ||
], | ||
"license": "MIT", | ||
"readmeFilename": "README.md", | ||
"devDependencies": { | ||
"@babel/cli": "^7.13.14", | ||
"@babel/core": "^7.13.14", | ||
"@babel/preset-env": "^7.13.12", | ||
"babel-plugin-add-module-exports": "^1.0.4", | ||
"babelify": "^10.0.0", | ||
"browserify": "^17.0.0", | ||
"mkdirp": "^1.0.4", | ||
"watch-run": "^1.2.5" | ||
"@rollup/plugin-terser": "^0.4.1", | ||
"@rollup/plugin-typescript": "^11.1.0", | ||
"@types/prettier": "^2.7.2", | ||
"@typescript-eslint/eslint-plugin": "^5.59.2", | ||
"@typescript-eslint/parser": "^5.59.2", | ||
"@vitest/ui": "^0.31.0", | ||
"eslint": "^8.39.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"playwright": "^1.33.0", | ||
"prettier": "^2.8.8", | ||
"rollup": "^3.21.5", | ||
"typescript": "^5.0.4", | ||
"vite": "^4.3.5", | ||
"vitest": "^0.31.0" | ||
} | ||
} |
216
README.md
@@ -5,5 +5,2 @@ # simple-load-script | ||
* Promise based ([use polyfill if you need](http://caniuse.com/#feat=promises)) | ||
* uses addEventListener (IE9+) | ||
## Installation | ||
@@ -17,32 +14,15 @@ | ||
```js | ||
// es5 CommonJS | ||
const loadScript = require('simple-load-script'); | ||
[Check dist folder](https://www.npmjs.com/package/simple-load-script?activeTab=code) or package.json | ||
// es6 | ||
const loadScript = require('simple-load-script/es6/simpleLoadScript'); | ||
// es5-umd | ||
const loadScript = require('simple-load-script/es5-umd/simpleLoadScript'); | ||
``` | ||
## Usage | ||
```js | ||
import loadScript from 'simple-load-script'; | ||
### Async/await | ||
loadScript('//code.jquery.com/jquery-2.2.3.js') | ||
.then(function(scriptRef) { | ||
console.log('success', scriptRef); | ||
}) | ||
.catch(function(err) { | ||
console.log(err); | ||
}); | ||
``` | ||
```js | ||
import loadScript from 'simple-load-script'; | ||
import simpleLoadScript from 'simple-load-script'; | ||
try { | ||
/* const scriptRef = */ loadScript('//code.jquery.com/jquery-2.2.3.js'); | ||
const scriptRef = await simpleLoadScript('//code.jquery.com/jquery-2.2.3.js'); | ||
console.log(scriptRef); // HTMLScriptElement | ||
} catch (err) { | ||
@@ -53,27 +33,12 @@ console.log(err); | ||
```js | ||
import loadScript from 'simple-load-script'; | ||
### Promise | ||
loadScript('//code.jquery.com/jquery-2.2.3.js', { | ||
inBody: true | ||
}) | ||
.then(function(scriptRef) { | ||
console.log('success', scriptRef); // 'success', script ref. | ||
}) | ||
.catch(function(err) { | ||
console.log(err); | ||
}); | ||
``` | ||
```js | ||
import loadScript from 'simple-load-script'; | ||
import simpleLoadScript from 'simple-load-script'; | ||
loadScript({ | ||
url: '//code.jquery.com/jquery-2.2.3.js', | ||
attrs: { id: 'one', charset: 'UTF-8' } | ||
}) | ||
.then(function(scriptRef) { | ||
console.log('success', scriptRef); // 'success', script ref. | ||
simpleLoadScript('//code.jquery.com/jquery-2.2.3.js') | ||
.then(function (scriptRef) { | ||
console.log(scriptRef); | ||
}) | ||
.catch(function(err) { | ||
.catch(function (err) { | ||
console.log(err); | ||
@@ -83,81 +48,74 @@ }); | ||
Google Maps API | ||
### Config object | ||
```js | ||
import loadScript from 'simple-load-script'; | ||
import simpleLoadScript from 'simple-load-script'; | ||
loadScript({ | ||
url: '//maps.googleapis.com/maps/api/js?&callback=gmapiready', | ||
callBackName: 'gmapiready' | ||
}) | ||
.then(function(scriptRef) { | ||
console.log('success', scriptRef); // 'success', undefined | ||
}) | ||
.catch(function(err) { | ||
console.log(err); | ||
try { | ||
const scriptRef = await simpleLoadScript({ | ||
url: '//code.jquery.com/jquery-2.2.3.js', | ||
inBody: true, | ||
attrs: { id: 'one', charset: 'UTF-8' }, | ||
}); | ||
console.log(scriptRef); // HTMLScriptElement inBody with attrs present | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
``` | ||
JSONP | ||
### Google Maps API | ||
Runs global callback (window.gmapiready) | ||
```js | ||
var loadScript = require('simple-load-script'); | ||
import simpleLoadScript from 'simple-load-script'; | ||
loadScripts('//api.ipinfodb.com/v3/ip-city/?format=json&callback=elo', { | ||
callBackName: 'elo', | ||
removeScript: true | ||
}) | ||
.then(function(scriptRef) { | ||
console.log('success', scriptRef); // 'success', res | ||
}) | ||
.catch(function(err) { | ||
console.log(err); | ||
}); | ||
try { | ||
const scriptRef = await simpleLoadScript('//maps.googleapis.com/maps/api/js?&callback=gmapiready'); | ||
console.log(scriptRef); // HTMLScriptElement | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
``` | ||
Load more scripts (Promise.all) - urls | ||
### JSONP | ||
Runs global callback (window.elo) | ||
```js | ||
import loadScript from 'simple-load-script'; | ||
var simpleLoadScript = require('simple-load-script'); | ||
loadScripts( | ||
'//example.com/test1.js', | ||
'//example.com/test2.js', | ||
'//example.com/test3.js' | ||
) | ||
.then(function(scriptRef) { | ||
console.log('success', scriptRef); // 'success', res | ||
}) | ||
.catch(function(err) { | ||
console.log(err); | ||
try { | ||
const scriptRef = await simpleLoadScript({ | ||
url: '//api.ipinfodb.com/v3/ip-city/?format=json&callback=elo', | ||
removeScript: true, | ||
}); | ||
console.log(scriptRef); // undefined | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
``` | ||
Load more scripts (Promise.all) - objects and urls, callBackNames must be unique names | ||
### Array mode - objects and urls, callBackNames must have unique names | ||
```js | ||
import loadScript from 'simple-load-script'; | ||
import simpleLoadScript from 'simple-load-script'; | ||
loadScripts( | ||
{ | ||
url: '//maps.googleapis.com/maps/api/js?&callback=gmapiready', | ||
callBackName: 'gmapiready' | ||
}, | ||
{ | ||
url: '//api.ipinfodb.com/v3/ip-city/?format=json&callback=elo', | ||
callBackName: 'elo', | ||
removeScript: true | ||
}, | ||
[ | ||
'https://api.twitter.com/1/statuses/oembed.json?id=507185938620219395&callback=elo2', | ||
{ callBackName: 'elo2' } | ||
], | ||
'//code.jquery.com/jquery-2.2.3.js' | ||
) | ||
.then(function(scriptRef) { | ||
console.log('success', scriptRef); // 'success', array | ||
}) | ||
.catch(function(err) { | ||
console.log(err); | ||
}); | ||
try { | ||
const scriptRefs = await simpleLoadScript([ | ||
'//maps.googleapis.com/maps/api/js?&callback=gmapiready', | ||
{ | ||
url: '//api.ipinfodb.com/v3/ip-city/?format=json&callback=elo', | ||
removeScript: true, | ||
}, | ||
'//code.jquery.com/jquery-2.2.3.js', | ||
]); | ||
console.log(scriptRefs); // HTMLScriptElement[] | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
``` | ||
@@ -167,22 +125,42 @@ | ||
* `url` (optional) - file to append to body | ||
* `options` (required) - options in object | ||
`Config | string | (Config | string)[]` | ||
## Options | ||
## Config | ||
* `url` (string) - file to append to body | ||
* `inBody` (boolean) - append to `document.body` instead of `document.head` | ||
* `attrs` (object) - with attributes to append to script tag (`charset`, `type`, `id`, …) | ||
* `callBackName` (string) - callback to add to `window` object; promise is resolved after callback is fired; callback is removed after that; multiple callbacks must have unique names | ||
* `dontRemoveCallBack` (boolean) - from `window` after load; no real use - let me know | ||
* `removeScript` (boolean) - after load (for JSONP, other reasons); it's always removed on error | ||
* `insertInto` (string) - [CSS selector (an ID, class name, element name, etc.)](https://developer.mozilla.org/en/docs/Web/API/Document/querySelector) to insert the script into. Overrides `inBody` value. | ||
### Interface | ||
## Returned values | ||
```ts | ||
interface Config { | ||
url: string; | ||
attrs?: Record<string, string>; | ||
inBody?: boolean; | ||
insertInto?: string | null; | ||
removeScript?: boolean; | ||
} | ||
``` | ||
* then: resource (JSONP - options.callBackName) script reference in DOM or undefined (options.callBackName, options.removeScript) | ||
* catch: error message | ||
### Default values | ||
```js | ||
const defaultConfig = { | ||
url: '', | ||
attrs: {}, | ||
inBody: false, | ||
insertInto: null, | ||
removeScript: false, | ||
}; | ||
``` | ||
- `url` - file to append to body | ||
- `attrs` - with attributes to append to script tag (`charset`, `type`, `id`, …) | ||
- `inBody` - append to `document.body` instead of `document.head` | ||
- `insertInto` - [CSS selector (an ID, class name, element name, etc.)](https://developer.mozilla.org/en/docs/Web/API/Document/querySelector) to insert the script into. Overrides `inBody` value. | ||
- `removeScript` - remove script tag after load; it's always removed on an error | ||
## Changelog | ||
[View on github](https://github.com/tomek-f/simple-load-script/blob/master/changelog.md). | ||
## Misc. | ||
- uses addEventListener, Array.isArray, for…of, destructuring Promise & Promise.all |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
1
9853
15
22
2
162