|
| 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; |
0 commit comments