Is Angular 2+ responsible for loading the entire module or only the exported components within it?

I'm dealing with a situation where I have a large module but only need to export one specific component. I'm wondering if Angular loads the entire module or just the exported components, as I want to optimize performance without compromising the project's structure by moving the component to a common module. Any advice on how to approach this?

To illustrate, let's consider ModuleA which contains 3 components but exports only ComponentA:

@NgModule({
  // ...
  exports: [
    ComponentA
  ],
  declarations: [
    ComponentA,
    ComponentB,
    ComponentC
  ]
})
export class ModuleA { }

In ModuleB, I import ModuleA to utilize ComponentA:

@NgModule({
  // ...
  imports: [
    ModuleA
  ]
})
export class ModuleB { }

My question is, when ModuleB is loaded, does it load all components from ModuleA or just ComponentA?

Answer №1

In case you're wondering:

module.ts

export const foo;

export const bar;

export const baz;

Then component.ts

import { foo } from './module'

It's my understanding that during development all members will be accessible, but when creating production builds Angular AoT building will eliminate the unnecessary ones using Webpack.

You can learn more about Webpacks tree shaking mechanisms here: https://webpack.js.org/guides/tree-shaking/

For information on the angular AoT compiler, check out their documentation here: https://angular.io/guide/aot-compiler

In any case, give a prod build a try and if everything functions as expected, you should be good to go :)

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

A guide to saving an ArrayBuffer as a file using JavaScript

I am currently developing a file uploader for the Meteor framework. The approach involves breaking down the file on the client side from an ArrayBuffer into small packets of 4096 bits, which are then sent to the server through a Meteor.method. The abridge ...

There was an error message saying "Unable to locate property 'getElementById' in undefined"

I am facing an issue with a jQuery filter function, and the error message it's showing is: Cannot read property 'getElementById' of undefined function CheckFilter() { var input, filter, table, tr, td, i, searchFilter; input = ...

Steps to connect two drop-down menus and establish a starting value for both

In the scope, I have a map called $scope.graphEventsAndFields. Each object inside this map is structured like so: {'event_name': ['field1', 'field2', ...]} (where the key represents an event name and the value is an array of f ...

Uncertain about where and how to properly register a service in Angular 2

All: Today was my first day diving into Angular2, as I embarked on the official TUTORIAL at part 6: Routing Around the App https://angular.io/docs/ts/latest/tutorial/toh-pt5.html Within the Create AppComponent section, it is mentioned: Add HeroService ...

The Angular 18 component was brought in as a dependency and integrated into the project, but unfortunately, it

How do I properly integrate the NavbarComponent into the template of the app component? https://i.sstatic.net/pBKTzUnf.png app.component.html <app-navbar></app-navbar> <router-outlet></router-outlet> main.ts import {CdkTableModul ...

Issue with Angular 2 Routing: Unable to find a matching route

Currently, I'm in the process of developing an Angular 2+ application that requires routing. One of the requirements is for the color scheme of the entire app to change based on a URL parameter input. In my app.module.ts file, I have the following co ...

Guide to encapsulating a container within a map function using a condition in JSX and TypeScript

Currently, I am working with an array of objects that are being processed by a .map() function. Within this process, I have a specific condition in mind - if the index of the object is greater than 1, it should be enclosed within a div element with a parti ...

Change typescript so that it shows "require" instead of "import" when outputting

Currently, I am converting TypeScript code to JavaScript for an application that is specifically targeting Node v.14. My goal is to have the output contain require statements instead of import statements. This is what my configuration file looks like: { ...

Retrieving session data from a different tab and website

The task at hand involves managing a PHP website (mysite.com) and an ASP.NET website (shop.mysite.com). The client's request is to implement a single sign-on solution for both sites. My approach is to develop a function on the ASP.NET site that can pr ...

How can JavaScript automatically calculate the sum of various numbers entered into multiple input fields?

I would like to automatically add different numbers entered in multiple input fields using JavaScript. Is there a way to calculate the sum of numbers entered in various input fields using JavaScript? "The input field consists of n numbers..." and I am ut ...

Troubleshooting issues with sorting and pagination in Angular Material table functionality

I am experiencing an issue with sorting and pagination using an Angular material table. The data is being fetched from a store as an observable and successfully displayed in the table. Even though the column names for sorting match the column definitions, ...

Aurelia's navigation feature adds "?id=5" to the URL instead of "/5"

I have set up my Aurelia Router in app.ts using the configureRouter function like this: configureRouter(config, router: Router) { config.map([ { route: ['users', 'users/:userId?'], na ...

Transitioning to TypeScript: Why won't my function get identified?

I am in the process of transitioning a functional JavaScript project to TypeScript. The project incorporates nightwatch.js Below is my primary test class: declare function require(path: string): any; import * as dotenv from "dotenv"; import signinPage = ...

Converting HTML Javascript to JAVA with the help of Selenium

Can someone help me extract the value of h1 as a string using selenium? Check out the HTML javascript snippet below- <script type="text/javascript"> $(window).load(function() { var $windowHeight = $(window).height() -12; $(" ...

What is the best way to include temporary attributes on a mongoose object solely for the purpose of a response, without saving them to the database

I am looking to add extra temporary properties with additional data to the response but have encountered some difficulties. 'use strict'; var mongoose = require('mongoose'); var express = require('express'); var app = expres ...

Is it possible to set environment variables in Next.js outside of the pages directory?

Are there alternative methods for setting environment variables outside of a pages directory in NextJS? I have a utility function that defines the base API route in one centralized location. However, since this is a utility function and not associated with ...

When a div is modified through an ajax function, I aim to activate a JavaScript function

Is there a way to automatically execute a javascript function once the status updates to "Upload successful"? I am uncertain about how to accomplish this task. Following a multi file upload, the status will switch based on whether it was successful or en ...

Link the source data within a v-for loop to the props value

I have brought in several JSON files containing different sets of data. For my parent.vue input, I aim to iterate through these JSON files dynamically. <div v-for="(item, index) in <!-- JSONFile + Rank -->" :key="index"> T ...

Unable to display the content

Why isn't the text expanding when I click "see more"? Thanks XHTML: <div id="wrap"> <h1>Show/Hide Content</h1> <p> This code snippet demonstrates how to create a show/hide container with links, div eleme ...

Creating a unique custom complex template typeahead implementation with Angular 2 and Ng2-Bootstrap

I encountered an issue with the ng2-bootstrap typeahead implementation and have created a plunker to demonstrate the problem. Check out the plunker example here <template #customItemTemplate let-model="item" let-index="index"> <h5>This is ...