简体   繁体   English

Angular2 + Webpack:如何优化供应商捆绑

[英]Angular2 + Webpack : how to optimise vendor bundle

I have an MEAN application client side is in anular2 build webpack. 我有一个MEAN应用程序客户端是在anular2构建webpack中。

When I serve index.html from server for initial request it takes loading time 5 or 6 even more seconds when application in production, because of vendor modules JS file is more than 3MB. 当我从服务器为初始请求提供index.html时,由于供应商模块JS文件大于3MB,因此在生产中应用程序加载时需要花费5或6秒钟甚至更多的时间。

How I can optimise this thing I want to separate vendor's JS file. 我要如何优化此东西,我想分离供应商的JS文件。 Following is my webpack.config.js 以下是我的webpack.config.js

const webpack = require('webpack');
const production = process.env.NODE_ENV === "production";
const autoprefixer = require('autoprefixer');
const path = require("path");

const config = {

  entry: {
    'app': './client/app.ts',
    'vendor': './client/vendor.ts'   },

  debug: !production,

  devtool: production ? null : "source-map",

  output: {
    path: "./dist",
    filename: "bundle.js"   },

  plugins: [
    new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.bundle.js')   ],

  resolve: {
    extensions: ['', '.ts']   },

  module: {
    loaders: [
      { test: /\.ts$/, loader: 'ts-loader' },
      { test: /\.html$/, loader: 'raw'},
      { test: /\.scss$/, include: [ path.resolve(__dirname, 'client/app/components') ], loader: 'style!css!postcss!sass' }
    ],
    noParse: [ path.join(__dirname, 'node_modules', 'angular2', 'bundles') ]   },   postcss: [ autoprefixer ] };

module.exports = config;

The following is my vendor.ts file 以下是我的vendor.ts文件

import 'core-js/es6';
import 'core-js/es7/reflect';
require('zone.js/dist/zone');

import 'ts-helpers';

import '@angular/platform-browser-dynamic';
import '@angular/core';
import '@angular/common';
import '@angular/http';
import '@angular/router';

import 'socket.io-client';

// RxJS
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';

import './assets/vendor/bootstrap';
import './assets/vendor/fontawesome.scss';

Building project I can get bundle.js and bundle.map.js contains my application js vendor.bundle.js and vendor.bundle.map.js will contain other third party js 构建项目,我可以获取bundle.jsbundle.map.js包含我的应用程序js vendor.bundle.jsvendor.bundle.map.js将包含其他第三方js

I want to compile this vendor.js each library separately as well as all scss should be compiled in separate style.css in ditribution. 我想分别编译每个库的vendor.js ,以及所有scss都应在独立的style.css中进行分发。

Thank you all. 谢谢你们。

I can see some areas to improve: 我可以看到一些需要改进的地方:

  1. I can't see any minification plugin in your code so it will give you a huge boost. 我在您的代码中看不到任何缩小插件,因此它将为您带来巨大的推动。 See UglifyJsPlugin 参见UglifyJsPlugin
  2. You added directly all the angular modules (even they may not be needed): 您直接添加了所有的角度模块(甚至可能不需要):

     import '@angular/platform-browser-dynamic'; import '@angular/core'; import '@angular/common'; import '@angular/http'; import '@angular/router'; 

    It's better to just import specific things when needed like: 最好在需要时仅导入特定内容,例如:

     import {Component} from '@angular/core'; 
  3. Having imports divided like above you'll be able to get the benefits of upcoming Webpack 2 . 像上面一样划分进口,您将可以得到即将推出的Webpack 2的好处。 The most important part is: 最重要的部分是:

The static nature of ES6 Modules allows some new kind of optimizations. ES6模块的静态性质允许进行一些新的优化。 In example in many cases it's possible to detect which exports are used and which aren't used. 在很多情况下,例如,可以检测使用了哪些出口,哪些未使用。

In cases in which webpack can say for sure that an export isn't used it omits the statement which exposes the export to other modules. 如果webpack可以肯定地说不使用导出,则会忽略将导出暴露给其他模块的语句。 Later the minimizer may flag the declaration as unused and omits it. 以后最小化器可以将声明标记为未使用并忽略它。

I think all of this together can make your production app much thinner without splitting libraries. 我认为所有这些都可以使您的生产应用程序更薄,而无需拆分库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM