angular最新版本 - angular版本




如何在Webpack中使用树摇动? (2)

我刚刚使用Angular 2(版本rc.2)app(带有Typescript)更新到webpack 2.1.0-beta.15,但我想知道如何使用树摇动功能。 我读过它应该“开箱即用”,但我仍然有一个1.7Mb的捆绑一个非常简单的应用程序,所以可能我做错了。

这是我到目前为止:

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

的package.json

{
  "name": "angular2-quickstart",
  "version": "1.0.0",
  "scripts": {
    "build": "webpack --progress",
    "build:prod": "webpack -p --progress --optimize-minimize",
    "postinstall": "typings install",
    "serve": "webpack-dev-server --inline --progress --output-public-path=/dist/"
  },
  "license": "ISC",
  "dependencies": {
    "@angular/common": "2.0.0-rc.2",
    "@angular/compiler": "2.0.0-rc.2",
    "@angular/core": "2.0.0-rc.2",
    "@angular/forms": "0.1.0",
    "@angular/http": "2.0.0-rc.2",
    "@angular/platform-browser": "2.0.0-rc.2",
    "@angular/platform-browser-dynamic": "2.0.0-rc.2",
    "@angular/router": "2.0.0-rc.2",
    "@angular/router-deprecated": "2.0.0-rc.2",
    "@angular/upgrade": "2.0.0-rc.2",
    "angular-pipes": "1.4.0",
    "bootstrap": "3.3.6",
    "core-js": "2.4.0",
    "file-loader": "0.9.0",
    "jquery": "2.2.3",
    "lodash": "4.13.1",
    "reflect-metadata": "0.1.3",
    "rxjs": "5.0.0-beta.6",
    "script-loader": "0.7.0",
    "style-loader": "0.13.1",
    "url-loader": "0.5.7",
    "zone.js": "0.6.12"
  },
  "devDependencies": {
    "concurrently": "^2.0.0",
    "css-loader": "0.23.1",
    "html-loader": "0.4.3",
    "json-loader": "^0.5.4",
    "raw-loader": "0.5.1",
    "ts-loader": "0.8.2",
    "typescript": "1.8.10",
    "typings": "1.0.4",
    "webpack": "^2.1.0-beta.15",
    "webpack-dev-server": "^2.1.0-beta",
  }
}

webpack.config.js

var webpack = require('webpack');

var PROD = JSON.parse(process.env.PROD_ENV || '0');

module.exports = {
    entry: './app/main.ts',
    output: {
        path: './dist',
        filename: 'app.bundle.js'
    },
    module: {
        loaders: [
        { test: /\.json$/, loader: 'json', include: [/node_modules/] },
        { test: /\.ts$/, loader: 'ts' },
        { test: /\.html$/, loader: 'html' },
        { test: /\.css$/, loader: 'style!css' },
        { test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=100000' }
        ]
    },
    resolve: {
        extensions: ['', '.js', '.ts', '.css']
    },
    'htmlLoader': {
        caseSensitive: true
    },
    plugins: PROD ? [
        new webpack.optimize.UglifyJsPlugin({
            compress: { warnings: false }
        })
    ] : []
}

我正在使用相当标准的导入语句,例如import {FORM_DIRECTIVES,REACTIVE_FORM_DIRECTIVES} from '@angular/forms'并尝试仅导入我需要的内容( import 'rxjs/add/operator/map'; )并使用npm run build:prod运行构建npm run build:prod ,环境变量PROD_ENV = 1。

也许像jquery或lodash这样的东西会导致问题( import * as $ from 'jquery'import {orderBy} from 'lodash' ),但我相信两者都相对较小。 根据webpack可视化工具 ,Jquery和Lodash分别占捆绑大小的6.2%和12%。 Angular使用49%的束大小。

我错过了什么?


切换到angular-cli目前似乎是继续使用Angular应用程序的方式。 它基于webpack,现在甚至支持树摇动功能。 只需使用ng build --prod


对于treeshaking,你必须使用typescript 2

npm i [email protected] --save-dev

哪个支持

tsconfig.json

{
  "compilerOptions": {
    "module": "es6",
    "target": "es5"
   }
}

这是因为es6模块是静态可分析的,Webpack可以确定使用哪些依赖项,哪些不依赖。







bundle