Skip to content

Commit 5f3ac04

Browse files
committed
chore: update
1 parent 2e9d24b commit 5f3ac04

File tree

11 files changed

+544
-154
lines changed

11 files changed

+544
-154
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
},
1515
"[css]": {
1616
"editor.defaultFormatter": "biomejs.biome"
17-
}
17+
},
18+
"cSpell.words": ["Rsbuild"]
1819
}

playground/rsbuild.config.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,60 @@ import { defineConfig } from '@rsbuild/core';
22
import { pluginReact } from '@rsbuild/plugin-react';
33
import { pluginAssetsRetry } from '../dist';
44

5+
export function createBlockMiddleware({ urlPrefix, blockNum, onBlock }) {
6+
let counter = 0;
7+
8+
return (req, res, next) => {
9+
if (req.url?.startsWith(urlPrefix)) {
10+
counter++;
11+
// if blockNum is 3, 1 2 3 would be blocked, 4 would be passed
12+
const isBlocked = counter % (blockNum + 1) !== 0;
13+
14+
if (isBlocked && onBlock) {
15+
onBlock({
16+
url: req.url,
17+
count: counter,
18+
timestamp: Date.now(),
19+
});
20+
}
21+
if (isBlocked) {
22+
res.statusCode = 404;
23+
}
24+
res.setHeader('block-async', counter);
25+
}
26+
next();
27+
};
28+
}
29+
530
export default defineConfig({
6-
plugins: [pluginAssetsRetry(), pluginReact()],
31+
dev: {
32+
setupMiddlewares: [
33+
(middlewares) => {
34+
middlewares.unshift(
35+
createBlockMiddleware({
36+
urlPrefix: '/static/js/async/src_AsyncCompTest_tsx.js',
37+
blockNum: 3,
38+
onBlock: ({ url, count }) => {
39+
console.info(`Blocked ${url} for the ${count}th time`);
40+
},
41+
}),
42+
);
43+
},
44+
],
45+
},
46+
plugins: [
47+
pluginAssetsRetry({
48+
minify: true,
49+
onRetry(context) {
50+
console.info('onRetry', context);
51+
},
52+
onSuccess(context) {
53+
console.info('onSuccess', context);
54+
},
55+
onFail(context) {
56+
console.info('onFail', context);
57+
},
58+
}),
59+
pluginReact(),
60+
],
761
});

src/AsyncChunkRetryPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ class AsyncChunkRetryPlugin implements Rspack.RspackPluginInstance {
5050
readonly name = 'ASYNC_CHUNK_RETRY_PLUGIN';
5151
readonly isRspack: boolean;
5252
readonly minify: boolean;
53-
readonly runtimeOptions: NormalizedRuntimeRetryOptions;
53+
readonly runtimeOptions: NormalizedRuntimeRetryOptions[];
5454

5555
constructor(
56-
options: NormalizedRuntimeRetryOptions,
56+
options: NormalizedRuntimeRetryOptions[],
5757
isRspack: boolean,
5858
minify: boolean,
5959
) {

src/index.ts

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { AsyncChunkRetryPlugin } from './AsyncChunkRetryPlugin.js';
1212
import type {
1313
NormalizedRuntimeRetryOptions,
1414
PluginAssetsRetryOptions,
15+
RuntimeRetryOptions,
1516
} from './types.js';
1617

1718
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -22,38 +23,48 @@ export const PLUGIN_ASSETS_RETRY_NAME = 'rsbuild:assets-retry';
2223

2324
function getRuntimeOptions(
2425
userOptions: PluginAssetsRetryOptions,
25-
): NormalizedRuntimeRetryOptions {
26-
const { inlineScript, minify, ...restOptions } = userOptions;
26+
defaultCrossOrigin: boolean | 'anonymous' | 'use-credentials',
27+
): NormalizedRuntimeRetryOptions[] {
28+
const { inlineScript, minify, ...runtimeOptions } = userOptions;
2729
const defaultOptions: NormalizedRuntimeRetryOptions = {
2830
max: 3,
2931
type: ['link', 'script', 'img'],
3032
domain: [],
31-
crossOrigin: false,
33+
crossOrigin: defaultCrossOrigin,
3234
delay: 0,
3335
addQuery: false,
3436
};
3537

36-
const result: NormalizedRuntimeRetryOptions = {
37-
...defaultOptions,
38-
...restOptions,
39-
};
38+
function normalizeOption(
39+
options: RuntimeRetryOptions,
40+
): NormalizedRuntimeRetryOptions {
41+
const result: NormalizedRuntimeRetryOptions = {
42+
...defaultOptions,
43+
...options,
44+
};
4045

41-
// Normalize config
42-
if (!Array.isArray(result.type) || result.type.length === 0) {
43-
result.type = defaultOptions.type;
44-
}
45-
if (!Array.isArray(result.domain) || result.domain.length === 0) {
46-
result.domain = defaultOptions.domain;
46+
// Normalize config
47+
if (!Array.isArray(result.type) || result.type.length === 0) {
48+
result.type = defaultOptions.type;
49+
}
50+
if (!Array.isArray(result.domain) || result.domain.length === 0) {
51+
result.domain = defaultOptions.domain;
52+
}
53+
if (Array.isArray(result.domain)) {
54+
result.domain = result.domain.filter(Boolean);
55+
}
56+
return result;
4757
}
48-
if (Array.isArray(result.domain)) {
49-
result.domain = result.domain.filter(Boolean);
58+
if ('rules' in runtimeOptions) {
59+
const result = runtimeOptions.rules.map((i) => normalizeOption(i));
60+
return result;
5061
}
5162

52-
return result;
63+
return [normalizeOption(runtimeOptions)];
5364
}
5465

5566
async function getRetryCode(
56-
runtimeOptions: NormalizedRuntimeRetryOptions,
67+
runtimeOptions: NormalizedRuntimeRetryOptions[],
5768
minify: boolean,
5869
): Promise<string> {
5970
const filename = 'initialChunkRetry';
@@ -79,38 +90,30 @@ export const pluginAssetsRetry = (
7990
return path.posix.join(distDir, `assets-retry.${PLUGIN_VERSION}.js`);
8091
};
8192

82-
const normalizeOptions = (
93+
const getDefaultValueFromRsbuildConfig = (
8394
config: NormalizedEnvironmentConfig,
84-
): PluginAssetsRetryOptions & {
95+
): {
8596
minify: boolean;
8697
crossorigin: boolean | 'anonymous' | 'use-credentials';
8798
} => {
88-
const options = { ...userOptions };
89-
90-
// options.crossOrigin should be same as html.crossorigin by default
91-
if (options.crossOrigin === undefined) {
92-
options.crossOrigin = config.html.crossorigin;
93-
}
94-
95-
if (options.minify === undefined) {
96-
const minify =
97-
typeof config.output.minify === 'boolean'
98-
? config.output.minify
99-
: config.output.minify?.js;
100-
options.minify = minify && config.mode === 'production';
101-
}
102-
103-
return options as PluginAssetsRetryOptions & {
104-
minify: boolean;
105-
crossorigin: boolean | 'anonymous' | 'use-credentials';
99+
const minify =
100+
typeof config.output.minify === 'boolean'
101+
? config.output.minify
102+
: config.output.minify?.js;
103+
104+
return {
105+
crossorigin: config.html.crossorigin,
106+
minify: Boolean(minify) && config.mode === 'production',
106107
};
107108
};
108109

109110
if (inlineScript) {
110111
api.modifyHTMLTags(async ({ headTags, bodyTags }, { environment }) => {
111-
const options = normalizeOptions(environment.config);
112-
const runtimeOptions = getRuntimeOptions(options);
113-
const code = await getRetryCode(runtimeOptions, options.minify);
112+
const { minify, crossorigin } = getDefaultValueFromRsbuildConfig(
113+
environment.config,
114+
);
115+
const runtimeOptions = getRuntimeOptions(userOptions, crossorigin);
116+
const code = await getRetryCode(runtimeOptions, minify);
114117

115118
headTags.unshift({
116119
tag: 'script',
@@ -141,9 +144,11 @@ export const pluginAssetsRetry = (
141144
{ stage: 'additional' },
142145
async ({ sources, compilation, environment }) => {
143146
const scriptPath = getScriptPath(environment);
144-
const options = normalizeOptions(environment.config);
145-
const runtimeOptions = getRuntimeOptions(options);
146-
const code = await getRetryCode(runtimeOptions, options.minify);
147+
const { crossorigin, minify } = getDefaultValueFromRsbuildConfig(
148+
environment.config,
149+
);
150+
const runtimeOptions = getRuntimeOptions(userOptions, crossorigin);
151+
const code = await getRetryCode(runtimeOptions, minify);
147152
compilation.emitAsset(scriptPath, new sources.RawSource(code));
148153
},
149154
);
@@ -156,13 +161,13 @@ export const pluginAssetsRetry = (
156161
return;
157162
}
158163

159-
const options = normalizeOptions(config);
160-
const runtimeOptions = getRuntimeOptions(options);
164+
const { crossorigin, minify } = getDefaultValueFromRsbuildConfig(config);
165+
const runtimeOptions = getRuntimeOptions(userOptions, crossorigin);
161166
const isRspack = api.context.bundlerType === 'rspack';
162167

163168
chain
164169
.plugin('async-chunk-retry')
165-
.use(AsyncChunkRetryPlugin, [runtimeOptions, isRspack, options.minify]);
170+
.use(AsyncChunkRetryPlugin, [runtimeOptions, isRspack, minify]);
166171
});
167172
},
168173
});

0 commit comments

Comments
 (0)