diff --git a/package.json b/package.json index 3744411a..4527519d 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/build-headers-program.ts b/src/build-headers-program.ts index a7709696..1f571b2b 100644 --- a/src/build-headers-program.ts +++ b/src/build-headers-program.ts @@ -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, @@ -15,6 +15,7 @@ import { NETLIFY_HEADERS_FILENAME, PAGE_DATA_DIR, } from './constants' +import { isBoolean, flow } from './util' const getHeaderName = (header: any) => { const matches = header.match(/^([^:]+):/) @@ -22,7 +23,7 @@ const getHeaderName = (header: any) => { } const validHeaders = (headers: any, reporter: any) => { - if (!headers || !_.isObject(headers)) { + if (!headers || typeof headers !== 'object') { return false } @@ -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), @@ -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) => { @@ -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.`, ) @@ -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)}` @@ -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 */ diff --git a/src/constants.ts b/src/constants.ts index 4731e5fc..db0f017b 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,5 +1,3 @@ -import _ from 'lodash' - // Gatsby values export const BUILD_HTML_STAGE = `build-html` export const BUILD_CSS_STAGE = `build-css` @@ -14,7 +12,7 @@ export const DEFAULT_OPTIONS = { mergeSecurityHeaders: true, mergeLinkHeaders: true, mergeCachingHeaders: true, - transformHeaders: _.identity, + transformHeaders: (value) => value, generateMatchPathRewrites: true, } diff --git a/src/util.ts b/src/util.ts new file mode 100644 index 00000000..67cba94e --- /dev/null +++ b/src/util.ts @@ -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 = +>(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] + + \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 6a2b87ed..5b38c6cf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -9185,7 +9190,7 @@ lodash.zip@^4.2.0: lodash@4.17.21, 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: