Skip to content

Commit 6550409

Browse files
committed
webpack生产环境配置
1 parent ce78a01 commit 6550409

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.idea/
22
node_modules/
3+
dist/
34

45
.DS_Store
56
*.log

build/webpack.dist.config.js

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
const webpack = require('webpack');
2+
const path = require('path');
3+
const ExtractTextPlugin = require('extract-text-webpack-plugin');
4+
const HtmlWebpackPlugin = require('html-webpack-plugin');
5+
const CleanWebpackPlugin = require('clean-webpack-plugin');
6+
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
7+
8+
const webpackConfig = {
9+
entry: {
10+
app: path.resolve(__dirname, '../src/main.js'),
11+
vendor: ['vue']
12+
},
13+
output: {
14+
path: path.join(__dirname, '../dist'),
15+
filename: '[name].[chunkhash].js',
16+
publicPath: '/'
17+
},
18+
cache: false,
19+
devtool: false,
20+
resolve: {
21+
extensions: ['.js', '.vue'],
22+
alias: {
23+
vue$: 'vue/dist/vue.esm.js'
24+
}
25+
},
26+
module: {
27+
rules: [
28+
{
29+
test: /\.vue$/,
30+
loader: 'vue-loader',
31+
options: {
32+
extractCSS: true
33+
}
34+
},
35+
{
36+
test: /\.js$/,
37+
use: [
38+
{
39+
loader: 'babel-loader'
40+
}
41+
],
42+
exclude: /node_modules/
43+
},
44+
{
45+
test: /\.(png|jpg|gif|woff|woff2|eot|ttf|svg)$/,
46+
use: ['url-loader?limit=1024&name=images/[name].[hash:8].[ext]']
47+
},
48+
{
49+
test: /\.styl$/,
50+
use: ExtractTextPlugin.extract({
51+
fallback: 'style-loader',
52+
use: [
53+
'css-loader',
54+
'stylus-loader'
55+
]
56+
})
57+
}]
58+
},
59+
plugins: [
60+
new BundleAnalyzerPlugin(),
61+
new CleanWebpackPlugin(['dist'], {
62+
root: path.resolve(__dirname, '../')
63+
}),
64+
new webpack.DefinePlugin({
65+
'process.env': {
66+
NODE_ENV: JSON.stringify('production')
67+
}
68+
}),
69+
new webpack.HashedModuleIdsPlugin(),
70+
new webpack.optimize.CommonsChunkPlugin({
71+
name: ['vendor', 'runtime'],
72+
minChunks: 2
73+
}),
74+
new ExtractTextPlugin({
75+
filename: 'styles.[contenthash].css',
76+
disable: false,
77+
allChunks: true
78+
}),
79+
new webpack.optimize.UglifyJsPlugin({
80+
compress: {
81+
warnings: false
82+
},
83+
mangle: {
84+
except: ['$super', '$', 'exports', 'require'] // 以上变量‘$super’, ‘$’, ‘exports’ or ‘require’,不会被混淆
85+
},
86+
output: {
87+
comments: false
88+
}
89+
}),
90+
new webpack.LoaderOptionsPlugin({
91+
minimize: true
92+
}),
93+
new webpack.NoEmitOnErrorsPlugin(),
94+
new webpack.ProvidePlugin({
95+
$: 'jquery',
96+
jQuery: 'jquery',
97+
'window.jQuery': 'jquery'
98+
}),
99+
new HtmlWebpackPlugin({
100+
filename: 'index.html',
101+
template: path.resolve(__dirname, '../src/index.html'),
102+
inject: true,
103+
minify: {
104+
removeAttributeQuotes: true,
105+
removeComments: true,
106+
removeEmptyAttributes: true,
107+
collapseWhitespace: true
108+
}
109+
})
110+
]
111+
};
112+
113+
module.exports = webpackConfig;

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "main.js",
66
"dependencies": {},
77
"scripts": {
8-
"start": "node build/dev-server.js"
8+
"start": "node build/dev-server.js",
9+
"build": "webpack --config build/webpack.dist.config.js --progress --hide-modules"
910
},
1011
"keywords": [
1112
"vue",
@@ -21,6 +22,7 @@
2122
"babel-loader": "^7.1.2",
2223
"babel-plugin-transform-object-rest-spread": "^6.26.0",
2324
"babel-preset-env": "^1.6.0",
25+
"clean-webpack-plugin": "^0.1.16",
2426
"css-loader": "^0.28.5",
2527
"eslint": "^4.5.0",
2628
"eslint-config-airbnb-base": "^11.3.2",
@@ -37,6 +39,7 @@
3739
"vue-loader": "^13.0.4",
3840
"vue-template-compiler": "^2.4.2",
3941
"webpack": "^3.5.5",
42+
"webpack-bundle-analyzer": "^2.9.0",
4043
"webpack-dev-server": "^2.7.1"
4144
}
4245
}

0 commit comments

Comments
 (0)