@@ -2501,7 +2501,7 @@ Type:
25012501` ` ` ts
25022502type Preload = Array < {
25032503 test: RegExp ;
2504- filter?: AdvancedFilter ;
2504+ filter?: PreloadFilter ;
25052505 as?: string;
25062506 rel?: string;
25072507 type?: string;
@@ -2510,11 +2510,11 @@ type Preload = Array<{
25102510` ` `
25112511
25122512` ` ` ts
2513- type AdvancedFilter =
2513+ type PreloadFilter =
25142514 | RegExp
25152515 | Array < RegExp >
25162516 | { includes?: Array < RegExp > ; excludes?: Array < RegExp > }
2517- | ((value : string ) => void | true | false );
2517+ | ((asset : { sourceFiles : Array < string> ; outputFile : string } ) => void | boolean );
25182518` ` `
25192519
25202520Default: ` null `
@@ -2572,9 +2572,12 @@ For example:
25722572By default, all files matching the ` test` option will be preloaded.
25732573You can use the ` filter` to preload specific files individually.
25742574
2575- The ` AdvancedFilter ` type provides a versatile way to define filtering logic,
2575+ The ` PreloadFilter ` type provides a versatile way to define filtering logic,
25762576from simple patterns to complex inclusion and exclusion criteria.
25772577
2578+ The filter RegExp matches both the source and output files.
2579+ If you need to match source or output files separately use the filter function, see below.
2580+
25782581Here's are supported filter formats:
25792582
25802583- ` RegExp ` \
@@ -2607,12 +2610,40 @@ Here's are supported filter formats:
26072610 excludes: [/ exclude-this/ ,],
26082611 }
26092612 ` ` `
2610- - ` (value : string ) => void | false ` \
2613+ - ` (asset : { sourceFiles : Array < string> ; outputFile : string } ) => void | boolean ` \
26112614 Custom logic provides maximum flexibility for filtering that cannot be expressed with regular expressions.\
2612- A custom filter function takes an output asset file as input and decides whether it matches based on the return value:
2615+ A custom filter function takes the source files and the output asset file as input, and decides whether they match based on the return value:
26132616 - Returns ` void ` (` undefined ` ) or ` true ` if the value passes the filter.
26142617 - Returns ` false ` if the value fails the filter.
26152618
2619+ > [!NOTE]
2620+ > Only ` style` type may have many files in the ` sourceFiles` .\
2621+ > If many styles are imported in one JS file, then all extracted CSS will be squashed into single CSS file with the name of a JS parent file.
2622+ > In this case the ` sourceFiles` will have many files.
2623+ >
2624+ > Usage for ` style` type:
2625+ > ` ` ` js
2626+ > {
2627+ > test: / \. (s? css| less)$ / ,
2628+ > filter : ({ sourceFiles, outputFile }) => {
2629+ > return ! sourceFiles .some ((sourceFile ) => / noPreload/ .test (sourceFile));
2630+ > },
2631+ > as: ' style' ,
2632+ > },
2633+ > ` ` `
2634+ >
2635+ > All other types have only one file in the ` sourceFiles` array.
2636+ > You can use the argument destructor to extract only first source file.
2637+ >
2638+ > Usage for all other types:
2639+ > ` ` ` js
2640+ > {
2641+ > test: / \. (js| ts)$ / ,
2642+ > filter : ({ sourceFiles: [sourceFile], outputFile }) => ! / noPreload/ .test (outputFile),
2643+ > as: ' script' ,
2644+ > },
2645+ > ` ` `
2646+
26162647For example, preload all JS files except dynamically imported (async chunks).
26172648
26182649There is the _app.js_:
@@ -2634,6 +2665,7 @@ preload: [
26342665 {
26352666 test: / \. (js| ts)$ / ,
26362667 filter: {
2668+ // matches source and output files
26372669 excludes: [/ asyncChunk/ ],
26382670 },
26392671 as: ' script' ,
@@ -2646,7 +2678,7 @@ The same effect using the `filter` function:
26462678preload: [
26472679 {
26482680 test: / \. (js| ts)$ / ,
2649- filter : (assetFile ) => ! / asyncChunk/ .test (assetFile ),
2681+ filter : ({ sourceFiles : [sourceFile], outputFile } ) => ! / asyncChunk/ .test (sourceFile) ),
26502682 as: ' script' ,
26512683 },
26522684],
0 commit comments