@beyonk/async-script-loader
Advanced tools
Comparing version 2.2.2 to 2.3.0
16
index.js
@@ -15,7 +15,9 @@ function load (urls, test, callback) { | ||
for (const { type, url, options = { async: true, defer: true }} of urls) { | ||
for (const { type, url, content, options = { async: true, defer: true }} of urls) { | ||
const isScript = type === 'script' | ||
const tag = document.createElement(isScript ? 'script': 'link') | ||
const attribute = isScript ? 'src' : 'href' | ||
const hasUrl = Boolean(url).valueOf() | ||
if (isScript) { | ||
tag.src = url | ||
tag.async = options.async | ||
@@ -25,4 +27,12 @@ tag.defer = options.defer | ||
tag.rel = 'stylesheet' | ||
tag.href = url | ||
} | ||
if (hasUrl) { | ||
tag[attribute] = url | ||
} else { | ||
tag.appendChild( | ||
document.createTextNode(content) | ||
) | ||
} | ||
tag.onload = maybeCallback | ||
@@ -29,0 +39,0 @@ document.body.appendChild(tag) |
{ | ||
"name": "@beyonk/async-script-loader", | ||
"version": "2.2.2", | ||
"version": "2.3.0", | ||
"description": "Loads scripts cleanly and asynchronously in SPAs", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -87,1 +87,20 @@ <a href="https://beyonk.com"> | ||
No more tears! | ||
#### Inline scripts / Inline css | ||
You can use inline content for either type of tag by passing the configuration attribute `content` *instead* of `url`. This will write the content passed into the tag's body rather than setting it as an `href` or `src` attribute `url` will always take prescidence, so leave it out for `content` to work. | ||
```js | ||
<script> | ||
import loader from '@beyonk/async-script-loader' | ||
loader([ | ||
{ type: 'script', content: 'console.log("foo");' }, | ||
{ type: 'style', content: '* { color: red; }' } | ||
], | ||
() => false, // always load | ||
() => () // no-op | ||
) | ||
</script> | ||
``` |
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
5201
34
105