Authored by shijian

web pack性能提优

@@ -146,3 +146,4 @@ public/bundle/* @@ -146,3 +146,4 @@ public/bundle/*
146 nbproject/* 146 nbproject/*
147 .DS_Store 147 .DS_Store
148 .devhost 148 .devhost
  149 +.happypack/
@@ -31,6 +31,7 @@ @@ -31,6 +31,7 @@
31 "express": "^4.14.0", 31 "express": "^4.14.0",
32 "express-session": "^1.14.1", 32 "express-session": "^1.14.1",
33 "handlebars-loader": "^1.5.0", 33 "handlebars-loader": "^1.5.0",
  34 + "happypack": "^3.1.0",
34 "influxdb-winston": "^1.0.1", 35 "influxdb-winston": "^1.0.1",
35 "lodash": "^4.15.0", 36 "lodash": "^4.15.0",
36 "memcached": "^2.2.1", 37 "memcached": "^2.2.1",
@@ -46,6 +47,7 @@ @@ -46,6 +47,7 @@
46 "vue-loader": "^8.3.0", 47 "vue-loader": "^8.3.0",
47 "vue-template-compiler": "^2.3.3", 48 "vue-template-compiler": "^2.3.3",
48 "webpack-uglify-parallel": "^0.1.3", 49 "webpack-uglify-parallel": "^0.1.3",
  50 + "webpack-visualizer-plugin": "^0.1.11",
49 "winston": "^2.2.0", 51 "winston": "^2.2.0",
50 "winston-daily-rotate-file": "^1.3.0", 52 "winston-daily-rotate-file": "^1.3.0",
51 "yoho-jquery": "^2.2.4", 53 "yoho-jquery": "^2.2.4",
@@ -3,6 +3,24 @@ const ExtractTextPlugin = require('extract-text-webpack-plugin'); @@ -3,6 +3,24 @@ const ExtractTextPlugin = require('extract-text-webpack-plugin');
3 const webpack = require('webpack'); 3 const webpack = require('webpack');
4 const shelljs = require('shelljs'); 4 const shelljs = require('shelljs');
5 const _ = require('lodash'); 5 const _ = require('lodash');
  6 +const HappyPack = require('happypack');
  7 +const os = require('os');
  8 +
  9 +const happyThreadPool = HappyPack.ThreadPool({ // eslint-disable-line
  10 + size: os.cpus().length
  11 +});
  12 +
  13 +const hbsLoader = {
  14 + loader: 'handlebars-loader',
  15 + options: {
  16 + helperDirs: [
  17 + path.join(__dirname, '../js/common/helpers')
  18 + ],
  19 + partialDirs: [
  20 + path.join(__dirname, '../../doraemon/views/partial')
  21 + ]
  22 + }
  23 +};
6 24
7 const getEntries = () => { 25 const getEntries = () => {
8 const entries = { 26 const entries = {
@@ -59,7 +77,7 @@ module.exports = { @@ -59,7 +77,7 @@ module.exports = {
59 test: /\.js$/, 77 test: /\.js$/,
60 exclude: /(node_modules|bower_components)/, 78 exclude: /(node_modules|bower_components)/,
61 use: { 79 use: {
62 - loader: 'babel-loader' 80 + loader: 'happypack/loader?id=js'
63 } 81 }
64 }, { 82 }, {
65 test: /\.css$/, 83 test: /\.css$/,
@@ -76,19 +94,21 @@ module.exports = { @@ -76,19 +94,21 @@ module.exports = {
76 }, { 94 }, {
77 test: /\.hbs$/, 95 test: /\.hbs$/,
78 use: [{ 96 use: [{
79 - loader: 'handlebars-loader',  
80 - options: {  
81 - helperDirs: [  
82 - path.join(__dirname, '../js/common/helpers')  
83 - ],  
84 - partialDirs: [  
85 - path.join(__dirname, '../../doraemon/views/partial')  
86 - ]  
87 - } 97 + loader: 'happypack/loader?id=hbs'
88 }] 98 }]
89 }] 99 }]
90 }, 100 },
91 plugins: [ 101 plugins: [
  102 + new HappyPack({
  103 + id: 'js',
  104 + threadPool: happyThreadPool,
  105 + loaders: ['babel-loader'],
  106 + }),
  107 + new HappyPack({
  108 + id: 'hbs',
  109 + threadPool: happyThreadPool,
  110 + loaders: [hbsLoader]
  111 + }),
92 new ExtractTextPlugin('[name].css'), 112 new ExtractTextPlugin('[name].css'),
93 new webpack.optimize.CommonsChunkPlugin({ 113 new webpack.optimize.CommonsChunkPlugin({
94 names: 'libs' 114 names: 'libs'
@@ -2,6 +2,7 @@ const merge = require('webpack-merge'); @@ -2,6 +2,7 @@ const merge = require('webpack-merge');
2 const webpack = require('webpack'); 2 const webpack = require('webpack');
3 const path = require('path'); 3 const path = require('path');
4 const devInfo = require('./dev-info.js'); 4 const devInfo = require('./dev-info.js');
  5 +var Visualizer = require('webpack-visualizer-plugin');
5 6
6 7
7 module.exports = merge(require('./webpack.base.config'), { 8 module.exports = merge(require('./webpack.base.config'), {
@@ -10,7 +11,10 @@ module.exports = merge(require('./webpack.base.config'), { @@ -10,7 +11,10 @@ module.exports = merge(require('./webpack.base.config'), {
10 path: path.resolve(__dirname, 'bundle') 11 path: path.resolve(__dirname, 'bundle')
11 }, 12 },
12 plugins: [ 13 plugins: [
13 - new webpack.HotModuleReplacementPlugin() 14 + new webpack.HotModuleReplacementPlugin(),
  15 + new Visualizer({
  16 + filename: './statistics.html'
  17 + })
14 ], 18 ],
15 devServer: { 19 devServer: {
16 contentBase: [path.join(__dirname, '../')], 20 contentBase: [path.join(__dirname, '../')],