Utilizing Webpack's treeshaking to optimize your project by exporting const

Our tech stack includes:
- Angular 5.2.x
- Ionic 4.4.x
- Webpack 3.6.x

We have organized our app structure as follows:

app
  |__features
  |    |__Feature1
  |    |     |__Feature1Service.ts
  |    |     |__Feature1Dto.ts
  |    |     |__index.ts
  |    |
  |    |__Feature2
  |          |__Feature2Service.ts
  |          |__Feature2Dto.ts
  |          |__index.ts
  |
  |__core
       |__SomeCoreStuff.ts
       |__index.ts

In index files, we export the necessary elements from each feature like this:

import { Feature1Service } from './Feature1Service';
import { Feature1Dto } from './Feature1Dto';
export const fromFeature1 = { Feature1Service, Feature1Dto };

This allows us to utilize only the required modules in other features, such as in Feature2:

import { fromFeature1 } from '../Feature1';

//using Feature1Service without using Feature1Dto
fromFeature1.Feature1Service;

We are curious if webpack's tree shaking eliminates unused exports (e.g., Feature1Dto). If not, how much does it affect the size of our deployed JavaScript bundle?

Answer №1

The property <code>fromFeature1.Feature1Service
belongs to the object fromFeature1. It is not exported and cannot be removed during tree shaking if fromFeature1 is being used.

To enable tree shaking, it should be done as follows:

export { Feature1Service } from './Feature1Service';
export { Feature1Dto } from './Feature1Dto';

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

Tips for validating and narrowing a type in TypeScript using isNull instead of isNonNullable

I want to implement a universal type guard for entities returned from an API. The goal is to handle any null | undefined values by throwing an HttpStatus.NOT_FOUND error. Initially, I tried the following approach: export const entityOr404 = <T>(entit ...

Concealing a navigation tab with Angular4 in Typescript: A tutorial

I have successfully implemented 3 tabs in my Angular 4 project. At the moment, I am focusing on working with the first two tabs and planning to tackle the third tab in the near future. To keep things clean and organized, I am looking to use JavaScript/Typ ...

Angular Datatables unable to locate DataTables with Webpack Encore configuration

As I've been updating a Symfony application from version 3.4 to 4.2.2, everything has been going smoothly until I encountered an issue with getting DataTables to work through yarn installation and webpack encore using angular-datatables. The Setup Pr ...

Develop a carousel component using Vue.js

I'm currently working on a dashboard that needs to display cards, but I'm running into an issue. I want only four cards visible at a time, and when you click the arrow, it should move just one step to the next card. For example, if cards 1-4 are ...

Develop an Angular application and deploy it on an IIS server

I've been facing issues while trying to deploy my Angular app on IIS. After researching, I found that using the cli command ng build resulted in errors such as Module not found. Interestingly, when I modified the templateUrl in the component, the erro ...

What is the best way to incorporate data types into a React useReducer reducer function?

I originally had a useReducer function in pure react without TypeScript, but now I want to add types to it. Here is the useReducer reducer function in pure react without types: export const cartReducer = (state, action) => { switch (action.type) { ...

Tips for maintaining image dimensions on the screen using html and css?

Hello everyone! I am excited to be diving into the world of html and css, as I have a course lined up for next month. However, I currently have a project that I need to tackle on my own. One issue I'm facing is keeping the products within the screen s ...

Broadening Cypress.config by incorporating custom attributes using Typescript

I'm attempting to customize my Cypress configuration by including a new property using the following method: Cypress.Commands.overwrite('getUser', (originalFn: any) => { const overwriteOptions = { accountPath: `accounts/${opti ...

Avoiding Bootstrap loading in a Hyperledger Composer application

I am looking to eliminate the Bootstrap stylesheet from my Hyperledger Composer-compiled project. After compiling using yo hyperledger-composer and then running npm start, I noticed that when src/index.html is served at http://localhost:4200/, a Twitter B ...

Include a search query parameter in the URL by adding "?search=" to connect with a

In my react/typescript application, I have a client and server setup. The client requests data from the server and displays it using React. When making a request for data on the client side, this is how it's done: export const createApiClient = (): A ...

Tips for formatting nested Angular components in a visually pleasing manner:

Seeking guidance on the best approach for the following scenario: I have an angular component positioned at a specific route. Let's say the route is: /main-page Currently, the template spans the full width of the screen at this route. I want to add ...

Ways to imitate an export default function

//CustomConfigurator.ts export default function customizeConfig(name: string): string { // add some custom logic here return output; } //CustomUtility.ts import customizeConfig from './CustomConfigurator'; export default class CustomUtility ...

Importing multiple modules in Typescript is a common practice

I need to include the 'express' module in my app. According to Mozilla's documentation, we should use the following code: import { Application }, * as Express from 'express' However, when using it in TypeScript and VSCode, I enc ...

I am struggling to figure out why NativeWind props are not being recognized in my tsx file

Can someone help me understand why I can call className in jsx files but not tsx files? The error message displayed is: No overload matches this call. Overload 1 of 2, '(props: ViewProps): View', gave the following error. Type '{ children: ...

Tips for optimizing compilation of TypeScript during the packaging process using Electron Forge

Upon opening the app, there is a momentary delay with a blank screen before it fully loads. I have utilized electron-forge's react-typescript template. While I am able to successfully create a dmg or deb file, I have observed that when running the p ...

Issue with unresolved module in ESLint

Currently, I am utilizing the vss-web-extension-sdk in my project. To ensure the validity of my files, I have integrated ESLint along with eslint-plugin-import and eslint-import-resolver-typescript. import { WidgetSettings, WidgetStatus } from "TFS/Dashbo ...

Perform a unit test on a variable declaration within the ngOnInit() function

As I dive into the world of TDD and revisit all the Angular tutorials available to me, I am determined this time to achieve 100% code coverage in my unit tests. Despite my efforts, I have stumbled upon a question that seems too simple to find an answer to ...

tsconfig.json configuration file for a project containing both `src` and `tests` directories

Looking to achieve a specific project structure: - tsconfig.json - src - app.ts - tests - appTest.ts - appTest.js - dist - app.js If the tests folder did not exist, this tsconfig.json configuration would suffice: { "compilerOptions": ...

"Enhance your Vue.js application with the powerful capabilities of vue3-easy

I am currently working on a Vue.js project utilizing the "vue3-easy-data-table" library and following the style recommendations outlined on this particular webpage: Despite attempting to apply the following CSS properties: --easy-table-body-even-row-font ...

What is the best way to convert a date to ISO 8601 format using JavaScript? Are there any built-in functions or methods in

Currently, I am using this function to set the duration: const setDuration = () => { const currentDate = new Date(); const newDate = new Date(currentDate.getTime()); const year = newDate.getUTCFullYear(); const m ...