gpt4 book ai didi

angular - 如何在 Webpack 中使用 tree shaking?

转载 作者:太空狗 更新时间:2023-10-29 16:51:09 26 4
gpt4 key购买 nike

我刚刚使用 Angular 2(版本 rc.2)应用程序(使用 Typescript)更新到 webpack 2.1.0-beta.15,但我想知道如何使用 tree shaking 功能。我读到它应该“开箱即用”,但我仍然有一个 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"
]
}

包.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'),但我相信两者相对较小。根据webpack visualizer , Jquery 和 Lodash 分别占 bundle 大小的 6.2% 和 12%。 Angular 使用了包大小的 49%。

我错过了什么?

最佳答案

对于 treeshaking 你必须使用 typescript 2

npm i typescript@next --save-dev

支持

tsconfig.json

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

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

关于angular - 如何在 Webpack 中使用 tree shaking?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38283985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com