@@ -16,9 +16,11 @@ export type HeadersProvider = (
16
16
ctx : WaterfallContext ,
17
17
) => HeadersProps | Promise < HeadersProps > ;
18
18
19
+ export type HeadersOption = HeadersProps | string | HeadersProvider ;
20
+
19
21
export interface LoadHeadersOptions {
20
22
root ?: string ;
21
- headers ?: HeadersProps | string | HeadersProvider ;
23
+ headers ?: HeadersOption ;
22
24
}
23
25
24
26
export class LoadHeaders extends Feature < LoadHeadersOptions > {
@@ -29,27 +31,27 @@ export class LoadHeaders extends Feature<LoadHeadersOptions> {
29
31
private headersFileTimestamp = 0 ;
30
32
31
33
public apply ( { hooks } : UserscriptPluginInstance ) : void {
32
- hooks . init . tapPromise ( this . name , async ( compiler ) => {
33
- const { headers } = this . options ;
34
+ const { headers : headersOption } = this . options ;
34
35
36
+ hooks . init . tapPromise ( this . name , async ( compiler ) => {
35
37
this . defaultHeaders = Object . assign (
36
38
{ } ,
37
39
await this . loadFromPackage ( compiler ) ,
38
- typeof headers === 'object' ? headers : { } ,
40
+ typeof headersOption === 'object' ? headersOption : { } ,
39
41
) ;
40
42
} ) ;
41
43
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
+ ) ;
48
51
49
- if ( typeof this . options . headers === 'string' ) {
50
52
const headersFile = path . resolve (
51
53
this . options . root ?? compilation . compiler . context ,
52
- this . options . headers ,
54
+ headersOption ,
53
55
) ;
54
56
55
57
const ts = await getFileTimestampAsync ( headersFile ) ;
@@ -74,21 +76,19 @@ export class LoadHeaders extends Feature<LoadHeadersOptions> {
74
76
) ;
75
77
76
78
compilation . fileDependencies . add ( headersFile ) ;
77
- }
78
- } ) ;
79
+ } ) ;
80
+ }
79
81
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 ( ) => ( {
82
88
...this . defaultHeaders ,
83
89
...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
+ }
92
92
}
93
93
94
94
private async loadFromPackage ( {
@@ -131,6 +131,19 @@ export class LoadHeaders extends Feature<LoadHeadersOptions> {
131
131
) : Promise < HeadersProps > {
132
132
return readJSON < HeadersProps > ( headersFile , fs ) ;
133
133
}
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
+ }
134
147
}
135
148
136
149
interface PackageInfo {
0 commit comments