webpack.caohejingxin.conf.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. 'use strict'
  2. const path = require('path')
  3. const utils = require('./utils')
  4. const webpack = require('webpack')
  5. const config = require('../config')
  6. const merge = require('webpack-merge')
  7. const baseWebpackConfig = require('./webpack.base.conf')
  8. const CopyWebpackPlugin = require('copy-webpack-plugin')
  9. const HtmlWebpackPlugin = require('html-webpack-plugin')
  10. const ExtractTextPlugin = require('extract-text-webpack-plugin')
  11. const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
  12. const UglifyJsPlugin = require("uglifyjs-webpack-plugin")
  13. process.env.NODE_ENV = 'production'
  14. const env = config.build.env
  15. var baseOption = {"project":"caohejingxin"};
  16. const webpackConfig = merge(baseWebpackConfig(baseOption), {
  17. module: {
  18. rules: utils.styleLoaders({
  19. sourceMap: config.build.productionSourceMap,
  20. extract: true,
  21. project:baseOption.project
  22. })
  23. },
  24. devtool: config.build.productionSourceMap ? '#source-map' : false,
  25. output: {
  26. path: config.build.assetsRoot,
  27. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  28. chunkFilename: utils.assetsPath('js/[id].[chunkhash].js'),
  29. publicPath: process.env.NODE_ENV === 'production'
  30. ? config.build.assetsPublicPath
  31. : config.dev.assetsPublicPath
  32. },
  33. plugins: [
  34. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  35. new webpack.DefinePlugin({
  36. 'process.env': env,
  37. 'host.PROJECT': JSON.stringify(baseOption.project),
  38. 'host.MQTT':JSON.stringify("10.1.200.16"),
  39. }),
  40. // UglifyJs do not support ES6+, you can also use babel-minify for better treeshaking: https://github.com/babel/minify
  41. // new webpack.optimize.UglifyJsPlugin({
  42. // compress: {
  43. // warnings: false
  44. // },
  45. // sourceMap: true
  46. // }),
  47. new UglifyJsPlugin({
  48. // 使用外部引入的新版本的js压缩工具
  49. parallel: true,
  50. uglifyOptions: {
  51. ie8: false,
  52. ecma: 6,
  53. warnings: false,
  54. mangle: true, // debug false
  55. output: {
  56. comments: false,
  57. beautify: false, // debug true
  58. },
  59. compress: {
  60. warnings: false, // 在UglifyJs删除没有用到的代码时不输出警告
  61. drop_console: true, //删除所有的 `console`语句,还可以兼容ie浏览器
  62. collapse_vars: true, // 内嵌定义了但是只用到一次的变量
  63. reduce_vars: true, // 提取出出现多次但是没有定义成变量去引用的静态值
  64. }
  65. }
  66. }),
  67. // extract css into its own file
  68. new ExtractTextPlugin({
  69. filename: utils.assetsPath('css/[name].[contenthash].css')
  70. }),
  71. // Compress extracted CSS. We are using this plugin so that possible
  72. // duplicated CSS from different components can be deduped.
  73. new OptimizeCSSPlugin({
  74. cssProcessorOptions: {
  75. safe: true
  76. }
  77. }),
  78. // generate dist index.html with correct asset hash for caching.
  79. // you can customize output by editing /index.html
  80. // see https://github.com/ampedandwired/html-webpack-plugin
  81. new HtmlWebpackPlugin({
  82. filename: config.build.index,
  83. template: 'index.html',
  84. inject: true,
  85. minify: {
  86. removeComments: true,
  87. collapseWhitespace: true,
  88. removeAttributeQuotes: true
  89. // more options:
  90. // https://github.com/kangax/html-minifier#options-quick-reference
  91. },
  92. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  93. chunksSortMode: 'dependency'
  94. }),
  95. // keep module.id stable when vender modules does not change
  96. new webpack.HashedModuleIdsPlugin(),
  97. // split vendor js into its own file
  98. new webpack.optimize.CommonsChunkPlugin({
  99. name: 'vendor',
  100. minChunks: function (module) {
  101. // any required modules inside node_modules are extracted to vendor
  102. return (
  103. module.resource &&
  104. /\.js$/.test(module.resource) &&
  105. module.resource.indexOf(
  106. path.join(__dirname, '../node_modules')
  107. ) === 0
  108. )
  109. }
  110. }),
  111. // extract webpack runtime and module manifest to its own file in order to
  112. // prevent vendor hash from being updated whenever app bundle is updated
  113. new webpack.optimize.CommonsChunkPlugin({
  114. name: 'manifest',
  115. chunks: ['vendor']
  116. }),
  117. // copy custom static assets
  118. new CopyWebpackPlugin([
  119. {
  120. from: path.resolve(__dirname, '../static'),
  121. to: config.build.assetsSubDirectory,
  122. ignore: ['.*']
  123. }
  124. ])
  125. ]
  126. })
  127. if (config.build.productionGzip) {
  128. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  129. webpackConfig.plugins.push(
  130. new CompressionWebpackPlugin({
  131. asset: '[path].gz[query]',
  132. algorithm: 'gzip',
  133. test: new RegExp(
  134. '\\.(' +
  135. config.build.productionGzipExtensions.join('|') +
  136. ')$'
  137. ),
  138. threshold: 10240,
  139. minRatio: 0.8
  140. })
  141. )
  142. }
  143. if (config.build.bundleAnalyzerReport) {
  144. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  145. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  146. }
  147. module.exports = webpackConfig