Skip to content

Commit dcdbb31

Browse files
author
Thomas Scholtes
committed
Slim down lodash dependencies
We replace the `lodash.isequal` dependency with `lodash/isEqual`. This makes it more likely that bundlers can deduplicate this dependency if other dependency use `lodash` too. To fully leverage this we also want to properly expose this in the bundles. First, we use the ES Modules `import` so that the module bundle uses it too instead of inlining `require('lodash/isEqual')`. To prevent bundling this dependency modify the `external` rollup configuration. We also replace the UMD bundle with a CommonJS bundle. Before, rollup would just inline `require('lodash/isEqual')` so the bundle would not function as a UMD bundle anyways.
1 parent 60576fe commit dcdbb31

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"TypeScript",
77
"JSON"
88
],
9-
"main": "dist/index.umd.js",
9+
"main": "dist/index.js",
1010
"module": "dist/index.es5.js",
1111
"typings": "dist/types/index.d.ts",
1212
"files": [
@@ -62,8 +62,8 @@
6262
},
6363
"devDependencies": {
6464
"@types/jest": "^22.2.3",
65-
"@types/lodash.isequal": "^4.5.3",
6665
"@types/node": "^10.5.3",
66+
"@types/lodash": "^4.5.0",
6767
"colors": "^1.1.2",
6868
"cross-env": "^5.0.1",
6969
"jest": "^22.0.2",
@@ -84,6 +84,6 @@
8484
"typescript": "~3.1.0"
8585
},
8686
"dependencies": {
87-
"lodash.isequal": "^4.5.0"
87+
"lodash": "^4.5.0"
8888
}
8989
}

rollup.config.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
import resolve from 'rollup-plugin-node-resolve';
2-
import commonjs from 'rollup-plugin-commonjs';
32
import sourceMaps from 'rollup-plugin-sourcemaps';
43
import typescript from 'rollup-plugin-typescript2';
54

65
const pkg = require('./package.json');
76

87
export default {
98
input: `src/index.ts`,
10-
output: [{file: pkg.main, name: 'index', format: 'umd'}, {file: pkg.module, format: 'es'}],
11-
sourcemap: true,
12-
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
13-
external: [],
9+
output: [
10+
{
11+
file: pkg.main,
12+
name: 'index',
13+
format: 'cjs',
14+
sourcemap: true
15+
},
16+
{
17+
file: pkg.module,
18+
format: 'es',
19+
sourcemap: true
20+
}
21+
],
22+
external: id => {
23+
return id.includes('/node_modules/');
24+
},
1425
watch: {
1526
include: 'src/**'
1627
},
@@ -19,11 +30,6 @@ export default {
1930
typescript({
2031
useTsconfigDeclarationDir: true
2132
}),
22-
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
23-
commonjs(),
24-
// Allow node_modules resolution, so you can use 'external' to control
25-
// which external modules to include in the bundle
26-
// https://github.com/rollup/rollup-plugin-node-resolve#usage
2733
resolve(),
2834

2935
// Resolve source maps to the original source

src/decoder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Result from './result';
2-
const isEqual = require('lodash.isequal'); // this syntax avoids TS1192
2+
import isEqual from "lodash/isEqual"
33

44
/**
55
* Information describing how json data failed to match a decoder.

yarn.lock

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,16 @@
6161
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-22.2.3.tgz#0157c0316dc3722c43a7b71de3fdf3acbccef10d"
6262
integrity sha512-e74sM9W/4qqWB6D4TWV9FQk0WoHtX1X4FJpbjxucMSVJHtFjbQOH3H6yp+xno4br0AKG0wz/kPtaN599GUOvAg==
6363

64-
"@types/lodash.isequal@^4.5.3":
65-
version "4.5.3"
66-
resolved "https://registry.yarnpkg.com/@types/lodash.isequal/-/lodash.isequal-4.5.3.tgz#b0d2747d3e76cc94da47ebd932c69a98c0ba61b4"
67-
integrity sha512-tpTUmHksO2H9RF98Y2w7v06ZeEKAxHPo2ysL0bV5qv5UBweiZl33NFu5QYmYOSxSnHMqBt/vsVsBVeQAcJiokg==
68-
dependencies:
69-
"@types/lodash" "*"
70-
71-
"@types/lodash@*", "@types/lodash@^4.14.110":
64+
"@types/lodash@^4.14.110":
7265
version "4.14.116"
7366
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.116.tgz#5ccf215653e3e8c786a58390751033a9adca0eb9"
7467
integrity sha512-lRnAtKnxMXcYYXqOiotTmJd74uawNWuPnsnPrrO7HiFuE3npE2iQhfABatbYDyxTNqZNuXzcKGhw37R7RjBFLg==
7568

69+
"@types/lodash@^4.14.168":
70+
version "4.14.168"
71+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.168.tgz#fe24632e79b7ade3f132891afff86caa5e5ce008"
72+
integrity sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q==
73+
7674
"@types/marked@^0.4.0":
7775
version "0.4.0"
7876
resolved "https://registry.yarnpkg.com/@types/marked/-/marked-0.4.0.tgz#057a6165703e7419217f8ffc6887747f980b6315"
@@ -2422,11 +2420,6 @@ locate-path@^2.0.0:
24222420
p-locate "^2.0.0"
24232421
path-exists "^3.0.0"
24242422

2425-
lodash.isequal@^4.5.0:
2426-
version "4.5.0"
2427-
resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
2428-
integrity sha1-QVxEePK8wwEgwizhDtMib30+GOA=
2429-
24302423
lodash.sortby@^4.7.0:
24312424
version "4.7.0"
24322425
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
@@ -2437,6 +2430,11 @@ lodash@^4.13.1, lodash@^4.17.10, lodash@^4.17.4:
24372430
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
24382431
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==
24392432

2433+
lodash@^4.5.0:
2434+
version "4.17.21"
2435+
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
2436+
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
2437+
24402438
loose-envify@^1.0.0:
24412439
version "1.4.0"
24422440
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"

0 commit comments

Comments
 (0)