@@ -16,9 +16,11 @@ export type HeadersProvider = (
1616 ctx : WaterfallContext ,
1717) => HeadersProps | Promise < HeadersProps > ;
1818
19+ export type HeadersOption = HeadersProps | string | HeadersProvider ;
20+
1921export interface LoadHeadersOptions {
2022 root ?: string ;
21- headers ?: HeadersProps | string | HeadersProvider ;
23+ headers ?: HeadersOption ;
2224}
2325
2426export class LoadHeaders extends Feature < LoadHeadersOptions > {
@@ -29,27 +31,27 @@ export class LoadHeaders extends Feature<LoadHeadersOptions> {
2931 private headersFileTimestamp = 0 ;
3032
3133 public apply ( { hooks } : UserscriptPluginInstance ) : void {
32- hooks . init . tapPromise ( this . name , async ( compiler ) => {
33- const { headers } = this . options ;
34+ const { headers : headersOption } = this . options ;
3435
36+ hooks . init . tapPromise ( this . name , async ( compiler ) => {
3537 this . defaultHeaders = Object . assign (
3638 { } ,
3739 await this . loadFromPackage ( compiler ) ,
38- typeof headers === 'object' ? headers : { } ,
40+ typeof headersOption === 'object' ? headersOption : { } ,
3941 ) ;
4042 } ) ;
4143
42- hooks . preprocess . tapPromise ( this . name , async ( compilation ) => {
43- const getFileTimestampAsync = promisify (
44- compilation . fileSystemInfo . getFileTimestamp . bind (
45- compilation . fileSystemInfo ,
46- ) ,
47- ) ;
44+ if ( typeof headersOption === 'string' ) {
45+ hooks . preprocess . tapPromise ( this . name , async ( compilation ) => {
46+ const getFileTimestampAsync = promisify (
47+ compilation . fileSystemInfo . getFileTimestamp . bind (
48+ compilation . fileSystemInfo ,
49+ ) ,
50+ ) ;
4851
49- if ( typeof this . options . headers === 'string' ) {
5052 const headersFile = path . resolve (
5153 this . options . root ?? compilation . compiler . context ,
52- this . options . headers ,
54+ headersOption ,
5355 ) ;
5456
5557 const ts = await getFileTimestampAsync ( headersFile ) ;
@@ -74,21 +76,19 @@ export class LoadHeaders extends Feature<LoadHeadersOptions> {
7476 ) ;
7577
7678 compilation . fileDependencies . add ( headersFile ) ;
77- }
78- } ) ;
79+ } ) ;
80+ }
7981
80- hooks . headers . tapPromise ( this . name , async ( _ , ctx ) => {
81- const headers = {
82+ if ( typeof headersOption === 'function' ) {
83+ hooks . headers . tapPromise ( this . name , ( _ , ctx ) =>
84+ this . loadFromHeadersProvider ( headersOption , ctx ) ,
85+ ) ;
86+ } else {
87+ hooks . headers . tapPromise ( this . name , async ( ) => ( {
8288 ...this . defaultHeaders ,
8389 ...this . fileHeaders ,
84- } ;
85-
86- if ( typeof this . options . headers === 'function' ) {
87- return await this . options . headers ( headers , ctx ) ;
88- }
89-
90- return headers ;
91- } ) ;
90+ } ) ) ;
91+ }
9292 }
9393
9494 private async loadFromPackage ( {
@@ -131,6 +131,19 @@ export class LoadHeaders extends Feature<LoadHeadersOptions> {
131131 ) : Promise < HeadersProps > {
132132 return readJSON < HeadersProps > ( headersFile , fs ) ;
133133 }
134+
135+ private async loadFromHeadersProvider (
136+ headersProvider : HeadersProvider ,
137+ ctx : WaterfallContext ,
138+ ) : Promise < HeadersProps > {
139+ return headersProvider (
140+ {
141+ ...this . defaultHeaders ,
142+ ...this . fileHeaders ,
143+ } ,
144+ ctx ,
145+ ) ;
146+ }
134147}
135148
136149interface PackageInfo {
0 commit comments