Skip to content

Commit 9d200e2

Browse files
authored
chore(type): extract the shared type (#19)
1 parent 98a9918 commit 9d200e2

File tree

4 files changed

+18
-24
lines changed

4 files changed

+18
-24
lines changed

README.zh-CN.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ bun add @rsbuild/plugin-assets-retry -D
3535
你可以在 `rsbuild.config.ts` 文件中注册插件:
3636

3737
```ts
38-
import { pluginAssetsRetry } from "@rsbuild/plugin-assets-retry";
38+
import { pluginAssetsRetry } from '@rsbuild/plugin-assets-retry';
3939

4040
export default {
4141
plugins: [pluginAssetsRetry()],
@@ -62,7 +62,7 @@ type AssetsRetryOptions = {
6262
domain?: string[];
6363
max?: number;
6464
test?: string | ((url: string) => boolean);
65-
crossOrigin?: boolean | "anonymous" | "use-credentials";
65+
crossOrigin?: boolean | 'anonymous' | 'use-credentials';
6666
inlineScript?: boolean;
6767
delay?: number | ((context: AssetsRetryHookContext) => number);
6868
onRetry?: (context: AssetsRetryHookContext) => void;
@@ -75,10 +75,10 @@ type AssetsRetryOptions = {
7575

7676
```ts
7777
const defaultOptions = {
78-
type: ["script", "link", "img"],
78+
type: ['script', 'link', 'img'],
7979
domain: [],
8080
max: 3,
81-
test: "",
81+
test: '',
8282
crossOrigin: false,
8383
delay: 0,
8484
onRetry: () => {},
@@ -101,11 +101,11 @@ const defaultOptions = {
101101
defineConfig({
102102
plugins: [
103103
pluginAssetsRetry({
104-
domain: ["cdn1.com", "cdn2.com", "cdn3.com"],
105-
})
104+
domain: ['cdn1.com', 'cdn2.com', 'cdn3.com'],
105+
}),
106106
],
107107
output: {
108-
assetPrefix: "https://cdn1.com", // 或者 "//cdn1.com"
108+
assetPrefix: 'https://cdn1.com', // 或者 "//cdn1.com"
109109
},
110110
});
111111
```
@@ -125,7 +125,7 @@ defineConfig({
125125

126126
```js
127127
pluginAssetsRetry({
128-
type: ["script", "link"],
128+
type: ['script', 'link'],
129129
});
130130
```
131131

@@ -180,7 +180,7 @@ pluginAssetsRetry({
180180
pluginAssetsRetry({
181181
onRetry: ({ times, domain, url, tagName, isAsyncChunk }) => {
182182
console.log(
183-
`Retry ${times} times, domain: ${domain}, url: ${url}, tagName: ${tagName}, isAsyncChunk: ${isAsyncChunk}`
183+
`Retry ${times} times, domain: ${domain}, url: ${url}, tagName: ${tagName}, isAsyncChunk: ${isAsyncChunk}`,
184184
);
185185
},
186186
});
@@ -196,7 +196,7 @@ pluginAssetsRetry({
196196
pluginAssetsRetry({
197197
onSuccess: ({ times, domain, url, tagName, isAsyncChunk }) => {
198198
console.log(
199-
`Retry ${times} times, domain: ${domain}, url: ${url}, tagName: ${tagName}, isAsyncChunk: ${isAsyncChunk}`
199+
`Retry ${times} times, domain: ${domain}, url: ${url}, tagName: ${tagName}, isAsyncChunk: ${isAsyncChunk}`,
200200
);
201201
},
202202
});
@@ -212,7 +212,7 @@ pluginAssetsRetry({
212212
pluginAssetsRetry({
213213
onFail: ({ times, domain, url, tagName, isAsyncChunk }) => {
214214
console.log(
215-
`Retry ${times} times, domain: ${domain}, url: ${url}, tagName: ${tagName}, isAsyncChunk: ${isAsyncChunk}`
215+
`Retry ${times} times, domain: ${domain}, url: ${url}, tagName: ${tagName}, isAsyncChunk: ${isAsyncChunk}`,
216216
);
217217
},
218218
});
@@ -319,7 +319,7 @@ pluginAssetsRetry({
319319
```js
320320
// 通过次数来计算延迟时间
321321
pluginAssetsRetry({
322-
delay: (ctx) => (ctx.times + 1) * 1000,
322+
delay: ctx => (ctx.times + 1) * 1000,
323323
});
324324
```
325325

@@ -334,12 +334,12 @@ pluginAssetsRetry({
334334
以下是一个错误示例:
335335

336336
```js
337-
import { someMethod } from "utils";
337+
import { someMethod } from 'utils';
338338

339339
pluginAssetsRetry({
340340
onRetry() {
341341
// 错误用法,包含了敏感信息
342-
const privateToken = "a-private-token";
342+
const privateToken = 'a-private-token';
343343

344344
// 错误用法,使用了外部的方法
345345
someMethod(privateToken);

src/runtime/asyncChunkRetry.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ declare global {
5555
var __RUNTIME_GLOBALS_RSBUILD_LOAD_STYLESHEET__: LoadStyleSheet;
5656
// RuntimeGlobals.publicPath
5757
var __RUNTIME_GLOBALS_PUBLIC_PATH__: string;
58-
// user options
59-
var __RETRY_OPTIONS__: NormalizedRuntimeRetryOptions;
60-
// global variables shared with initial chunk retry runtime
61-
var __RB_ASYNC_CHUNKS__: Record<ChunkFilename, boolean>;
6258
}
6359

6460
// init retryCollector and nextRetry function

src/runtime/initialChunkRetry.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ const TAG_TYPE: { [propName: string]: new () => HTMLElement } = {
2121
img: HTMLImageElement,
2222
};
2323

24-
declare global {
25-
// global variables shared with async chunk
26-
var __RB_ASYNC_CHUNKS__: Record<string, boolean>;
27-
var __RETRY_OPTIONS__: NormalizedRuntimeRetryOptions;
28-
}
29-
3024
function getRequestUrl(element: HTMLElement) {
3125
if (
3226
element instanceof HTMLScriptElement ||

src/runtime/runtime.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
declare type CrossOrigin = import('@rsbuild/core').CrossOrigin;
44
declare type AssetsRetryHookContext = import('../types.js').AssetsRetryHookContext;
55
declare type NormalizedRuntimeRetryOptions = import('../types.js').NormalizedRuntimeRetryOptions;
6+
7+
// global variables shared between initialChunkRetry and asyncChunkRetry
8+
var __RB_ASYNC_CHUNKS__: Record<string, boolean>;
9+
var __RETRY_OPTIONS__: NormalizedRuntimeRetryOptions;

0 commit comments

Comments
 (0)