@@ -12,6 +12,7 @@ import { AsyncChunkRetryPlugin } from './AsyncChunkRetryPlugin.js';
1212import type {
1313 NormalizedRuntimeRetryOptions ,
1414 PluginAssetsRetryOptions ,
15+ RuntimeRetryOptions ,
1516} from './types.js' ;
1617
1718const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
@@ -22,38 +23,48 @@ export const PLUGIN_ASSETS_RETRY_NAME = 'rsbuild:assets-retry';
2223
2324function 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
5566async 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