@@ -16,12 +16,12 @@ import {
16
16
PAGE_DATA_DIR ,
17
17
} from './constants'
18
18
19
- const getHeaderName = ( header ) => {
19
+ const getHeaderName = ( header : any ) => {
20
20
const matches = header . match ( / ^ ( [ ^ : ] + ) : / )
21
21
return matches && matches [ 1 ]
22
22
}
23
23
24
- const validHeaders = ( headers , reporter ) => {
24
+ const validHeaders = ( headers : any , reporter : any ) => {
25
25
if ( ! headers || ! _ . isObject ( headers ) ) {
26
26
return false
27
27
}
@@ -40,20 +40,20 @@ const validHeaders = (headers, reporter) => {
40
40
)
41
41
}
42
42
43
- const linkTemplate = ( assetPath , type = `script` ) =>
43
+ const linkTemplate = ( assetPath : any , type = `script` ) =>
44
44
`Link: <${ assetPath } >; rel=preload; as=${ type } ${ type === `fetch` ? `; crossorigin` : `` } `
45
45
46
- const pathChunkName = ( path ) => {
46
+ const pathChunkName = ( path : any ) => {
47
47
const name = path === `/` ? `index` : kebabHash ( path )
48
48
return `path---${ name } `
49
49
}
50
50
51
- const getPageDataPath = ( path ) => {
51
+ const getPageDataPath = ( path : any ) => {
52
52
const fixedPagePath = path === `/` ? `index` : path
53
53
return posix . join ( `page-data` , fixedPagePath , `page-data.json` )
54
54
}
55
55
56
- const getScriptPath = ( file , manifest ) => {
56
+ const getScriptPath = ( file : any , manifest : any ) => {
57
57
const chunk = manifest [ file ]
58
58
59
59
if ( ! chunk ) {
@@ -71,14 +71,19 @@ const getScriptPath = (file, manifest) => {
71
71
} )
72
72
}
73
73
74
- const getLinkHeaders = ( filesByType , pathPrefix ) =>
75
- Object . entries ( filesByType ) . flatMap ( ( [ type , files ] ) =>
74
+ const getLinkHeaders = ( filesByType : any , pathPrefix : any ) =>
75
+ Object . entries ( filesByType ) . flatMap ( ( [ type , files ] : [ string , Array < string > ] ) =>
76
76
files . map ( ( file ) => linkTemplate ( `${ pathPrefix } /${ file } ` , type ) ) ,
77
77
)
78
78
79
- const headersPath = ( pathPrefix , path ) => `${ pathPrefix } ${ path } `
79
+ const headersPath = ( pathPrefix : any , path : any ) => `${ pathPrefix } ${ path } `
80
80
81
- const preloadHeadersByPage = ( { pages, manifest, pathPrefix, publicFolder } ) => {
81
+ const preloadHeadersByPage = ( {
82
+ pages,
83
+ manifest,
84
+ pathPrefix,
85
+ publicFolder
86
+ } : any ) => {
82
87
const linksByPage = { }
83
88
84
89
const appDataPath = publicFolder ( PAGE_DATA_DIR , `app-data.json` )
@@ -91,7 +96,7 @@ const preloadHeadersByPage = ({ pages, manifest, pathPrefix, publicFolder }) =>
91
96
hasPageData = existsSync ( pageDataPath )
92
97
}
93
98
94
- pages . forEach ( ( page ) => {
99
+ pages . forEach ( ( page : any ) => {
95
100
const scripts = _ . flatMap ( COMMON_BUNDLES , ( file ) => getScriptPath ( file , manifest ) )
96
101
scripts . push (
97
102
...getScriptPath ( pathChunkName ( page . path ) , manifest ) ,
@@ -119,8 +124,8 @@ const preloadHeadersByPage = ({ pages, manifest, pathPrefix, publicFolder }) =>
119
124
return linksByPage
120
125
}
121
126
122
- const defaultMerge = ( ...headers ) => {
123
- const unionMerge = ( objValue , srcValue ) => {
127
+ const defaultMerge = ( ...headers : any [ ] ) => {
128
+ const unionMerge = ( objValue : any , srcValue : any ) => {
124
129
if ( Array . isArray ( objValue ) ) {
125
130
return _ . union ( objValue , srcValue )
126
131
}
@@ -130,18 +135,18 @@ const defaultMerge = (...headers) => {
130
135
return _ . mergeWith ( { } , ...headers , unionMerge )
131
136
}
132
137
133
- const headersMerge = ( userHeaders , defaultHeaders ) => {
138
+ const headersMerge = ( userHeaders : any , defaultHeaders : any ) => {
134
139
const merged = { }
135
140
Object . keys ( defaultHeaders ) . forEach ( ( path ) => {
136
141
if ( ! userHeaders [ path ] ) {
137
142
merged [ path ] = defaultHeaders [ path ]
138
143
return
139
144
}
140
145
const headersMap = { }
141
- defaultHeaders [ path ] . forEach ( ( header ) => {
146
+ defaultHeaders [ path ] . forEach ( ( header : any ) => {
142
147
headersMap [ getHeaderName ( header ) ] = header
143
148
} )
144
- userHeaders [ path ] . forEach ( ( header ) => {
149
+ userHeaders [ path ] . forEach ( ( header : any ) => {
145
150
// override if exists
146
151
headersMap [ getHeaderName ( header ) ] = header
147
152
} )
@@ -155,34 +160,32 @@ const headersMerge = (userHeaders, defaultHeaders) => {
155
160
return merged
156
161
}
157
162
158
- const transformLink = ( manifest , publicFolder , pathPrefix ) => ( header ) =>
159
- header . replace ( LINK_REGEX , ( __ , prefix , file , suffix ) => {
160
- const hashed = manifest [ file ]
161
- if ( hashed ) {
162
- return `${ prefix } ${ pathPrefix } ${ hashed } ${ suffix } `
163
- }
164
- if ( existsSync ( publicFolder ( file ) ) ) {
165
- return `${ prefix } ${ pathPrefix } ${ file } ${ suffix } `
166
- }
167
- throw new Error (
168
- `Could not find the file specified in the Link header \`${ header } \`.` +
169
- `The gatsby-plugin-netlify is looking for a matching file (with or without a ` +
170
- `webpack hash). Check the public folder and your gatsby-config.js to ensure you are ` +
171
- `pointing to a public file.` ,
172
- )
173
- } )
163
+ const transformLink = ( manifest : any , publicFolder : any , pathPrefix : any ) => ( header : any ) => header . replace ( LINK_REGEX , ( __ : any , prefix : any , file : any , suffix : any ) => {
164
+ const hashed = manifest [ file ]
165
+ if ( hashed ) {
166
+ return `${ prefix } ${ pathPrefix } ${ hashed } ${ suffix } `
167
+ }
168
+ if ( existsSync ( publicFolder ( file ) ) ) {
169
+ return `${ prefix } ${ pathPrefix } ${ file } ${ suffix } `
170
+ }
171
+ throw new Error (
172
+ `Could not find the file specified in the Link header \`${ header } \`.` +
173
+ `The gatsby-plugin-netlify is looking for a matching file (with or without a ` +
174
+ `webpack hash). Check the public folder and your gatsby-config.js to ensure you are ` +
175
+ `pointing to a public file.` ,
176
+ )
177
+ } )
174
178
175
179
// Writes out headers file format, with two spaces for indentation
176
180
// https://www.netlify.com/docs/headers-and-basic-auth/
177
- const stringifyHeaders = ( headers ) =>
178
- Object . entries ( headers ) . reduce ( ( text , [ path , headerList ] ) => {
179
- const headersString = headerList . reduce ( ( accum , header ) => `${ accum } ${ header } \n` , `` )
180
- return `${ text } ${ path } \n${ headersString } `
181
- } , `` )
181
+ const stringifyHeaders = ( headers : any ) => Object . entries ( headers ) . reduce ( ( text , [ path , headerList ] : [ string , Array < string > ] ) => {
182
+ const headersString = headerList . reduce ( ( accum , header ) => `${ accum } ${ header } \n` , `` )
183
+ return `${ text } ${ path } \n${ headersString } `
184
+ } , `` )
182
185
183
186
// program methods
184
187
185
- const validateUserOptions = ( pluginOptions , reporter ) => ( headers ) => {
188
+ const validateUserOptions = ( pluginOptions : any , reporter : any ) => ( headers : any ) => {
186
189
if ( ! validHeaders ( headers , reporter ) ) {
187
190
throw new Error (
188
191
`The "headers" option to gatsby-plugin-netlify is in the wrong shape. ` +
@@ -192,7 +195,7 @@ const validateUserOptions = (pluginOptions, reporter) => (headers) => {
192
195
)
193
196
}
194
197
195
- ; [ `mergeSecurityHeaders` , `mergeLinkHeaders` , `mergeCachingHeaders` ] . forEach ( ( mergeOption ) => {
198
+ [ `mergeSecurityHeaders` , `mergeLinkHeaders` , `mergeCachingHeaders` ] . forEach ( ( mergeOption ) => {
196
199
if ( ! _ . isBoolean ( pluginOptions [ mergeOption ] ) ) {
197
200
throw new TypeError (
198
201
`The "${ mergeOption } " option to gatsby-plugin-netlify must be a boolean. Check your gatsby-config.js.` ,
@@ -212,18 +215,23 @@ const validateUserOptions = (pluginOptions, reporter) => (headers) => {
212
215
}
213
216
214
217
const mapUserLinkHeaders =
215
- ( { manifest, pathPrefix, publicFolder } ) =>
216
- ( headers ) =>
217
- Object . fromEntries (
218
- Object . entries ( headers ) . map ( ( [ path , headerList ] ) => [
219
- path ,
220
- headerList . map ( transformLink ( manifest , publicFolder , pathPrefix ) ) ,
221
- ] ) ,
222
- )
218
+ ( {
219
+ manifest,
220
+ pathPrefix,
221
+ publicFolder
222
+ } : any ) =>
223
+ ( headers : any ) => Object . fromEntries (
224
+ Object . entries ( headers ) . map ( ( [ path , headerList ] : [ string , Array < string > ] ) => [
225
+ path ,
226
+ headerList . map ( transformLink ( manifest , publicFolder , pathPrefix ) ) ,
227
+ ] ) ,
228
+ )
223
229
224
230
const mapUserLinkAllPageHeaders =
225
- ( pluginData , { allPageHeaders } ) =>
226
- ( headers ) => {
231
+ ( pluginData : any , {
232
+ allPageHeaders
233
+ } : any ) =>
234
+ ( headers : any ) => {
227
235
if ( ! allPageHeaders ) {
228
236
return headers
229
237
}
@@ -233,7 +241,7 @@ const mapUserLinkAllPageHeaders =
233
241
const headersList = allPageHeaders . map ( transformLink ( manifest , publicFolder , pathPrefix ) )
234
242
235
243
const duplicateHeadersByPage = { }
236
- pages . forEach ( ( page ) => {
244
+ pages . forEach ( ( page : any ) => {
237
245
const pathKey = headersPath ( pathPrefix , page . path )
238
246
duplicateHeadersByPage [ pathKey ] = headersList
239
247
} )
@@ -242,8 +250,10 @@ const mapUserLinkAllPageHeaders =
242
250
}
243
251
244
252
const applyLinkHeaders =
245
- ( pluginData , { mergeLinkHeaders } ) =>
246
- ( headers ) => {
253
+ ( pluginData : any , {
254
+ mergeLinkHeaders
255
+ } : any ) =>
256
+ ( headers : any ) => {
247
257
if ( ! mergeLinkHeaders ) {
248
258
return headers
249
259
}
@@ -260,8 +270,10 @@ const applyLinkHeaders =
260
270
}
261
271
262
272
const applySecurityHeaders =
263
- ( { mergeSecurityHeaders } ) =>
264
- ( headers ) => {
273
+ ( {
274
+ mergeSecurityHeaders
275
+ } : any ) =>
276
+ ( headers : any ) => {
265
277
if ( ! mergeSecurityHeaders ) {
266
278
return headers
267
279
}
@@ -270,8 +282,10 @@ const applySecurityHeaders =
270
282
}
271
283
272
284
const applyCachingHeaders =
273
- ( pluginData , { mergeCachingHeaders } ) =>
274
- ( headers ) => {
285
+ ( pluginData : any , {
286
+ mergeCachingHeaders
287
+ } : any ) =>
288
+ ( headers : any ) => {
275
289
if ( ! mergeCachingHeaders ) {
276
290
return headers
277
291
}
@@ -301,18 +315,20 @@ const applyCachingHeaders =
301
315
}
302
316
303
317
const applyTransfromHeaders =
304
- ( { transformHeaders } ) =>
305
- ( headers ) =>
306
- _ . mapValues ( headers , transformHeaders )
318
+ ( {
319
+ transformHeaders
320
+ } : any ) =>
321
+ ( headers : any ) => _ . mapValues ( headers , transformHeaders )
307
322
308
- const transformToString = ( headers ) => `${ HEADER_COMMENT } \n\n${ stringifyHeaders ( headers ) } `
323
+ const transformToString = ( headers : any ) => `${ HEADER_COMMENT } \n\n${ stringifyHeaders ( headers ) } `
309
324
310
325
const writeHeadersFile =
311
- ( { publicFolder } ) =>
312
- ( contents ) =>
313
- writeFile ( publicFolder ( NETLIFY_HEADERS_FILENAME ) , contents )
326
+ ( {
327
+ publicFolder
328
+ } : any ) =>
329
+ ( contents : any ) => writeFile ( publicFolder ( NETLIFY_HEADERS_FILENAME ) , contents )
314
330
315
- const buildHeadersProgram = ( pluginData , pluginOptions , reporter ) =>
331
+ const buildHeadersProgram = ( pluginData : any , pluginOptions : any , reporter : any ) =>
316
332
_ . flow (
317
333
validateUserOptions ( pluginOptions , reporter ) ,
318
334
mapUserLinkHeaders ( pluginData ) ,
0 commit comments