问题
I'm currently using https://github.com/ng-bootstrap/ng-bootstrap in my angular2 application and use webpack2 to build all ts files.
i'm only using Modal Component from NgbModule for now, but in minified file i can still see NbgAccordian and other modules reference which is not used in my app
@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.15
i tried import { NgbModule, NgbModal, NgbModalOptions, ModalDismissReasons, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
but didn't worked as expected also. Is this something to do with tree shaking or the way Ngbmodule is written. Any options to omit unused modules from 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 ---------------------
Had to update my modules, and component files to import deeplink reference of files instead of root to exclude unused modules from ng bootstrap
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 ------------
Followed tree shaking configuration from
https://github.com/Andrey-Pavlov/angular2-webpack-starter/blob/d0a225851e6d63b03a21ad6b7a71552a941229ef/config/webpack.common.js#L220
回答1:
tl;dr; is that you can pick and choose only components that you use from ng-bootstrap but you need to import only what you are using.
If you are just using individual module(s) from the ng-bootstrap project than you should just import the modules used (instead of the whole NgbModule
as you do today). Here is how (using modal as an example):
import {NgbModalModule} from '@ng-bootstrap/ng-bootstrap';
...
@NgModule({
declarations: [AppComponent, ...],
imports: [NgbModalModule.forRoot(), ...],
bootstrap: [AppComponent]
})
export class AppModule {
}
Finally here is a live example in plunker: http://plnkr.co/edit/3TdcMzPBXb3OKWYIQisG?p=preview
Also, when using WebPack make sure that you import only what it used in your vendor.ts
file (as import '@ng-bootstrap/ng-bootstrap';
will bring all the components);
回答2:
Even though I was following @pkozlowski.opensource recommendation, I was still seeing an ng-template error in the Accordion module. I ended up importing from a nested path, as I would do with rxjs/Observable
:
import {NgbModalModule} from '@ng-bootstrap/ng-bootstrap/modal/modal.module';
...
@NgModule({
declarations: [AppComponent, ...],
imports: [NgbModalModule.forRoot(), ...],
bootstrap: [AppComponent]
})
export class AppModule {
}
Hope that helps!
来源:https://stackoverflow.com/questions/43429897/webpack2-angular2-ng-bootstrap-tree-shaking