Skip to content

Commit

Permalink
fix: reduce bundle size by limiting external dependencies (#276)
Browse files Browse the repository at this point in the history
I removed external dependency of `flux-standard-action` that includes entire loadash with it when imported. Instead, I re-implemented single function we are using and relied on single modules from loadash that are required. Should reduce the size significantly
  • Loading branch information
matmalkowski authored Oct 27, 2020
1 parent 5238bce commit 0971fd3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 48 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"node": ">= 12"
},
"dependencies": {
"flux-standard-action": "^2.0.0"
"lodash.isplainobject": "^4.0.6",
"lodash.isstring": "^4.0.1"
},
"peerDependencies": {
"electron": ">=8.0.0",
Expand All @@ -51,6 +52,8 @@
"@rollup/plugin-commonjs": "^15.0.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@types/jest": "^26.0.14",
"@types/lodash.isplainobject": "^4.0.6",
"@types/lodash.isstring": "^4.0.6",
"@types/node": "^14.11.1",
"@typescript-eslint/eslint-plugin": "^4.4.0",
"@typescript-eslint/parser": "^4.4.0",
Expand Down
10 changes: 4 additions & 6 deletions src/utils/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FluxStandardAction, isFSA } from 'flux-standard-action'
import { isFSA, FluxStandardAction } from './isFSA'

// Certain actions that we should never replay across stores
const blacklist = [/^@@/, /^redux-form/]
Expand All @@ -12,7 +12,7 @@ type ActionMeta = {
* stopForwarding allows you to give it an action, and it will return an
* equivalent action that will only play in the current process
*/
export const stopForwarding = (action: FluxStandardAction<string, unknown, ActionMeta>) => ({
export const stopForwarding = (action: FluxStandardAction<ActionMeta>) => ({
...action,
meta: {
...action.meta,
Expand All @@ -23,11 +23,9 @@ export const stopForwarding = (action: FluxStandardAction<string, unknown, Actio
/**
* validateAction ensures that the action meets the right format and isn't scoped locally
*/
export const validateAction = (
action: unknown
): action is FluxStandardAction<string, unknown, ActionMeta> => {
export const validateAction = (action: any): action is FluxStandardAction<ActionMeta> => {
return (
isFSA<string, unknown, ActionMeta>(action) &&
isFSA(action) &&
action.meta?.scope !== 'local' &&
blacklist.every((rule) => !rule.test(action.type))
)
Expand Down
13 changes: 13 additions & 0 deletions src/utils/isFSA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import isPlainObject from 'lodash.isplainobject'
import isString from 'lodash.isstring'

export const isFSA = (action: FluxStandardAction): boolean =>
isPlainObject(action) && isString(action.type) && Object.keys(action).every(isValidKey)

const isValidKey = (key: string) => ['type', 'payload', 'error', 'meta'].indexOf(key) > -1

export type FluxStandardAction<Meta = unknown> = {
type: string
payload?: unknown
meta?: Meta
}
62 changes: 21 additions & 41 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,25 @@
dependencies:
"@types/node" "*"

"@types/lodash.isplainobject@^4.0.6":
version "4.0.6"
resolved "https://registry.yarnpkg.com/@types/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#757d2dcdecbb32f4452018b285a586776092efd1"
integrity sha512-8G41YFhmOl8Ck6NrwLK5hhnbz6ADfuDJP+zusDnX3PoYhfC60+H/rQE6zmdO4yFzPCPJPY4oGZK2spbXm6gYEA==
dependencies:
"@types/lodash" "*"

"@types/lodash.isstring@^4.0.6":
version "4.0.6"
resolved "https://registry.yarnpkg.com/@types/lodash.isstring/-/lodash.isstring-4.0.6.tgz#1534d0c19a2ad79caa17558a298e366893ffd08c"
integrity sha512-uUGvF9G1G7jQ5H42Y38GA9rZmUoY8wI/OMSwnW0BZA+Ra0uxzpuQf4CixXl3yG3TvF6LjuduMyt1WvKl+je8QA==
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.14.162"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.162.tgz#65d78c397e0d883f44afbf1f7ba9867022411470"
integrity sha512-alvcho1kRUnnD1Gcl4J+hK0eencvzq9rmzvFPRmP5rPHx9VVsJj6bKLTATPVf9ktgv4ujzh7T+XWKp+jhuODig==

"@types/minimatch@*":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
Expand Down Expand Up @@ -4015,7 +4034,7 @@ debug@^4.0.0:
dependencies:
ms "2.1.2"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -5433,13 +5452,6 @@ flush-write-stream@^1.0.0:
inherits "^2.0.3"
readable-stream "^2.3.6"

flux-standard-action@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/flux-standard-action/-/flux-standard-action-2.1.1.tgz#b2e204145ea8e4294d6b0f178d8e8c416802948f"
integrity sha512-W86GzmXmIiTVq/dpYVd2HtTIUX9c35Iq3ao3xR6qcKtuXgbu+BDEj72op5VnEIe/kpuSbhl+I8kT1iS2hpcusw==
dependencies:
lodash "^4.17.15"

follow-redirects@^1.0.0:
version "1.13.0"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.0.tgz#b42e8d93a2a7eea5ed88633676d6597bc8e384db"
Expand Down Expand Up @@ -6413,7 +6425,7 @@ import-local@^3.0.2:
pkg-dir "^4.2.0"
resolve-cwd "^3.0.0"

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -7912,11 +7924,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -7925,33 +7932,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._root@~3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
Expand Down Expand Up @@ -8017,11 +8002,6 @@ lodash.merge@^4.6.1:
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

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

0 comments on commit 0971fd3

Please sign in to comment.