@internetarchive/lazy-loader-service
Advanced tools
Comparing version 0.0.0-alpha.2 to 0.0.0-alpha.3
{ | ||
"name": "@internetarchive/lazy-loader-service", | ||
"version": "0.0.0-alpha.2", | ||
"version": "0.0.0-alpha.3", | ||
"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": "npm 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": "npm run lint:eslint", | ||
"format": "npm run format:eslint", | ||
"test": "npm run build && npm run 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": "^2.0.6", | ||
"@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.4.4", | ||
"@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", | ||
"@open-wc/testing-karma": "^3.0.0", | ||
"@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" | ||
}, | ||
@@ -60,5 +49,5 @@ "eslintConfig": { | ||
}, | ||
"prettier": "@open-wc/prettier-config", | ||
"publishConfig": { | ||
"access": "public" | ||
"prettier": { | ||
"singleQuote": true, | ||
"arrowParens": "avoid" | ||
}, | ||
@@ -73,2 +62,3 @@ "husky": { | ||
"eslint --fix", | ||
"prettier --write", | ||
"git add" | ||
@@ -75,0 +65,0 @@ ] |
@@ -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: | ||
@@ -122,15 +83,18 @@ script.setAttribute(options.bundleType, ''); | ||
// and onerrors and all the callbacks will be called in-order of being received | ||
const originalOnLoad: ((ev: Event) => any) | null = script.onload; | ||
script.onload = (e) => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const originalOnLoad: ((event: Event) => any) | null = script.onload; | ||
script.onload = (event): void => { | ||
if (originalOnLoad) { | ||
originalOnLoad(e); | ||
originalOnLoad(event); | ||
} | ||
script.setAttribute('dynamicImportLoaded', 'true'); | ||
resolve(e); | ||
resolve(event); | ||
}; | ||
const originalOnError: ((error: string | Event) => any) | null = script.onerror; | ||
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(e); | ||
originalOnError(error); | ||
} | ||
@@ -142,3 +106,3 @@ | ||
} | ||
reject(e); | ||
reject(error); | ||
}; | ||
@@ -145,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
342193
18
52
3165
2