@internetarchive/lazy-loader-service
Advanced tools
Comparing version 0.0.1-alpha.564 to 0.1.0
{ | ||
"name": "@internetarchive/lazy-loader-service", | ||
"version": "0.0.1-alpha.564+90289d0", | ||
"version": "0.1.0", | ||
"description": "A small library to lazy load javascript with a Promise", | ||
"license": "AGPL-3.0-only", | ||
"main": "index.js", | ||
"module": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"lib/**/*", | ||
"src/**/*", | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"prepare": "yarn run build", | ||
"build": "tsc -p tsconfig.build.json", | ||
"start": "concurrently \"tsc --watch\" \"es-dev-server --app-index demo/index.html --node-resolve --open --watch --preserve-symlinks\"", | ||
"start:compatibility": "concurrently \"tsc --watch\" \"es-dev-server --app-index demo/index.html --node-resolve --open --watch --compatibility all --preserve-symlinks\"", | ||
"lint:eslint": "eslint --ext .ts . --ignore-path .gitignore", | ||
"format:eslint": "eslint --ext .ts . --fix --ignore-path .gitignore", | ||
"lint": "yarn run lint:eslint", | ||
"format": "yarn run format:eslint", | ||
"test": "yarn run build && yarn lint && karma start --coverage", | ||
"test:watch": "concurrently \"tsc --watch\" \"karma start --auto-watch=true --single-run=false\"", | ||
"test:update-snapshots": "karma start --update-snapshots", | ||
"test:prune-snapshots": "karma start --prune-snapshots", | ||
"test:compatibility": "karma start --compatibility all --coverage", | ||
"test:compatibility:watch": "karma start --compatibility all --auto-watch=true --single-run=false", | ||
"test:bs": "karma start karma.bs.config.js --compatibility all --coverage" | ||
"start": "concurrently --kill-others --names tsc,es-dev-server \"npm run tsc:watch\" \"es-dev-server --app-index demo/index.html --node-resolve --open --watch\"", | ||
"docs": "typedoc", | ||
"tsc:watch": "tsc --watch", | ||
"lint:eslint": "eslint --ext .ts,.html . --ignore-path .gitignore", | ||
"format:eslint": "eslint --ext .ts,.html . --fix --ignore-path .gitignore", | ||
"lint:prettier": "prettier \"**/*.js\" \"**/*.ts\" --check --ignore-path .gitignore", | ||
"format:prettier": "prettier \"**/*.js\" \"**/*.ts\" --write --ignore-path .gitignore", | ||
"lint": "npm run lint:eslint && npm run lint:prettier", | ||
"format": "npm run format:eslint && npm run format:prettier", | ||
"test": "npm run lint && tsc && karma start --coverage", | ||
"test:watch": "concurrently --kill-others --names tsc,karma \"npm run tsc:watch\" \"karma start --auto-watch=true --single-run=false\"" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@open-wc/eslint-config": "^1.0.0", | ||
"@open-wc/prettier-config": "^0.1.10", | ||
"@open-wc/eslint-config": "^2.0.0", | ||
"@open-wc/testing": "^2.0.0", | ||
"@open-wc/testing-karma": "^3.0.0", | ||
"@open-wc/testing-karma-bs": "^1.0.0", | ||
"@types/form-data": "^2.5.0", | ||
"@typescript-eslint/eslint-plugin": "^3.3.0", | ||
"@typescript-eslint/parser": "^3.3.0", | ||
"concurrently": "^4.1.2", | ||
"es-dev-server": "^1.5.0", | ||
"eslint": "^7.2.0", | ||
"gulp": "^4.0.2", | ||
"@types/node": "13.11.1", | ||
"@typescript-eslint/eslint-plugin": "^2.20.0", | ||
"@typescript-eslint/parser": "^2.20.0", | ||
"concurrently": "^5.1.0", | ||
"deepmerge": "^3.2.0", | ||
"es-dev-server": "^1.23.0", | ||
"eslint": "^6.1.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"husky": "^1.0.0", | ||
"karma-coverage": "^2.0.1", | ||
"lint-staged": "^8.0.0", | ||
"lit-html": "^1.0.0", | ||
"polymer-cli": "^1.9.11", | ||
"typescript": "^3.9.5", | ||
"webpack-merge": "^4.1.5" | ||
"lit-html": "^1.1.2", | ||
"prettier": "^2.0.4", | ||
"tslib": "^1.11.0", | ||
"typedoc": "^0.17.8", | ||
"typescript": "~3.8.2" | ||
}, | ||
@@ -59,13 +49,18 @@ "eslintConfig": { | ||
}, | ||
"prettier": "@open-wc/prettier-config", | ||
"publishConfig": { | ||
"access": "public" | ||
"prettier": { | ||
"singleQuote": true, | ||
"arrowParens": "avoid" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"*.ts": [ | ||
"eslint --fix", | ||
"prettier --write", | ||
"git add" | ||
] | ||
}, | ||
"gitHead": "90289d024d4b9c421eb8178fc56024f364e359d8" | ||
} | ||
} |
@@ -0,1 +1,3 @@ | ||
[![Build Status](https://travis-ci.com/internetarchive/iaux-lazy-loader-service.svg?branch=master)](https://travis-ci.com/internetarchive/iaux-lazy-loader-service) [![codecov](https://codecov.io/gh/internetarchive/iaux-lazy-loader-service/branch/master/graph/badge.svg)](https://codecov.io/gh/internetarchive/iaux-lazy-loader-service) | ||
# Lazy Loader Service | ||
@@ -7,3 +9,3 @@ | ||
```bash | ||
yarn add @internetarchive/lazy-loader-service | ||
npm install @internetarchive/lazy-loader-service | ||
``` | ||
@@ -72,3 +74,3 @@ | ||
```bash | ||
yarn install | ||
npm install | ||
``` | ||
@@ -78,3 +80,3 @@ | ||
```bash | ||
yarn start | ||
npm start | ||
``` | ||
@@ -84,3 +86,3 @@ | ||
```bash | ||
yarn test | ||
npm test | ||
``` | ||
@@ -90,3 +92,3 @@ | ||
```bash | ||
yarn test:bs | ||
npm test:bs | ||
``` | ||
@@ -96,3 +98,3 @@ | ||
```bash | ||
yarn lint | ||
npm lint | ||
``` |
@@ -1,50 +0,8 @@ | ||
export enum BundleType { | ||
Module = 'module', | ||
NoModule = 'nomodule' | ||
} | ||
import { BundleType } from './bundle-type'; | ||
import { LazyLoaderServiceInterface } from './lazy-loader-service-interface'; | ||
export interface LazyLoaderServiceInterface { | ||
/** | ||
* Load a javascript bundle (module and nomodule pair) | ||
* | ||
* eg: | ||
* | ||
* lazyLoaderService.loadBundle({ | ||
* module: 'https://my-server.com/module.js', | ||
* nomodule: 'https://my-server.com/no-module.js' | ||
* }); | ||
* | ||
* @param bundle | ||
*/ | ||
loadBundle(bundle: { | ||
module?: string; | ||
nomodule?: string; | ||
}): Promise<Event | undefined>; | ||
/** | ||
* Load a script with a Promise | ||
* | ||
* eg. | ||
* | ||
* lazyLoaderService.loadScript({ | ||
* src: 'https://my-server.com/script.js' | ||
* }); | ||
* | ||
* | ||
* @param options | ||
*/ | ||
loadScript(options: { | ||
src: string; | ||
bundleType?: BundleType; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
attributes?: { key: string; value: any }[]; | ||
}): Promise<Event>; | ||
} | ||
export class LazyLoaderService { | ||
export class LazyLoaderService implements LazyLoaderServiceInterface { | ||
private container: HTMLElement; | ||
constructor( | ||
container: HTMLElement = document.head | ||
) { | ||
constructor(container: HTMLElement = document.head) { | ||
this.container = container; | ||
@@ -65,3 +23,3 @@ } | ||
src: bundle.module, | ||
bundleType: BundleType.Module | ||
bundleType: BundleType.Module, | ||
}); | ||
@@ -74,3 +32,3 @@ } | ||
src: bundle.nomodule, | ||
bundleType: BundleType.NoModule | ||
bundleType: BundleType.NoModule, | ||
}); | ||
@@ -90,3 +48,5 @@ } | ||
const scriptSelector = `script[src='${options.src}'][async]`; | ||
let script = this.container.querySelector(scriptSelector) as HTMLScriptElement; | ||
let script = this.container.querySelector( | ||
scriptSelector | ||
) as HTMLScriptElement; | ||
if (!script) { | ||
@@ -109,3 +69,4 @@ script = document.createElement('script') as HTMLScriptElement; | ||
break; | ||
/* istanbul ignore next */ // cannot be tested because modern browsers ignore `nomodule` | ||
// cannot be tested because modern browsers ignore `nomodule` | ||
/* istanbul ignore next */ | ||
case BundleType.NoModule: | ||
@@ -120,8 +81,22 @@ script.setAttribute(options.bundleType, ''); | ||
return new Promise((resolve, reject) => { | ||
script.onload = (e) => { | ||
// if multiple requests get made for this script, just stack the onloads | ||
// and onerrors and all the callbacks will be called in-order of being received | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const originalOnLoad: ((event: Event) => any) | null = script.onload; | ||
script.onload = (event): void => { | ||
if (originalOnLoad) { | ||
originalOnLoad(event); | ||
} | ||
script.setAttribute('dynamicImportLoaded', 'true'); | ||
resolve(e); | ||
resolve(event); | ||
}; | ||
script.onerror = (e) => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const originalOnError: ((error: string | Event) => any) | null = | ||
script.onerror; | ||
script.onerror = (error): void => { | ||
if (originalOnError) { | ||
originalOnError(error); | ||
} | ||
/* istanbul ignore else */ | ||
@@ -131,3 +106,3 @@ if (script.parentNode) { | ||
} | ||
reject(e); | ||
reject(error); | ||
}; | ||
@@ -134,0 +109,0 @@ |
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
Network access
Supply chain riskThis module accesses the network.
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
373251
18
52
5563
95
2