如何在Webpack中使用树摇动?

时间:2016-07-09 16:39:43

标签: angular webpack bundle

我刚刚使用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';)并使用环境变量PROD_ENV = 1运行带npm run build:prod的构建

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

我错过了什么?

3 个答案:

答案 0 :(得分:5)

对于treeshaking,你必须使用typescript 2

npm i typescript@next --save-dev

支持

tsconfig.json

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

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

答案 1 :(得分:1)

根据Core团队在该问题上的评论,

Tree Shking并未随Webpack 2和is still blocked一起提供,没有明确的前进方向。我担心你可能会穿The Emperor's New Clothes

答案 2 :(得分:0)

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