Skip to content
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

[e18e]: Replace lodash/merge & lodash/mergeWith with an alternative #28663

Draft
wants to merge 7 commits into
base: e18e-lodash
Choose a base branch
from
Draft
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
4 changes: 0 additions & 4 deletions code/builders/builder-vite/src/optimizeDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ const INCLUDE_CANDIDATES = [
'lodash/mapKeys',
'lodash/mapValues.js',
'lodash/mapValues',
'lodash/merge.js',
'lodash/merge',
'lodash/mergeWith.js',
'lodash/mergeWith',
'lodash/pick.js',
'lodash/pick',
'lodash/pickBy.js',
Expand Down
1 change: 1 addition & 0 deletions code/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
"@types/express": "^4.17.21",
"@types/node": "^18.0.0",
"browser-assert": "^1.2.1",
"defu": "^6.1.4",
"esbuild": "^0.18.0 || ^0.19.0 || ^0.20.0 || ^0.21.0 || ^0.22.0 || ^0.23.0",
"esbuild-register": "^3.5.0",
"express": "^4.19.2",
Expand Down
15 changes: 15 additions & 0 deletions code/core/src/manager-api/lib/merge.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test, describe } from "vitest";

import merge from "./merge";

describe(merge.name, () => {
test("applies entries not in the source", ({ expect }) => {
const merged = merge({ a: 1 }, { a: 2, b: 3, c: 4 });
console.log({ merged });
expect(merged).toStrictEqual({
a: 1,
b: 3,
c: 4
});
});
});
34 changes: 15 additions & 19 deletions code/core/src/manager-api/lib/merge.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import mergeWith from 'lodash/mergeWith.js';
import isEqual from 'lodash/isEqual.js';
import { defu, defuArrayFn } from 'defu';

import { logger } from '@storybook/core/client-logger';

export default <TObj = any>(a: TObj, b: Partial<TObj>) =>
mergeWith({}, a, b, (objValue: TObj, srcValue: Partial<TObj>) => {
if (Array.isArray(srcValue) && Array.isArray(objValue)) {
srcValue.forEach((s) => {
const existing = objValue.find((o) => o === s || isEqual(o, s));
if (!existing) {
objValue.push(s);
}
});

return objValue;
}
if (Array.isArray(objValue)) {
logger.log(['the types mismatch, picking', objValue]);
return objValue;
export default <TObj = any>(a: TObj, b: Partial<TObj> & any) => {
if (Array.isArray(a)) {
if (Array.isArray(b)) {
return defuArrayFn(a, b);
}
return undefined;
});

logger.log(['the types mismatch, picking', a]);
return a;
}

// TODO: Ask for what is prefered:
// 1. type guard (safe at runtime, but potential performance slow-down)
// 2. or type assertion (unsafe at runtime)
return defu(a as object, b);
Comment on lines +15 to +17
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need help with decision on this one.

I've had issues with types mismatch, when the records from the second argument had types which did not exists on the first argument (source) in the PreviewWeb.test.ts.

Is it okay to change type of second argument from Partial<TObj> to Partial<TObj> & any?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Just widen the types for now and we can take a proper look afterwards.

};
Loading
Loading