-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: loadCSS, loadScript to support options
- Loading branch information
1 parent
3c79808
commit 727d5bb
Showing
7 changed files
with
83 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,24 @@ | ||
<script setup lang="ts"> | ||
import { _stringifyAny } from '@naturalcycles/js-lib' | ||
import { _stringify } from '@naturalcycles/js-lib' | ||
import { ref } from 'vue' | ||
import { loadScript } from '../../src' | ||
import { loadCSS, loadScript } from '../../src' | ||
const loading = ref(false) | ||
async function loadGood() { | ||
await load(`https://unpkg.com/[email protected]/dist/jquery.js`) | ||
loading.value = false | ||
} | ||
async function loadBad() { | ||
await load(`https://unpkg.com/jqueryNON_EXISTING`) | ||
loading.value = false | ||
} | ||
async function loadGoodCSS() { | ||
await loadStylesheet(`https://cdn.simplecss.org/simple.min.css`) | ||
} | ||
async function loadBadCSS() { | ||
await loadStylesheet(`https://cdn.simplecss.org/simpleNOTFOUND.min.css`) | ||
} | ||
async function load(src: string) { | ||
|
@@ -22,7 +28,22 @@ async function load(src: string) { | |
await loadScript(src) | ||
alert('loaded ok') | ||
} catch (err) { | ||
alert(_stringifyAny(err)) | ||
alert(_stringify(err)) | ||
} finally { | ||
loading.value = false | ||
} | ||
} | ||
async function loadStylesheet(src: string) { | ||
loading.value = true | ||
try { | ||
await loadCSS(src) | ||
alert('loaded ok') | ||
} catch (err) { | ||
alert(_stringify(err)) | ||
} finally { | ||
loading.value = false | ||
} | ||
} | ||
</script> | ||
|
@@ -31,6 +52,9 @@ async function load(src: string) { | |
<div class="app-content"> | ||
<button @click="loadGood">Load good script</button> | ||
<button @click="loadBad">Load bad script</button> | ||
<br /><br /> | ||
<button @click="loadGoodCSS">Load good CSS</button> | ||
<button @click="loadBadCSS">Load bad CSS</button> | ||
<p v-if="loading">loading...</p> | ||
</div> | ||
</template> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,51 @@ | ||
/* eslint-disable unicorn/prefer-add-event-listener */ | ||
|
||
export async function loadScript(src: string, async = true): Promise<void> { | ||
// eslint-disable-next-line @typescript-eslint/return-await | ||
return new Promise<void>((resolve, reject) => { | ||
const s = document.createElement('script') | ||
s.src = src | ||
s.onload = resolve as any | ||
s.onerror = (_event, _source, _lineno, _colno, error) => { | ||
reject(error || new Error(`loadScript failed: ${src}`)) | ||
} | ||
if (async) s.async = true | ||
import { _objectAssign, isServerSide } from '@naturalcycles/js-lib' | ||
|
||
export type LoadScriptOptions = Partial<HTMLScriptElement> | ||
export type LoadCSSOptions = Partial<HTMLLinkElement> | ||
|
||
/** | ||
* opt.async defaults to `true`. | ||
* No other options are set by default. | ||
*/ | ||
export async function loadScript(src: string, opt?: LoadScriptOptions): Promise<void> { | ||
if (isServerSide()) return | ||
|
||
return await new Promise<void>((resolve, reject) => { | ||
const s = _objectAssign(document.createElement('script'), { | ||
src, | ||
async: true, | ||
...opt, | ||
onload: resolve as any, | ||
onerror: (_event, _source, _lineno, _colno, err) => { | ||
reject(err || new Error(`loadScript failed: ${src}`)) | ||
}, | ||
}) | ||
document.head.append(s) | ||
}) | ||
} | ||
|
||
/** | ||
* Default options: | ||
* rel: 'stylesheet' | ||
* | ||
* No other options are set by default. | ||
*/ | ||
export async function loadCSS(href: string, opt?: LoadCSSOptions): Promise<void> { | ||
if (isServerSide()) return | ||
|
||
return await new Promise<void>((resolve, reject) => { | ||
const link = _objectAssign(document.createElement('link'), { | ||
href, | ||
rel: 'stylesheet', | ||
// type seems to be unnecessary: https://stackoverflow.com/a/5409146/4919972 | ||
// type: 'text/css', | ||
...opt, | ||
onload: resolve as any, | ||
onerror: (_event, _source, _lineno, _colno, err) => { | ||
reject(err || new Error(`loadCSS failed: ${href}`)) | ||
}, | ||
}) | ||
|
||
document.head.append(link) | ||
}) | ||
} |