@@ -3,7 +3,7 @@ import { parse, posix } from 'path'
3
3
4
4
import { writeFile , existsSync } from 'fs-extra'
5
5
import kebabHash from 'kebab-hash'
6
- import _ from 'lodash'
6
+ import mergeWith from 'lodash.mergewith '
7
7
8
8
import {
9
9
HEADER_COMMENT ,
@@ -15,14 +15,15 @@ import {
15
15
NETLIFY_HEADERS_FILENAME ,
16
16
PAGE_DATA_DIR ,
17
17
} from './constants'
18
+ import { isBoolean , flow } from './util'
18
19
19
20
const getHeaderName = ( header : any ) => {
20
21
const matches = header . match ( / ^ ( [ ^ : ] + ) : / )
21
22
return matches && matches [ 1 ]
22
23
}
23
24
24
25
const validHeaders = ( headers : any , reporter : any ) => {
25
- if ( ! headers || ! _ . isObject ( headers ) ) {
26
+ if ( ! headers || typeof headers !== 'object' ) {
26
27
return false
27
28
}
28
29
@@ -97,7 +98,7 @@ const preloadHeadersByPage = ({
97
98
}
98
99
99
100
pages . forEach ( ( page : any ) => {
100
- const scripts = _ . flatMap ( COMMON_BUNDLES , ( file ) => getScriptPath ( file , manifest ) )
101
+ const scripts = COMMON_BUNDLES . flatMap ( ( file ) => getScriptPath ( file , manifest ) )
101
102
scripts . push (
102
103
...getScriptPath ( pathChunkName ( page . path ) , manifest ) ,
103
104
...getScriptPath ( page . componentChunkName , manifest ) ,
@@ -124,17 +125,15 @@ const preloadHeadersByPage = ({
124
125
return linksByPage
125
126
}
126
127
127
- const defaultMerge = ( ...headers : any [ ] ) => {
128
- const unionMerge = ( objValue : any , srcValue : any ) => {
129
- if ( Array . isArray ( objValue ) ) {
130
- return _ . union ( objValue , srcValue )
131
- }
132
- // opt into default merge behavior
128
+ const unionMerge = ( objValue : any , srcValue : any ) => {
129
+ if ( Array . isArray ( objValue ) ) {
130
+ return [ ...new Set ( [ ...objValue , ...srcValue ] ) ]
133
131
}
134
-
135
- return _ . mergeWith ( { } , ...headers , unionMerge )
132
+ // opt into default merge behavior
136
133
}
137
134
135
+ const defaultMerge = ( ...headers : any [ ] ) => mergeWith ( { } , ...headers , unionMerge )
136
+
138
137
const headersMerge = ( userHeaders : any , defaultHeaders : any ) => {
139
138
const merged = { }
140
139
Object . keys ( defaultHeaders ) . forEach ( ( path ) => {
@@ -196,7 +195,7 @@ const validateUserOptions = (pluginOptions: any, reporter: any) => (headers: any
196
195
}
197
196
198
197
[ `mergeSecurityHeaders` , `mergeLinkHeaders` , `mergeCachingHeaders` ] . forEach ( ( mergeOption ) => {
199
- if ( ! _ . isBoolean ( pluginOptions [ mergeOption ] ) ) {
198
+ if ( ! isBoolean ( pluginOptions [ mergeOption ] ) ) {
200
199
throw new TypeError (
201
200
`The "${ mergeOption } " option to gatsby-plugin-netlify must be a boolean. Check your gatsby-config.js.` ,
202
201
)
@@ -314,11 +313,16 @@ const applyCachingHeaders =
314
313
return defaultMerge ( headers , cachingHeaders , CACHING_HEADERS )
315
314
}
316
315
317
- const applyTransfromHeaders =
316
+ const applyTransformHeaders =
318
317
( {
319
318
transformHeaders
320
319
} : any ) =>
321
- ( headers : any ) => _ . mapValues ( headers , transformHeaders )
320
+ ( headers : any ) =>
321
+ Object . entries ( headers ) . reduce ( ( temp , [ key , value ] ) => {
322
+ temp [ key ] = transformHeaders ( value )
323
+ return temp
324
+ } , { } )
325
+
322
326
323
327
const transformToString = ( headers : any ) => `${ HEADER_COMMENT } \n\n${ stringifyHeaders ( headers ) } `
324
328
@@ -328,18 +332,22 @@ const writeHeadersFile =
328
332
} : any ) =>
329
333
( contents : any ) => writeFile ( publicFolder ( NETLIFY_HEADERS_FILENAME ) , contents )
330
334
331
- const buildHeadersProgram = ( pluginData : any , pluginOptions : any , reporter : any ) =>
332
- _ . flow (
335
+ const buildHeadersProgram = ( pluginData : any , pluginOptions : any , reporter : any ) => {
336
+ const returnVal = flow (
337
+ [
333
338
validateUserOptions ( pluginOptions , reporter ) ,
334
339
mapUserLinkHeaders ( pluginData ) ,
335
340
applySecurityHeaders ( pluginOptions ) ,
336
341
applyCachingHeaders ( pluginData , pluginOptions ) ,
337
342
mapUserLinkAllPageHeaders ( pluginData , pluginOptions ) ,
338
343
applyLinkHeaders ( pluginData , pluginOptions ) ,
339
- applyTransfromHeaders ( pluginOptions ) ,
344
+ applyTransformHeaders ( pluginOptions ) ,
340
345
transformToString ,
341
346
writeHeadersFile ( pluginData ) ,
342
- ) ( pluginOptions . headers )
347
+ ] ) ( pluginOptions . headers )
348
+ console . log ( { returnVal} )
349
+ return returnVal
350
+ }
343
351
344
352
export default buildHeadersProgram
345
353
/* eslint-enable max-lines */
0 commit comments