TypeScript in Angular causing lodash tree shaking failure

I am currently working on a large project that involves TypeScript.

Various attempts have been made to optimize the use of lodash in my project.

Before making any conclusions, I believe it is important to review the outcomes of my efforts.

The command I use for building is

build command

ng build --stats-json
webpack-bundle-analyzer {{stats.json path}}

custom_loadsh.ts

export { forEach } from 'lodash/forEach';
export { cloneDeep } from 'lodash/cloneDeep';
...
..
.

app.component.ts

import * as _ from './custom_lodash';
export class AppComponent implements OnInit {
   ...
   ngOnInit() {
       let data = {
          test: 1  
       };
       let data2 = _.cloneDeep(data);
   }
   ...
}

Instead of importing the entire lodash library through custom_lodash, I have selectively imported specific modules.

https://i.stack.imgur.com/CGcNF.png

In reality, the example code provided above may differ slightly from the actual project, but the overall setup remains consistent.

I am curious about why tree shaking with lodash is not successful.

You can find more information on this topic on the following site: https://medium.com/@martin_hotell/tree-shake-lodash-with-webpack-jest-and-typescript-2734fa13b5cd

Answer №1

I have discovered the root of the problem.

The issue lies within another package located in the node_modules directory.

Specifically, in my case, it appears that the ngx-treeview package is utilizing lodash.

Here is an excerpt of the code from the ngx-treeview package:

import { isNil, isString } from 'lodash';

To address this, I attempted the following modifications:

import isNil from 'lodash/isString';
import isString from 'lodash/isNil';

However, it seems that this solution is only temporary.

My next step will be to submit a bug report to the GitHub repository of the ngx-treeview package. Unfortunately, the last update to that package was made over a year ago.

Therefore, I have decided to fork that particular branch for further examination.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Choosing and unchoosing columns in an Angular Material table

Feel free to check out this link for more information: Angular PrimeNG Table Order Resize Toggle. It provides guidance on how to select and deselect columns in Angular Mat table. ...

Display a variety of components in a single route

I have a question: Is it possible to show multiple components in one route in Angular 7? For example, if the route is '/' then I want to display HeaderComponent and SideBarComponent. So when the path is '/home', these two components alo ...

Integrating fresh components into a JSON structure

I've been attempting to insert a new element into my JSON, but I'm struggling to do it correctly. I've tried numerous approaches and am unsure of what might be causing the issue. INITIAL JSON INPUT { "UnitID":"1148", "UNIT":"202B", "Sp ...

Sharing information with a service in Ionic and Angular

I need to send data to my service and incorporate it into a URL string. The code snippet below shows how I am obtaining the data in my constructor when the user navigates to the page: constructor(public alertController: AlertController, pri ...

Tips for successfully importing $lib in SvelteKit without encountering any TypeScript errors

Is there a way to import a $lib into my svelte project without encountering typescript errors in vscode? The project is building and running smoothly. import ThemeSwitch from '$lib/ThemeSwitch/ThemeSwitch.svelte'; The error message says "Canno ...

Typescript Event Handling in React Select

I am facing an issue with my handleSelect function: const handlerSelectBank = (event: React.ChangeEvent<{ value: unknown }>) => { setState({ ...state, buttonDisabled: false, selectedBank: event }); }; Upon execution, I encountered ...

Delay in Updating Nativescript Slider Value

After developing a metronome using the Nativescript Slider here to adjust speed (interval), I encountered an issue with incorrect updating of values. The code initially worked well, allowing real-time speed changes: app.component.html <Slider #sl min ...

ESLint refuses to be turned off for a particular file

I am in the process of creating a Notes.ts file specifically for TypeScript notes. I require syntax highlighting but do not want to use eslint. How can I prevent eslint from running on my notes file? Directory Structure root/.eslintignore root/NestJS.ts r ...

Using Angular 8, you can create a reactive form that includes a custom ng-select component which has

My custom angular reactive form includes a ng-select component along with other elements. I have implemented the following code to validate the form: private validateCustForm() { this.validation.touchFormControls(this.appointmentForm); if (this.ap ...

What are some ways to leverage a promise-returning callback function?

Here is a function that I have: export const paramsFactory = (params: paramsType) => { return ... } In a different component, the same function also contains await getPageInfo({ page: 1 }) after the return .... To make this work, I need to pass a cal ...

Setting up Webpack for react-pdf in a Next.js application

In my Next.js application, I am utilizing the react-pdf library to generate and handle PDF files on the client side without involving the server. However, I am facing challenges in setting up Webpack for Next.js as I lack sufficient expertise in this area. ...

Tips for circumventing debounceTime in Angular

In my current setup, I am utilizing a text input along with a debounceTime pipe to ensure that server requests are not made too frequently while the user is typing: this.keyUp$ .pipe(debounceTime(500)) .subscribe(data => this.onInputChanged.emit ...

The AngularFireAuth.user observable does not trigger when combined with the withLatestFrom RxJS operator

When using the AngularFireAuth.user observable as the source observable, like in the example below: this.AngularFireAuth.user.subscribe((u) => console.log(u)) everything works fine. However, when I try to include it in the withLatestFrom operator, as s ...

What is the best way to send an email with a time-sensitive code (token) using Firebase?

Currently, I am developing an application that requires sending a verification code to users before they can proceed with certain actions. For instance, when users log in to the app, they need to enter their email, password (authenticated using Firebase au ...

Encountered an error while trying to generate the Component class for the ColorlibStepIcon from Material UI in TypeScript

I am trying to convert the ColorlibStepIcon functional component into a class component for my Stepper. Unfortunately, I have not been successful and keep encountering errors. I have attempted some changes but it is still not working as expected. You can ...

Testing the GET method in an Angular service: A guide

I'm currently facing an issue with my service method and unit test setup. Despite writing a unit test for the getter method, the coverage report indicates that this method is not covered. I would appreciate any advice on what might be going wrong in m ...

Using an asynchronous pipe filter with the ngFor loop in Angular 2 for efficient data

I have a JSON array that I need to iterate through in order to display data using an NGfor Loop. My goal is to apply filters after the data has been loaded to refine the results. The issue I am facing is that my pipe filter is returning 'cannot read p ...

In TypeScript Next.js 14 APP, object literals are limited to declaring existing properties

I encountered an error in my typescript next.js 14 APP. I need assistance resolving this issue, which states: Object literal may only specify known properties, and 'productPackages' does not exist in type '(Without<ProductCreateInput, Pr ...

Dynamic Angular form with nested elements

Need help creating a nested form following the example from angular.io documentation: https://stackblitz.com/angular/pbdkbbnmrdg The objective is to include two DropdownQuestion elements from question.service.ts within a child formgroup called "details", ...

What is the method for extending props from React.HTMLProps<HTMLButtonElement>?

"react": "^17.0.2", "typescript": "^4.2.4" Can someone help me understand how to extend props from React.HTMLProps? import { FC, HTMLProps } from 'react' export interface SliderButtonProps extends HTMLPro ...