You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

204 lines
7.5 KiB

  1. var path = require('path');
  2. var webpack = require('webpack');
  3. const CopyWebpackPlugin = require('copy-webpack-plugin');
  4. const HtmlWebpackPlugin = require('html-webpack-plugin');
  5. const CleanWebpackPlugin = require('clean-webpack-plugin');
  6. const packageInfo = require('./package.json');
  7. var ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin');
  8. const CompressionWebpackPlugin = require("compression-webpack-plugin");
  9. const FileManagerPlugin = require('filemanager-webpack-plugin');
  10. module.exports = (env = {}) => {
  11. var plugins = (module.exports.plugins || []).concat([
  12. new CleanWebpackPlugin(['dist']),
  13. new webpack.optimize.CommonsChunkPlugin({
  14. names: ['vendor'],
  15. minChunks: Infinity,
  16. filename: 'common.bundle.[chunkhash].js',
  17. }),
  18. new webpack.LoaderOptionsPlugin({
  19. minimize: true
  20. }),
  21. // new webpack.HotModuleReplacementPlugin(), //热加载插件
  22. //代码压缩插件,暂不需要
  23. // new FileManagerPlugin({
  24. // onEnd: {
  25. // mkdir: ['./zip'],
  26. // archive: [
  27. // { source: './dist', destination: './zip/test.zip' },
  28. // ]
  29. // }
  30. // }),
  31. new ParallelUglifyPlugin({
  32. // cacheDir: '/dist',
  33. uglifyJS: {
  34. output: {
  35. beautify: false,
  36. comments: false
  37. },
  38. compress: {
  39. // warnings: true,
  40. // drop_console:true,
  41. collapse_vars: true,
  42. reduce_vars: true
  43. }
  44. }
  45. })
  46. ])
  47. if (!env.Generative) {
  48. plugins = [];
  49. }
  50. plugins.push(
  51. new CompressionWebpackPlugin({
  52. filename: '[path].gz[query]', //压缩后的文件名
  53. algorithm: 'gzip', // 压缩格式 有:gzip、brotliCompress、
  54. test: /\.(js|css|svg)$/,
  55. threshold: 10240, // 只处理比这个值大的资源,按字节算
  56. minRatio: 0.8, //只有压缩率比这个值小的文件才会被处理,压缩率=压缩大小/原始大小,如果压缩后和原始文件大小没有太大区别,就不用压缩
  57. deleteOriginalAssets: false //是否删除原文件,最好不删除,服务器会自动优先返回同名的.gzip资源,如果找不到还可以拿原始文件
  58. }));
  59. plugins.push(new CopyWebpackPlugin([{
  60. // from: './src/static',
  61. // to: 'static'
  62. from: path.join(__dirname, './src/static'),
  63. to: 'static'
  64. },
  65. {
  66. from: path.join(__dirname, './static'),
  67. to: 'static'
  68. }]));
  69. plugins.push(new HtmlWebpackPlugin({
  70. // title: "自动生成网页标题",
  71. filename: "./index.html",
  72. hash: true,
  73. showErrors: true,
  74. version: packageInfo.version,
  75. template: './src/index.html'
  76. }));
  77. return {
  78. entry: {
  79. app: ['babel-polyfill', './src/main.js'],
  80. // entry:'./src/main.js',//入口
  81. vendor: ['vue', 'vuex', 'element-ui']
  82. // vendor: ['vue', 'vue-router', 'vuex', 'element-ui']
  83. },
  84. output: {
  85. path: path.resolve(__dirname, './dist'),//输出结果
  86. publicPath: env.production ? './' : '/', //文件路径
  87. filename: '[name].js',
  88. chunkFilename: 'js/[name].js'
  89. },
  90. module: {
  91. rules: [
  92. // 使用vue-loader 加载 .vue 结尾的文件
  93. {
  94. test: /\.vue$/,
  95. loader: 'vue-loader',
  96. options: {
  97. loaders: {
  98. },
  99. // other vue-loader options go here
  100. presets: ['es2015'],
  101. plugins: ['transform-runtime', 'transform-object-rest-spread'],
  102. },
  103. },
  104. // 使用babel 加载 .js 结尾的文件
  105. {
  106. test: /\.js$/,
  107. loader: 'babel-loader',
  108. exclude: /node_modules/,
  109. },
  110. // 加载图标
  111. {
  112. test: /\.(png|jpg|gif|svg)$/,
  113. loader: 'file-loader',
  114. options: {
  115. name: '[name].[ext]?[hash]',
  116. limit: 1000,
  117. outputPath: 'static/images/'
  118. }
  119. },
  120. //加载css
  121. {
  122. test: /\.css$/,
  123. loader: "style-loader!css-loader",
  124. // loader: 'style-loader!css-loader?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]',
  125. // exclude: /node_modules/
  126. },
  127. {
  128. test: /\.scss$/,
  129. use: ['style-loader', 'css-loader', 'sass-loader']
  130. },
  131. // {
  132. // test: /\.(woff|woff2|svg|ttf|eot)$/,
  133. // use:[
  134. // {loader:'file-loader',options:{name:'fonts/[name].[hash:8].[ext]'}}//项目设置打包到dist下的fonts文件夹下
  135. // // {loader:'url-loader',options:{name:'fonts/[name].[hash:8].[ext]'}}//项目设置打包到dist下的fonts文件夹下
  136. // ]
  137. // },
  138. {
  139. test: /\.(woff|woff2|svg|eot|ttf)\??.*$/,
  140. // loader: 'url-loader'
  141. loader: 'url-loader',
  142. options: {
  143. name: 'fonts/[name].[hash:8].[ext]',
  144. limit: 1000,
  145. }//项目设置打包到dist下的fonts文件夹下
  146. },
  147. // {
  148. // test: /\.html$/,
  149. // loader: 'html-loader',
  150. // options: {
  151. // minimize: false
  152. // }
  153. // }
  154. ]
  155. },
  156. resolve: {
  157. alias: {
  158. 'vue$': 'vue/dist/vue.esm.js'
  159. }
  160. },
  161. node: {
  162. fs: 'empty'
  163. },
  164. externals: {
  165. "BMap": "BMap",
  166. echarts: 'echarts',
  167. 'vue-ueditor-wrap': 'vue-ueditor-wrap',
  168. // 'element-ui': 'element-ui',//用了cdn引入这里直接不打包
  169. // bootstrap:'bootstrap'
  170. },
  171. devServer: {
  172. inline: true, //检测文件变化,实时构建并刷新浏览器
  173. port: "8888",
  174. proxy: {
  175. '/api': {
  176. target: 'https://admintest.malls.iformall.com/api',
  177. // target: 'https://admin.malls.iformall.com/api',
  178. // target: 'https://admin.malls.iformall.com/api',
  179. pathRewrite: {
  180. "^/api": ""
  181. },
  182. secure: false,
  183. changeOrigin: true
  184. },
  185. },
  186. // hot:true,
  187. disableHostCheck: true,//新版的webpack-dev-server修改了一些东西,默认检查hostname。如果hostname不是配置内的,将不可访问
  188. //404 页面返回 index.html
  189. historyApiFallback: true,
  190. },
  191. performance: {
  192. hints: false
  193. },
  194. plugins: plugins,
  195. // devtool: '#eval-source-map'//开发模式下更方便定位错误
  196. devtool: env.Generative ? false : '#eval-source-map'
  197. }
  198. }