@@ -12,6 +12,7 @@ import { AsyncChunkRetryPlugin } from './AsyncChunkRetryPlugin.js';
12
12
import type {
13
13
NormalizedRuntimeRetryOptions ,
14
14
PluginAssetsRetryOptions ,
15
+ RuntimeRetryOptions ,
15
16
} from './types.js' ;
16
17
17
18
const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
@@ -22,38 +23,48 @@ export const PLUGIN_ASSETS_RETRY_NAME = 'rsbuild:assets-retry';
22
23
23
24
function getRuntimeOptions (
24
25
userOptions : PluginAssetsRetryOptions ,
25
- ) : NormalizedRuntimeRetryOptions {
26
- const { inlineScript, minify, ...restOptions } = userOptions ;
26
+ defaultCrossOrigin : boolean | 'anonymous' | 'use-credentials' ,
27
+ ) : NormalizedRuntimeRetryOptions [ ] {
28
+ const { inlineScript, minify, ...runtimeOptions } = userOptions ;
27
29
const defaultOptions : NormalizedRuntimeRetryOptions = {
28
30
max : 3 ,
29
31
type : [ 'link' , 'script' , 'img' ] ,
30
32
domain : [ ] ,
31
- crossOrigin : false ,
33
+ crossOrigin : defaultCrossOrigin ,
32
34
delay : 0 ,
33
35
addQuery : false ,
34
36
} ;
35
37
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
+ } ;
40
45
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 ;
47
57
}
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 ;
50
61
}
51
62
52
- return result ;
63
+ return [ normalizeOption ( runtimeOptions ) ] ;
53
64
}
54
65
55
66
async function getRetryCode (
56
- runtimeOptions : NormalizedRuntimeRetryOptions ,
67
+ runtimeOptions : NormalizedRuntimeRetryOptions [ ] ,
57
68
minify : boolean ,
58
69
) : Promise < string > {
59
70
const filename = 'initialChunkRetry' ;
@@ -79,38 +90,30 @@ export const pluginAssetsRetry = (
79
90
return path . posix . join ( distDir , `assets-retry.${ PLUGIN_VERSION } .js` ) ;
80
91
} ;
81
92
82
- const normalizeOptions = (
93
+ const getDefaultValueFromRsbuildConfig = (
83
94
config : NormalizedEnvironmentConfig ,
84
- ) : PluginAssetsRetryOptions & {
95
+ ) : {
85
96
minify : boolean ;
86
97
crossorigin : boolean | 'anonymous' | 'use-credentials' ;
87
98
} => {
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' ,
106
107
} ;
107
108
} ;
108
109
109
110
if ( inlineScript ) {
110
111
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 ) ;
114
117
115
118
headTags . unshift ( {
116
119
tag : 'script' ,
@@ -141,9 +144,11 @@ export const pluginAssetsRetry = (
141
144
{ stage : 'additional' } ,
142
145
async ( { sources, compilation, environment } ) => {
143
146
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 ) ;
147
152
compilation . emitAsset ( scriptPath , new sources . RawSource ( code ) ) ;
148
153
} ,
149
154
) ;
@@ -156,13 +161,13 @@ export const pluginAssetsRetry = (
156
161
return ;
157
162
}
158
163
159
- const options = normalizeOptions ( config ) ;
160
- const runtimeOptions = getRuntimeOptions ( options ) ;
164
+ const { crossorigin , minify } = getDefaultValueFromRsbuildConfig ( config ) ;
165
+ const runtimeOptions = getRuntimeOptions ( userOptions , crossorigin ) ;
161
166
const isRspack = api . context . bundlerType === 'rspack' ;
162
167
163
168
chain
164
169
. plugin ( 'async-chunk-retry' )
165
- . use ( AsyncChunkRetryPlugin , [ runtimeOptions , isRspack , options . minify ] ) ;
170
+ . use ( AsyncChunkRetryPlugin , [ runtimeOptions , isRspack , minify ] ) ;
166
171
} ) ;
167
172
} ,
168
173
} ) ;
0 commit comments