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 10 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
37 changes: 20 additions & 17 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,14 @@ const applyCachingHeaders =
return defaultMerge(headers, cachingHeaders, CACHING_HEADERS)
}

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


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

Expand All @@ -329,17 +331,18 @@ const writeHeadersFile =
(contents: any) => writeFile(publicFolder(NETLIFY_HEADERS_FILENAME), contents)

const buildHeadersProgram = (pluginData: any, pluginOptions: any, reporter: any) =>
_.flow(
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)

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
12 changes: 12 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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'
)

export const flow =
funcs =>
(...args) =>
funcs.reduce((prev, fnc) => [fnc(...prev)], args)[0]
5 changes: 5 additions & 0 deletions 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