Correct way of importing and using lodash in Angular

前端 未结 5 1758
再見小時候
再見小時候 2020-12-13 17:27

I used to be able to use a lodash method in Angular by an import statement that looked like the following:

import {debounce as _debounce} from \'lodash\';
         


        
5条回答
  •  温柔的废话
    2020-12-13 18:17

    I've tried different approaches to reduce lodash size in bundle and while the updated answer by Kuncevič works my bundle still includes non-es lodash (my guess it's because it's required by some other dependency) so at this point I dont think it makes sense adding lodash-es at all or importing functions separately from 'lodash'. From what I understand npm does deduplication and moves lodash to top-level folder (node_modules) that's why it's there even after I removed it from package.json and reinstalled node_modules. Webpack doesn't care about package.json and if it sees that lodash is imported by anyone - bundles it.

提交回复
热议问题