"Optimizing Angular2 with Tree Shaking using Webpack2 and ng

In my angular2 application, I am currently utilizing https://github.com/ng-bootstrap/ng-bootstrap with webpack2 to compile all ts files.

At the moment, I am only using the Modal Component from NgbModule. However, even in the minified file, I can still see references to NbgAccordian and other modules that are not utilized in my app.

@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.15

I attempted to use

import { NgbModule, NgbModal, NgbModalOptions, ModalDismissReasons, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
, but it did not work as expected. Could this be related to tree shaking or how Ngbmodule is structured? Are there any options to exclude unused modules from the vendor.js file?

vendor.ts

// Angular

import '@angular/core';
import '@angular/common';
import '@angular/forms';
import '@angular/http';
import '@angular/platform-browser';
import '@angular/platform-browser-dynamic';
import '@angular/router';


// RxJS
import 'rxjs';
// Other vendors for example jQuery, Lodash or Bootstrap
// You can import js, ts, css, sass, ...

import  '@ng-bootstrap/ng-bootstrap';
import  'moment/moment';
import  'angular2-toaster/angular2-toaster';
import  'angular2-moment';
import  'ng2-tag-input';

import 'ng2-img-cropper';

webpack.prod.js

var webpack = require('webpack');
var webpackMerge = require('webpack-merge');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var commonConfig = require('./webpack.common.js');
var CompressionPlugin = require("compression-webpack-plugin");
var helpers = require('./helpers');

var packageJson = require('../../package.json');
var version = packageJson.version;
const ENV = process.env.NODE_ENV = process.env.ENV = 'production';
var drop_console = true;

//https://github.com/mishoo/UglifyJS2#mangle
//https://github.com/mishoo/UglifyJS2#compressor-options
https://github.com/mishoo/UglifyJS2
module.exports = webpackMerge(commonConfig, {
    devtool: "source-map",
    plugins: [
        new webpack.LoaderOptionsPlugin({

            minimize: true,
            debug: false,
            options: {

                /**
                 * Html loader advanced options
                 *
                 * See: https://github.com/webpack/html-loader#advanced-options
                 */
                // TODO: Need to workaround Angular 2's html syntax => #id [bind] (event) *ngFor
                htmlLoader: {
                    minimize: true,
                    removeAttributeQuotes: false,
                    caseSensitive: true,
                    customAttrSurround: [
                        [/#/, /(?:)/],
                        [/\*/, /(?:)/],
                        [/\[?\(?/, /(?:)/]
                    ],
                    customAttrAssign: [/\)?\]?=/]
                }

            }
        }),
        new webpack.NoErrorsPlugin(),
        new webpack.optimize.UglifyJsPlugin({ // https://github.com/angular/angular/issues/10618
            minimize: true,
            sourceMap: true,

             // Don't beautify output (enable for neater output)
            beautify: false,

            // Eliminate comments
            comments: false,



            mangle:  {
                toplevel : true,
                screw_ie8: true,
                keep_fnames: false
            },
            compress: {
                screw_ie8: true,

                dead_code : true,

                unused : true,

                warnings: false,

                // Drop `console` statements
                drop_console: drop_console
            }

        }),
        new CompressionPlugin({
            regExp: /\.css$|\.html$|\.js$|\.woff$|\.map$/,
            algorithm: "gzip",
            threshold: 2 * 1024,
            minRatio: 0.8
        }),
        new webpack.DefinePlugin({
            'process.env': {
                'ENV': JSON.stringify(ENV)
            }
        })
    ]
});

----------------------------- Updates on 04/20/2017 ---------------------

To exclude unused modules from ng bootstrap, I had to update my modules and component files to import deeplink reference of files instead of root.

app.modules.ts

import {  NgbModalModule }                            from '@ng-bootstrap/ng-bootstrap/modal/modal.module';
import {  NgbTooltipModule}                            from '@ng-bootstrap/ng-bootstrap/tooltip/tooltip.module';
import {  NgbAlertModule }                            from '@ng-bootstrap/ng-bootstrap/alert/alert.module';

app.component.ts

import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap/modal/modal';
import { ModalDismissReasons }                  from '@ng-bootstrap/ng-bootstrap/modal/modal-dismiss-reasons';
import { NgbActiveModal} from '@ng-bootstrap/ng-bootstrap/modal/modal-ref';
import { NgbTooltipConfig }                        from "@ng-bootstrap/ng-bootstrap/tooltip/tooltip-config";

vendor.ts

import { NgbModalModule, NgbModal, NgbModalOptions, ModalDismissReasons, NgbActiveModal, NgbTooltipModule, NgbTooltipConfig, NgbAlertModule } from '@ng-bootstrap/ng-bootstrap';

------ Further Updates ------------

I followed the tree shaking configuration from

https://github.com/Andrey-Pavlov/angular2-webpack-starter/blob/d0a225851e6d63b03a21ad6b7a71552a941229ef/config/webpack.common.js#L220

Answer №1

In summary, you have the flexibility to choose specific components from ng-bootstrap and only import what you need. For example, if you are only using certain modules from the project, you can import those individual modules instead of importing the entire NgbModule. Here's an example using modal:

import {NgbModalModule} from '@ng-bootstrap/ng-bootstrap';

...

@NgModule({
  declarations: [AppComponent, ...],
  imports: [NgbModalModule.forRoot(), ...],
  bootstrap: [AppComponent]
})
export class AppModule {
}

You can see a live example in plunker here: http://plnkr.co/edit/3TdcMzPBXb3OKWYIQisG?p=preview

If you are using WebPack, be sure to only import the necessary components in your vendor.ts file, as importing @ng-bootstrap/ng-bootstrap will bring in all components.

Answer №2

Despite following @pkozlowski.opensource's suggestion, I encountered an ng-template error within the Accordion module. To resolve this issue, I had to import from a nested path, similar to how I would import rxjs/Observable:

import {NgbModalModule} from '@ng-bootstrap/ng-bootstrap/modal/modal.module';

...

@NgModule({
  declarations: [AppComponent, ...],
  imports: [NgbModalModule.forRoot(), ...],
  bootstrap: [AppComponent]
})
export class AppModule {
}

I hope this solution proves helpful!

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

Leveraging es6-promise in conjunction with TypeScript 2.1 and ES5 Webpack for streamlined async/await functionality

Utilizing es6-promise with TypeScript version 2.1.1 that targets ES5 and webpack in my project has presented some challenges. app.ts import "es6-promise/auto"; export class Foo { bar() : Promise<string>{ return Promise.resolve("baz"); ...

ESLint detecting error with returning values in async arrow functions

Currently facing a minor inconvenience instead of a major problem. Here is the code snippet causing the issue: export const getLoginSession = async (req: NextApiRequest): Promise<undefined | User> => { const token = getTokenCookie(req) if (!t ...

How to dynamically assign tab indices in Angular's mat-tab-group using ngFor

In my angular web application, I have a tab group that requires a mandatory tab (for posting a new object) and dynamic tabs (for editing existing objects). I am looking to set specific tabindex values for the dynamic tabs in order to have one of them disp ...

Maintain Symbolic Links in Angular Libraries

Hey there! I'm facing an issue with creating an Angular 8 Library using ng-cli. I'm struggling to maintain symlinks when linking my external application with npm link. I attempted to modify my angular.json file like this: "build": { "bui ...

Can you please provide the Typescript type of a route map object in hookrouter?

Is there a way to replace the 'any' type in hookrouter? type RouteMap = Record<string, (props?: any) => JSX.Element>; Full Code import { useRoutes, usePath, } from 'hookrouter' //// HOW DO I REPLACE any??? type RouteMap = ...

Angular Material Themes - retrieve the current main color shade

My current setup includes two themes: Theme 1: Light $candy-app-primary: mat-palette($mat-blue, A700); $candy-app-accent: mat-palette($mat-orange, A200, A100, A400); $candy-app-warn: mat-palette($mat-red); $candy-app-theme: mat-light-theme($candy-app ...

Using Angular to access specific child elements from a JSON object

I possess this specific item: elements120: { "data": { "name": "120", "type": "120" }, "children": [ { "data": { "nam ...

Creating an external link in Angular with query parameters

I have created an app where users have their addresses listed, and I want to implement a feature that allows me to open Google Maps when clicking on the address. However, I am currently facing an issue where instead of getting the actual value of {{ this. ...

Excluding properties based on type in Typescript using the Omit or Exclude utility types

I am looking to create a new type that selectively inherits properties from a parent type based on the data types of those properties. For instance, I aim to define a Post type that comprises only string values. type Post = { id: string; title: string ...

What is the approach of Angular 2 in managing attributes formatted in camelCase?

Recently, I've been dedicating my time to a personal project centered around web components. In this endeavor, I have been exploring the development of my own data binding library. Progress has been made in creating key functionalities akin to those f ...

Using jest-dom without Jest is definitely an interesting challenge that many developers may

Can anyone help me with extending Typescript interfaces? I have come across a situation that I am trying to solve. In my tests, I am utilizing expect without using Jest directly (I installed it separately and it functions properly). Now, I am interested ...

Oops! The API request was denied with error code 401 - Unauthorized in React

I have been working on incorporating an API into my front-end project using React/Typescript. The documentation for the API specifies that authorization requires a key named token with a corresponding value, which should be included in the header. To stor ...

Issue encountered during unit testing: "Error occurred: 'Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1'"

I am encountering an issue while trying to perform unit testing on a service. The error that I am seeing when running the test is: Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1 at MapSubscriber.project (auth.service ...

Error: The element 'scrollable' is not recognized in Angular2

I recently updated my angular2 project to the final release after previously using angular2 RC5 for development. However, I encountered an error message stating "scrollable is not a known element." If I change <scrollable><!--- angular code -- ...

Enhancing native JavaScript types in TypeScript 1.8 with the power of global augmentation

Currently, I am working on expanding the capabilities of native JavaScript types using the new global augmentation feature in TypeScript 1.8, as detailed in this resource. However, I'm encountering difficulties when the extension functions return the ...

Guide to sending jQuery data back to main class in TypeScript

Hi everyone, I'm diving into the world of typescript and JQuery. I have a simple question. In my typescript class called DatePicker, I am calling a function. Here's a snippet of the code: Class DatePicker { private pickerData; public update( ...

Encountering 'null' error in template with Angular 4.1.0 and strictNullChecks mode

After updating Angular to version 4.1.0 and activating "strictNullChecks" in my project, I am encountering numerous errors in the templates (.html) that look like this: An object may be 'null' All these errors are pointing to .html templat ...

Change the background color of a MUI ToggleButton based on a dynamic selection

const StyledToggleButton = styled(MuiToggleButton)(({ selectedColor }) => ({ "&.Mui-selected, &.Mui-selected:hover": { backgroundColor: selectedColor, } })); const FilterTeam = (props) => { const [view, setView] = ...

"Encountered a type error with the authorization from the credentials provider

Challenge I find myself utilizing a lone CredentialsProvider in next-auth, but grappling with the approach to managing async authorize() alongside a customized user interface. The portrayal of the user interface within types/next-auth.d.ts reads as follo ...

Tips on ensuring all fields are mandatory in a form

Currently, I am in the process of working on my Angular2 project. Specifically, I have created a form and my intention is to make all fields required. However, I encountered an issue when attempting to make the Title field mandatory as it is not displaying ...