首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack2 Angular2 ng-引导树抖动

Webpack2 Angular2 ng-引导树抖动
EN

Stack Overflow用户
提问于 2017-04-15 18:43:52
回答 2查看 1.2K关注 0票数 4

我目前正在angular2应用程序中使用angular2,并使用webpack2构建所有ts文件。

目前我只使用来自NgbModule的Modal组件,但在缩小的文件中,我仍然可以看到NbgAccordian和其他模块引用,这些引用在我的应用程序中没有使用

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

我试过import { NgbModule, NgbModal, NgbModalOptions, ModalDismissReasons, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';,但也没有像预期的那样工作。这是与摇动树有关还是与Ngbmodule的编写方式有关。从vendor.js文件中省略未使用模块的任何选项

vendor.ts

代码语言:javascript
复制
// 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

代码语言:javascript
复制
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)
            }
        })
    ]
});

必须更新我的模块,并更新组件文件以导入文件的深度链接引用,而不是根文件,以从ng引导程序中排除未使用的模块。

app.modules.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
import { NgbModalModule, NgbModal, NgbModalOptions, ModalDismissReasons, NgbActiveModal, NgbTooltipModule, NgbTooltipConfig, NgbAlertModule } from '@ng-bootstrap/ng-bootstrap';

跟随树的抖动配置

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-16 06:15:44

tl;dr;是指您只能从ng-自助中选择和选择使用的组件,但只需要导入所使用的组件。

如果您只是从ng-自助项目中使用单个模块,那么您应该只导入所使用的模块(而不是像今天一样导入整个NgbModule )。下面是如何(以模态为例):

代码语言:javascript
复制
import {NgbModalModule} from '@ng-bootstrap/ng-bootstrap';

...

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

最后,下面是柱塞中的一个活例子:http://plnkr.co/edit/3TdcMzPBXb3OKWYIQisG?p=preview

此外,在使用WebPack时,请确保只导入它在vendor.ts文件中使用的内容(因为import '@ng-bootstrap/ng-bootstrap';将带来所有组件);

票数 3
EN

Stack Overflow用户

发布于 2017-05-07 18:04:59

即使我遵循@pkozlowski.opensource的建议,我仍然在手风琴模块中看到一个ng模板错误。最后,我从嵌套路径导入,就像使用rxjs/Observable一样

代码语言:javascript
复制
import {NgbModalModule} from '@ng-bootstrap/ng-bootstrap/modal/modal.module';

...

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

希望这能帮上忙!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43429897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档