Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useExternal): avoid wrong option #2011

Merged
merged 5 commits into from
Feb 16, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions packages/hooks/src/useExternal/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { useEffect, useRef, useState } from 'react';

export type JsOptions = {
type?: 'js';
type JsOptions = {
type: 'js';
js?: Partial<HTMLScriptElement>;
};

export type CssOptions = {
type?: 'css';
type CssOptions = {
type: 'css';
css?: Partial<HTMLStyleElement>;
};

export type Options =
| CssOptions
| JsOptions
| {
type: never;
js?: Partial<HTMLScriptElement>;
css?: Partial<HTMLStyleElement>;
};
type DefaultOptions = {
type?: 'js' | 'css';
js?: Partial<HTMLScriptElement>;
css?: Partial<HTMLStyleElement>;
};

type OptionsType = 'js' | 'css' | undefined;
type Opt<T> = T extends 'js' ? JsOptions : T extends 'css' ? CssOptions : DefaultOptions;
export type Options = Opt<OptionsType>;

// {[path]: count}
// remove external when no used
@@ -100,11 +101,11 @@ const useExternal = (path?: string, options?: Options) => {
}
const pathname = path.replace(/[|#].*$/, '');
if (options?.type === 'css' || (!options?.type && /(^css!|\.css$)/.test(pathname))) {
const result = loadCss(path, (options as CssOptions)?.css);
const result = loadCss(path, options?.css);
ref.current = result.ref;
setStatus(result.status);
} else if (options?.type === 'js' || (!options?.type && /(^js!|\.js$)/.test(pathname))) {
const result = loadScript(path, (options as JsOptions)?.js);
const result = loadScript(path, options?.js);
ref.current = result.ref;
setStatus(result.status);
} else {