1 |
|
2 |
|
3 | var chalk = require('chalk')
|
4 | var path = require('path')
|
5 | var utils = require('../libs/utils')
|
6 | var webpack = require('webpack')
|
7 | var config = require('../config')
|
8 | var merge = require('webpack-merge')
|
9 | var baseWebpackConfig = require('./webpack.wx-base.conf')
|
10 | var CopyWebpackPlugin = require('copy-webpack-plugin-hash')
|
11 | var ExtractTextPlugin = require('extract-text-webpack-plugin')
|
12 | var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
|
13 | var maraConf = require(config.paths.marauder)
|
14 | var shouldUseSourceMap = !!maraConf.sourceMap
|
15 |
|
16 | module.exports = function(entry) {
|
17 | var webpackConfig = merge(baseWebpackConfig, {
|
18 | entry: entry,
|
19 | module: {
|
20 | rules: utils.styleLoaders({
|
21 | sourceMap: shouldUseSourceMap ? 'source-map' : false,
|
22 | extract: true
|
23 | })
|
24 | },
|
25 | bail: true,
|
26 | devtool: shouldUseSourceMap ? 'source-map' : false,
|
27 | output: {
|
28 | path: config.paths['wx-dist'],
|
29 | filename: utils.assetsPath('js/[name].js'),
|
30 | chunkFilename: utils.assetsPath('js/[id].js')
|
31 | },
|
32 | plugins: [
|
33 | // http://vuejs.github.io/vue-loader/en/workflow/production.html
|
34 | new webpack.DefinePlugin({
|
35 | 'process.env': config.build.env
|
36 | }),
|
37 | new webpack.optimize.UglifyJsPlugin({
|
38 | compress: {
|
39 | warnings: false
|
40 | },
|
41 | sourceMap: true
|
42 | }),
|
43 | // extract css into its own file
|
44 | new ExtractTextPlugin({
|
45 | // filename: utils.assetsPath('css/[name].[contenthash].css')
|
46 | filename: utils.assetsPath('css/[name].wxss')
|
47 | }),
|
48 | // Compress extracted CSS. We are using this plugin so that possible
|
49 | // duplicated CSS from different components can be deduped.
|
50 | new OptimizeCSSPlugin({
|
51 | cssProcessorOptions: {
|
52 | safe: true
|
53 | }
|
54 | }),
|
55 | // generate dist index.html with correct asset hash for caching.
|
56 | // you can customize output by editing /index.html
|
57 | // see https://github.com/ampedandwired/html-webpack-plugin
|
58 | // new HtmlWebpackPlugin({
|
59 | // filename: config.build.index,
|
60 | // template: 'index.html',
|
61 | // inject: true,
|
62 | // minify: {
|
63 | // removeComments: true,
|
64 | // collapseWhitespace: true,
|
65 | // removeAttributeQuotes: true
|
66 | // // more options:
|
67 | // // https://github.com/kangax/html-minifier#options-quick-reference
|
68 | // },
|
69 | // // necessary to consistently work with multiple chunks via CommonsChunkPlugin
|
70 | // chunksSortMode: 'dependency'
|
71 | // }),
|
72 | // keep module.id stable when vender modules does not change
|
73 | new webpack.HashedModuleIdsPlugin(),
|
74 | // split vendor js into its own file
|
75 | new webpack.optimize.CommonsChunkPlugin({
|
76 | name: 'vendor',
|
77 | minChunks: function(module, count) {
|
78 | // any required modules inside node_modules are extracted to vendor
|
79 | return (
|
80 | module.resource &&
|
81 | /\.js$/.test(module.resource) &&
|
82 | module.resource.indexOf('node_modules') >= 0
|
83 | ) || count >= 2
|
84 | }
|
85 | }),
|
86 | // extract webpack runtime and module manifest to its own file in order to
|
87 | // prevent vendor hash from being updated whenever app bundle is updated
|
88 | new webpack.optimize.CommonsChunkPlugin({
|
89 | name: 'manifest',
|
90 | chunks: ['vendor']
|
91 | }),
|
92 | // copy custom static assets
|
93 | new CopyWebpackPlugin([{
|
94 | from: config.paths['wx-assets'],
|
95 | to: config.paths['wx-assets-dist'],
|
96 | ignore: ['.*']
|
97 | }])
|
98 | ]
|
99 | })
|
100 |
|
101 | // if (config.build.productionGzip) {
|
102 | // var CompressionWebpackPlugin = require('compression-webpack-plugin')
|
103 |
|
104 | // webpackConfig.plugins.push(
|
105 | // new CompressionWebpackPlugin({
|
106 | // asset: '[path].gz[query]',
|
107 | // algorithm: 'gzip',
|
108 | // test: new RegExp(
|
109 | // '\\.(' +
|
110 | // config.build.productionGzipExtensions.join('|') +
|
111 | // ')$'
|
112 | // ),
|
113 | // threshold: 10240,
|
114 | // minRatio: 0.8
|
115 | // })
|
116 | // )
|
117 | // }
|
118 |
|
119 | if (config.build.bundleAnalyzerReport) {
|
120 | var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
|
121 | webpackConfig.plugins.push(new BundleAnalyzerPlugin())
|
122 | }
|
123 |
|
124 | return webpackConfig;
|
125 | }
|