Skip to content

chore: Remove lodash #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"fs-extra": "^10.0.0",
"gatsby-core-utils": "^3.5.2",
"kebab-hash": "^0.1.2",
"lodash": "^4.17.21",
"lodash.mergewith": "^4.6.2",
"webpack-assets-manifest": "^5.0.6"
},
"devDependencies": {
Expand Down
44 changes: 26 additions & 18 deletions src/build-headers-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { parse, posix } from 'path'

import { writeFile, existsSync } from 'fs-extra'
import kebabHash from 'kebab-hash'
import _ from 'lodash'
import mergeWith from 'lodash.mergewith'

import {
HEADER_COMMENT,
Expand All @@ -15,14 +15,15 @@ import {
NETLIFY_HEADERS_FILENAME,
PAGE_DATA_DIR,
} from './constants'
import { isBoolean, flow } from './util'

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

const validHeaders = (headers: any, reporter: any) => {
if (!headers || !_.isObject(headers)) {
if (!headers || typeof headers !== 'object') {
return false
}

Expand Down Expand Up @@ -97,7 +98,7 @@ const preloadHeadersByPage = ({
}

pages.forEach((page: any) => {
const scripts = _.flatMap(COMMON_BUNDLES, (file) => getScriptPath(file, manifest))
const scripts = COMMON_BUNDLES.flatMap((file) => getScriptPath(file, manifest))
scripts.push(
...getScriptPath(pathChunkName(page.path), manifest),
...getScriptPath(page.componentChunkName, manifest),
Expand All @@ -124,17 +125,15 @@ const preloadHeadersByPage = ({
return linksByPage
}

const defaultMerge = (...headers: any[]) => {
const unionMerge = (objValue: any, srcValue: any) => {
if (Array.isArray(objValue)) {
return _.union(objValue, srcValue)
}
// opt into default merge behavior
const unionMerge = (objValue: any, srcValue: any) => {
if (Array.isArray(objValue)) {
return [...new Set([...objValue, ...srcValue])]
}

return _.mergeWith({}, ...headers, unionMerge)
// opt into default merge behavior
}

const defaultMerge = (...headers: any[]) => mergeWith({}, ...headers, unionMerge)

const headersMerge = (userHeaders: any, defaultHeaders: any) => {
const merged = {}
Object.keys(defaultHeaders).forEach((path) => {
Expand Down Expand Up @@ -196,7 +195,7 @@ const validateUserOptions = (pluginOptions: any, reporter: any) => (headers: any
}

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

const applyTransfromHeaders =
const applyTransformHeaders =
({
transformHeaders
}: any) =>
(headers: any) => _.mapValues(headers, transformHeaders)
(headers: any) =>
Object.entries(headers).reduce((temp, [key, value]) => {
temp[key] = transformHeaders(value)
return temp
}, {})


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

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

const buildHeadersProgram = (pluginData: any, pluginOptions: any, reporter: any) =>
_.flow(
const buildHeadersProgram = (pluginData: any, pluginOptions: any, reporter: any) => {
const returnVal = flow(
[
validateUserOptions(pluginOptions, reporter),
mapUserLinkHeaders(pluginData),
applySecurityHeaders(pluginOptions),
applyCachingHeaders(pluginData, pluginOptions),
mapUserLinkAllPageHeaders(pluginData, pluginOptions),
applyLinkHeaders(pluginData, pluginOptions),
applyTransfromHeaders(pluginOptions),
applyTransformHeaders(pluginOptions),
transformToString,
writeHeadersFile(pluginData),
)(pluginOptions.headers)
])(pluginOptions.headers)
console.log({returnVal})
return returnVal
}

export default buildHeadersProgram
/* eslint-enable max-lines */
4 changes: 1 addition & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _ from 'lodash'

// Gatsby values
export const BUILD_HTML_STAGE = `build-html`
export const BUILD_CSS_STAGE = `build-css`
Expand All @@ -14,7 +12,7 @@ export const DEFAULT_OPTIONS = {
mergeSecurityHeaders: true,
mergeLinkHeaders: true,
mergeCachingHeaders: true,
transformHeaders: _.identity,
transformHeaders: (value) => value,
generateMatchPathRewrites: true,
}

Expand Down
31 changes: 31 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Accounting for true, false, and new Boolean()
export const isBoolean = (val: any): boolean => typeof val === 'boolean' ||
(
typeof val === 'object' &&
val !== null &&
typeof val.valueOf() === 'boolean'
)

type Header = string
type Headers = Header[]

// TODO: better return type
type FlowableFunction = (...flowArgs: Headers) => any;
/**
*
* @param functions - takes in an array of functions
* @returns The function documented below
*/
export const flow =
<FlowReturnType = Promise<any>>(functions: FlowableFunction[]) =>
/**
*
* @param headers - In our case, headers is only {pluginOptions.headers} (in build-headers-program.ts),
* but in this generic implementation could take in any number of arguments
*
* @returns The evaluated return value of the last function from the array of functions provided in the
* {functions} parameter
*/
(...headers: Headers): FlowReturnType => functions.reduce((resultOfPrev, func) => [func(...resultOfPrev)], headers)[0]


7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9158,6 +9158,11 @@ lodash.merge@^4.6.2:
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

lodash.mergewith@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/lodash.mergewith/-/lodash.mergewith-4.6.2.tgz#617121f89ac55f59047c7aec1ccd6654c6590f55"
integrity sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==

lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
Expand Down Expand Up @@ -9185,7 +9190,7 @@ lodash.zip@^4.2.0:

[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:
version "4.17.21"
resolved "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

loose-envify@^1.0.0, loose-envify@^1.4.0:
Expand Down