@solid-primitives/script-loader
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,3 +0,13 @@ | ||
import { Accessor } from "solid-js"; | ||
export declare const createScriptLoader: (opts: { | ||
import { Accessor } from 'solid-js'; | ||
/** | ||
* Creates a convenient script loader utility | ||
* | ||
* @param string URL of the script to load. | ||
* @param type Type value to put in the script attribute. | ||
* @param function Callback to trigger onLoad. | ||
* @param function callback on error. | ||
* @returns | ||
*/ | ||
declare const createScriptLoader: (opts: { | ||
src: string | Accessor<string>; | ||
@@ -7,2 +17,4 @@ type?: string; | ||
onerror?: () => void; | ||
}) => [script: HTMLScriptElement, remove: () => void]; | ||
}) => [script: HTMLScriptElement | undefined, remove: () => void]; | ||
export { createScriptLoader }; |
@@ -0,15 +1,19 @@ | ||
// src/index.ts | ||
import { createEffect, onCleanup } from "solid-js"; | ||
export const createScriptLoader = (opts) => { | ||
const script = document.createElement("script"); | ||
var createScriptLoader = (opts) => { | ||
const script = document.createElement("script"); | ||
script.src = typeof opts.src === "function" ? opts.src() : opts.src; | ||
opts.type && (script.type = opts.type); | ||
opts.onload && script.addEventListener("load", opts.onload); | ||
opts.onerror && script.addEventListener("error", opts.onerror); | ||
document.head.appendChild(script); | ||
const remove = () => document.head.removeChild(script); | ||
createEffect(() => { | ||
script.src = typeof opts.src === "function" ? opts.src() : opts.src; | ||
opts.type && (script.type = opts.type); | ||
opts.onload && script.addEventListener("load", opts.onload); | ||
opts.onerror && script.addEventListener("error", opts.onerror); | ||
document.head.appendChild(script); | ||
const remove = () => document.head.removeChild(script); | ||
createEffect(() => { | ||
script.src = typeof opts.src === "function" ? opts.src() : opts.src; | ||
}); | ||
onCleanup(remove); | ||
return [script, remove]; | ||
}); | ||
onCleanup(remove); | ||
return [script, remove]; | ||
}; | ||
export { | ||
createScriptLoader | ||
}; |
{ | ||
"name": "@solid-primitives/script-loader", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Primitive to load scripts dynamically", | ||
@@ -8,20 +8,35 @@ "author": "Alex Lohr <alex.lohr@logmein.com>", | ||
"homepage": "https://github.com/davedbase/solid-primitives/tree/main/packages/script", | ||
"private": false, | ||
"main": "dist/index.js", | ||
"module": "dist/index.js", | ||
"type": "module", | ||
"types": "dist/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/davedbase/solid-primitives.git" | ||
}, | ||
"primitive": { | ||
"name": "script-loader", | ||
"stage": 3, | ||
"list": [ | ||
"createScriptLoader" | ||
], | ||
"category": "Utilities" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"private": false, | ||
"sideEffects": false, | ||
"type": "module", | ||
"main": "./dist/server.cjs", | ||
"module": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
"require": "./dist/cjs/index.js", | ||
"node": { | ||
"import": "./dist/server.js", | ||
"require": "./dist/server.cjs" | ||
}, | ||
"import": "./dist/index.js", | ||
"default": "./dist/index.js" | ||
"require": "./dist/index.cjs" | ||
}, | ||
"sideEffects": "false", | ||
"scripts": { | ||
"prebuild": "npm run clean", | ||
"clean": "rimraf dist/", | ||
"build": "tsc && tsc --target es5 --module commonjs --declaration false --outDir ./dist/cjs", | ||
"build": "tsup", | ||
"test": "uvu -r solid-register test" | ||
@@ -41,2 +56,3 @@ }, | ||
"tslib": "^2.3.1", | ||
"tsup": "^5.10.1", | ||
"typescript": "^4.4.3", | ||
@@ -43,0 +59,0 @@ "uvu": "^0.5.2" |
@@ -1,8 +0,1 @@ | ||
--- | ||
Name: script-loader | ||
Stage: 3 | ||
Package: "@solid-primitives/script-loader" | ||
Primitives: createScriptLoader | ||
--- | ||
# @solid-primitives/script-loader | ||
@@ -16,2 +9,10 @@ | ||
## Installation | ||
``` | ||
npm install @solid-primitives/script-loader | ||
# or | ||
yarn add @solid-primitives/script-loader | ||
``` | ||
## How to use it | ||
@@ -51,3 +52,3 @@ | ||
1.0.1 | ||
1.0.2 | ||
@@ -54,0 +55,0 @@ Release first first with CJS support. |
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
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
6915
7
115
56
0
8