-
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.
- Loading branch information
Showing
7 changed files
with
202 additions
and
2 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
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,3 +1,6 @@ | ||
/** | ||
* @vitest-environment happy-dom | ||
*/ | ||
import { deepClone } from './deepClone' | ||
|
||
describe('deepClone', () => { | ||
|
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { loadScript } from './loadScript' | ||
|
||
// TODO 每个用户单独跑没问题,但一直跑就会报错 | ||
describe.todo('loadScript', () => { | ||
afterEach(() => { | ||
vi.resetAllMocks() | ||
document.body.innerHTML = '' | ||
document.head.innerHTML = '' | ||
}) | ||
const src = 'https://unpkg.com/@plugin-web-update-notification/core@1.3.1/dist/webUpdateNoticeInjectScript.js' | ||
const getScriptTag = (): HTMLScriptElement | null => | ||
document.head.querySelector(`script[src="${src}"]`) | ||
|
||
it('call unload, should remove script tag', async () => { | ||
const removeListener = vi.spyOn(Element.prototype, 'remove') | ||
const { unload } = loadScript(src) | ||
expect(getScriptTag()).not.toBeNull() | ||
unload() | ||
expect(getScriptTag()).toBeNull() | ||
expect(removeListener).toHaveBeenCalled() | ||
}) | ||
|
||
it('should add script tag', async () => { | ||
const appendChildListener = vitest.spyOn(document.head, 'appendChild') | ||
const { scriptTag, unload } = loadScript(src) | ||
expect(scriptTag).instanceof(HTMLScriptElement) | ||
expect(scriptTag?.src).toBe(src) | ||
expect(getScriptTag()).toBe(scriptTag) | ||
expect(appendChildListener).toHaveBeenCalled() | ||
unload() | ||
}) | ||
|
||
it('should re-use the same src for multiple loads', async () => { | ||
const addChildListener = vitest.spyOn(document.head, 'appendChild') | ||
|
||
expect(addChildListener).not.toBeCalled() | ||
expect(getScriptTag()).toBeNull() | ||
|
||
const { scriptTag: scriptTag1 } = loadScript(src) | ||
const { scriptTag: scriptTag2, unload } = loadScript(src) | ||
|
||
expect(scriptTag1).not.toBeNull() | ||
expect(scriptTag2).not.toBeNull() | ||
expect(document.querySelectorAll(`script[src="${src}"]`)).toHaveLength(1) | ||
|
||
expect(addChildListener).toBeCalledTimes(1) | ||
expect(getScriptTag()).toBeInstanceOf(HTMLScriptElement) | ||
unload() | ||
}) | ||
|
||
it('should support attributes', async () => { | ||
const { unload } = loadScript(src, { | ||
attrs: { 'id': 'id-value', 'data-test': 'data-test-value' }, | ||
defer: true, | ||
crossOrigin: 'anonymous', | ||
}) | ||
|
||
const element = getScriptTag() | ||
// expect(element).toBeInstanceOf(HTMLScriptElement) | ||
expect(element?.getAttribute('id')).toBe('id-value') | ||
expect(element?.getAttribute('data-test')).toBe('data-test-value') | ||
expect(element?.defer).toBe(true) | ||
expect(element?.crossOrigin).toBe('anonymous') | ||
unload() | ||
}) | ||
it('status change', async () => { | ||
let status = '' | ||
const onStatusChange = vi.fn((status_) => { | ||
status = status_ | ||
}) | ||
const { unload } = loadScript(src, { onStatusChange }) | ||
expect(status).toBe('loading') | ||
expect(onStatusChange).toBeCalledTimes(1) | ||
unload() | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
interface LoadScriptOptions { | ||
/** | ||
* Add `async` attribute to the script tag | ||
* @default true | ||
*/ | ||
async?: boolean | ||
/** | ||
* Add `defer` attribute to the script tag | ||
*/ | ||
defer?: boolean | ||
/** | ||
* Script type | ||
* | ||
* @default 'text/javascript' | ||
* @see https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/script#attr-type | ||
*/ | ||
type?: string | ||
crossOrigin?: 'anonymous' | 'use-credentials' | ||
/** | ||
* @see https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/script#attr-referrerpolicy | ||
*/ | ||
referrerPolicy?: 'no-referrer' | 'no-referrer-when-downgrade' | 'origin' | 'origin-when-cross-origin' | 'same-origin' | 'strict-origin' | 'strict-origin-when-cross-origin' | 'unsafe-url' | ||
noModule?: boolean | ||
/** | ||
* Add custom attribute to the script tag | ||
* | ||
*/ | ||
attrs?: Record<string, string> | ||
/** | ||
* Append the script tag to the document.head or document.body | ||
* @default 'head' | ||
*/ | ||
appendPosition?: 'head' | 'body' | ||
onStatusChange?: (status: 'loading' | 'loaded' | 'error') => void | ||
} | ||
|
||
export function loadScript(src: string, options?: LoadScriptOptions) { | ||
const { | ||
async = true, | ||
defer, | ||
type = 'text/javascript', | ||
crossOrigin, | ||
referrerPolicy, | ||
attrs = {}, | ||
noModule, | ||
appendPosition = 'head', | ||
onStatusChange, | ||
} = options || {} | ||
let scriptTag = document.querySelector<HTMLScriptElement>(`script[src="${src}"]`) | ||
|
||
// if the script is exist, it has high probability loaded | ||
if (scriptTag) | ||
onStatusChange?.('loaded') | ||
|
||
if (!scriptTag) { | ||
scriptTag = document.createElement('script') | ||
scriptTag.src = src | ||
scriptTag.type = type | ||
scriptTag.async = async | ||
|
||
if (defer) | ||
scriptTag.defer = defer | ||
if (crossOrigin) | ||
scriptTag.crossOrigin = crossOrigin | ||
if (referrerPolicy) | ||
scriptTag.referrerPolicy = referrerPolicy | ||
if (noModule) | ||
scriptTag.noModule = noModule | ||
Object.entries(attrs).forEach(([name, value]) => scriptTag?.setAttribute(name, value)) | ||
|
||
const appendTarget = appendPosition === 'head' ? document.head : document.body | ||
appendTarget.appendChild(scriptTag) | ||
onStatusChange?.('loading') | ||
} | ||
|
||
scriptTag?.addEventListener('load', () => { | ||
onStatusChange?.('loaded') | ||
}) | ||
scriptTag?.addEventListener('error', () => { | ||
onStatusChange?.('error') | ||
}) | ||
scriptTag?.addEventListener('abort', () => { | ||
onStatusChange?.('error') | ||
}) | ||
|
||
return { | ||
/** remove the script tag */ | ||
unload: () => unload(src), | ||
scriptTag, | ||
} | ||
} | ||
|
||
/** remove the script tag */ | ||
function unload(src: string) { | ||
const script = document.querySelector(`script[src="${src}"]`) | ||
if (script) | ||
script.remove() | ||
} |