Skip to content

Commit 510b4b5

Browse files
authored
chore: Remove lodash (#130)
* Remove _.isObject * remove flatmap * remove isBoolean * fix typo * remove mapvalues * remove identity * remove union * switch mergeWith to single import * remove flow and remove lodash * Add comment * chore: switch to reduce * Add comments * More comments * Modify types
1 parent 187dff7 commit 510b4b5

File tree

5 files changed

+65
-23
lines changed

5 files changed

+65
-23
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"fs-extra": "^10.0.0",
1818
"gatsby-core-utils": "^3.5.2",
1919
"kebab-hash": "^0.1.2",
20-
"lodash": "^4.17.21",
20+
"lodash.mergewith": "^4.6.2",
2121
"webpack-assets-manifest": "^5.0.6"
2222
},
2323
"devDependencies": {

src/build-headers-program.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { parse, posix } from 'path'
33

44
import { writeFile, existsSync } from 'fs-extra'
55
import kebabHash from 'kebab-hash'
6-
import _ from 'lodash'
6+
import mergeWith from 'lodash.mergewith'
77

88
import {
99
HEADER_COMMENT,
@@ -15,14 +15,15 @@ import {
1515
NETLIFY_HEADERS_FILENAME,
1616
PAGE_DATA_DIR,
1717
} from './constants'
18+
import { isBoolean, flow } from './util'
1819

1920
const getHeaderName = (header: any) => {
2021
const matches = header.match(/^([^:]+):/)
2122
return matches && matches[1]
2223
}
2324

2425
const validHeaders = (headers: any, reporter: any) => {
25-
if (!headers || !_.isObject(headers)) {
26+
if (!headers || typeof headers !== 'object') {
2627
return false
2728
}
2829

@@ -97,7 +98,7 @@ const preloadHeadersByPage = ({
9798
}
9899

99100
pages.forEach((page: any) => {
100-
const scripts = _.flatMap(COMMON_BUNDLES, (file) => getScriptPath(file, manifest))
101+
const scripts = COMMON_BUNDLES.flatMap((file) => getScriptPath(file, manifest))
101102
scripts.push(
102103
...getScriptPath(pathChunkName(page.path), manifest),
103104
...getScriptPath(page.componentChunkName, manifest),
@@ -124,17 +125,15 @@ const preloadHeadersByPage = ({
124125
return linksByPage
125126
}
126127

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])]
133131
}
134-
135-
return _.mergeWith({}, ...headers, unionMerge)
132+
// opt into default merge behavior
136133
}
137134

135+
const defaultMerge = (...headers: any[]) => mergeWith({}, ...headers, unionMerge)
136+
138137
const headersMerge = (userHeaders: any, defaultHeaders: any) => {
139138
const merged = {}
140139
Object.keys(defaultHeaders).forEach((path) => {
@@ -196,7 +195,7 @@ const validateUserOptions = (pluginOptions: any, reporter: any) => (headers: any
196195
}
197196

198197
[`mergeSecurityHeaders`, `mergeLinkHeaders`, `mergeCachingHeaders`].forEach((mergeOption) => {
199-
if (!_.isBoolean(pluginOptions[mergeOption])) {
198+
if (!isBoolean(pluginOptions[mergeOption])) {
200199
throw new TypeError(
201200
`The "${mergeOption}" option to gatsby-plugin-netlify must be a boolean. Check your gatsby-config.js.`,
202201
)
@@ -314,11 +313,16 @@ const applyCachingHeaders =
314313
return defaultMerge(headers, cachingHeaders, CACHING_HEADERS)
315314
}
316315

317-
const applyTransfromHeaders =
316+
const applyTransformHeaders =
318317
({
319318
transformHeaders
320319
}: 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+
322326

323327
const transformToString = (headers: any) => `${HEADER_COMMENT}\n\n${stringifyHeaders(headers)}`
324328

@@ -328,18 +332,22 @@ const writeHeadersFile =
328332
}: any) =>
329333
(contents: any) => writeFile(publicFolder(NETLIFY_HEADERS_FILENAME), contents)
330334

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+
[
333338
validateUserOptions(pluginOptions, reporter),
334339
mapUserLinkHeaders(pluginData),
335340
applySecurityHeaders(pluginOptions),
336341
applyCachingHeaders(pluginData, pluginOptions),
337342
mapUserLinkAllPageHeaders(pluginData, pluginOptions),
338343
applyLinkHeaders(pluginData, pluginOptions),
339-
applyTransfromHeaders(pluginOptions),
344+
applyTransformHeaders(pluginOptions),
340345
transformToString,
341346
writeHeadersFile(pluginData),
342-
)(pluginOptions.headers)
347+
])(pluginOptions.headers)
348+
console.log({returnVal})
349+
return returnVal
350+
}
343351

344352
export default buildHeadersProgram
345353
/* eslint-enable max-lines */

src/constants.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import _ from 'lodash'
2-
31
// Gatsby values
42
export const BUILD_HTML_STAGE = `build-html`
53
export const BUILD_CSS_STAGE = `build-css`
@@ -14,7 +12,7 @@ export const DEFAULT_OPTIONS = {
1412
mergeSecurityHeaders: true,
1513
mergeLinkHeaders: true,
1614
mergeCachingHeaders: true,
17-
transformHeaders: _.identity,
15+
transformHeaders: (value) => value,
1816
generateMatchPathRewrites: true,
1917
}
2018

src/util.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Accounting for true, false, and new Boolean()
2+
export const isBoolean = (val: any): boolean => typeof val === 'boolean' ||
3+
(
4+
typeof val === 'object' &&
5+
val !== null &&
6+
typeof val.valueOf() === 'boolean'
7+
)
8+
9+
type Header = string
10+
type Headers = Header[]
11+
12+
// TODO: better return type
13+
type FlowableFunction = (...flowArgs: Headers) => any;
14+
/**
15+
*
16+
* @param functions - takes in an array of functions
17+
* @returns The function documented below
18+
*/
19+
export const flow =
20+
<FlowReturnType = Promise<any>>(functions: FlowableFunction[]) =>
21+
/**
22+
*
23+
* @param headers - In our case, headers is only {pluginOptions.headers} (in build-headers-program.ts),
24+
* but in this generic implementation could take in any number of arguments
25+
*
26+
* @returns The evaluated return value of the last function from the array of functions provided in the
27+
* {functions} parameter
28+
*/
29+
(...headers: Headers): FlowReturnType => functions.reduce((resultOfPrev, func) => [func(...resultOfPrev)], headers)[0]
30+
31+

yarn.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9158,6 +9158,11 @@ lodash.merge@^4.6.2:
91589158
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
91599159
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==
91609160

9161+
lodash.mergewith@^4.6.2:
9162+
version "4.6.2"
9163+
resolved "https://registry.yarnpkg.com/lodash.mergewith/-/lodash.mergewith-4.6.2.tgz#617121f89ac55f59047c7aec1ccd6654c6590f55"
9164+
integrity sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==
9165+
91619166
lodash.sortby@^4.7.0:
91629167
version "4.7.0"
91639168
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
@@ -9185,7 +9190,7 @@ lodash.zip@^4.2.0:
91859190

91869191
[email protected], lodash@^4.13.1, lodash@^4.17.10, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.17.4, lodash@^4.7.0:
91879192
version "4.17.21"
9188-
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz"
9193+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
91899194
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
91909195

91919196
loose-envify@^1.0.0, loose-envify@^1.4.0:

0 commit comments

Comments
 (0)