1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

lodashをimport時のwebpack容量を抑えたい...

Posted at

lodashをimport時の容量節約

Webpackのバンドル時にlodashが全部含まれしまい容量が肥大化する。
スクリーンショット 2022-10-18 17.15.29.png

importの仕方を変える

import { isEmpty } from 'lodash';

...
const obj = hoge.fuga;
if (isEmpty(obj)) {
  // 処理
} else {
  // 処理
}

まずは安直に上記のようなコードを書いたが、実はlodashの中身がすべてインポートされている。

// ビルド後
var lodash = require('lodash');
var isEmpty = lodash.isEmpty;
...

スクリーンショット 2022-10-04 14.21.57.png
VSCodeのプラグインであるimport Costを使っても変わらないことがわかる。

一旦落ち着いた方法

一つずつ呼ぶ

import get from "lodash/get";
import set from "lodash/set";
import isEmpty from "lodash/isEmpty";
import map from "lodash/map";
.
.
.
.

これまた安直だが、一旦はこれに落ち着いた。
容量も減っていることが確認できる
スクリーンショット 2022-10-18 17.18.43.png

最後に

他に賢いやり方知ってる方いたら教えてくださいまし。。。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?