如何在Webpack中使用摇树? [英] How to use tree shaking in Webpack?

查看:291
本文介绍了如何在Webpack中使用摇树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just updated to webpack 2.1.0-beta.15 with an Angular 2 (version rc.2) app (with Typescript), but I was wondering how to use the tree shaking feature. I read it should work "out of the box", but I'm still having a bundle of 1.7Mb for a very simple application, so probably I'm doing something wrong.

这是我到目前为止所拥有的:

This is what I have so far:

tsconfig.json

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

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

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运行构建.

I'm using fairly standard import statements such as import {FORM_DIRECTIVES,REACTIVE_FORM_DIRECTIVES} from '@angular/forms' and trying to import only what I need (import 'rxjs/add/operator/map';) and running the build with npm run build:prod with the environment variable PROD_ENV=1.

也许是jquery或lodash之类的问题引起了问题(import * as $ from 'jquery'import {orderBy} from 'lodash'),但我相信两者都相对较小.根据 webpack可视化工具,Jquery和Lodash分别占捆绑包大小的6.2%和12% , 分别. Angular使用了包大小的49%.

Maybe something like jquery or lodash is causing issues (import * as $ from 'jquery' or import {orderBy} from 'lodash'), but I believe both are relatively small. According to the webpack visualizer, Jquery and Lodash account for 6.2% and 12% of the bundle size, respectively. Angular uses 49% of the bundle size.

我想念什么?

推荐答案

对于树状握手,您必须使用打字稿2

For treeshaking you have to use typescript 2

npm i typescript@next --save-dev

支持

tsconfig.json

tsconfig.json

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

这是因为es6模块是可以静态分析的,并且Webpack可以确定使用了哪些依赖项,哪些没有使用.

This is because es6 modules are statically analyzable and Webpack can determine which of your dependencies are used and which are not.

这篇关于如何在Webpack中使用摇树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆