vue.config.example.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * @description: 案例的打包配置
  3. * @Author: xing.heng
  4. */
  5. 'use strict'
  6. const path = require('path')
  7. const CompressionPlugin = require('compression-webpack-plugin')
  8. // const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
  9. const webpack = require('webpack')
  10. function resolve (dir) {
  11. return path.join(__dirname, dir)
  12. }
  13. const publicPath = process.env.VUE_APP_HISTORY === 'y' ? process.env.VUE_APP_BASE + '/' : ''
  14. const JS_CDN = [
  15. publicPath + 'static/libs/vuex/vuex.min.js',
  16. publicPath + 'static/libs/vue-router/vue-router.min.js'
  17. ]
  18. const CSS_CDN = []
  19. const cdn = {
  20. css: CSS_CDN,
  21. js: JS_CDN
  22. }
  23. const port = process.env.port || process.env.npm_config_port || 7521 // dev port
  24. const plugins = [
  25. new webpack.ProvidePlugin({
  26. jQuery: 'jquery',
  27. $: 'jquery',
  28. 'window.jQuery': 'jquery'
  29. })
  30. ]
  31. module.exports = {
  32. parallel: require('os').cpus().length > 1,
  33. pages: {
  34. index: {
  35. entry: ['example/main.js'],
  36. template: 'public/index.html',
  37. filename: 'index.html',
  38. chunks: 'all'
  39. }
  40. },
  41. publicPath:
  42. process.env.VUE_APP_HISTORY === 'y' ? process.env.VUE_APP_BASE : './',
  43. outputDir: process.env.VUE_APP_OUTPUT_DIR || 'dist',
  44. assetsDir: 'static',
  45. lintOnSave: false,
  46. productionSourceMap: false,
  47. runtimeCompiler: true,
  48. devServer: {
  49. hot: true,
  50. port: port,
  51. client: {
  52. overlay: false
  53. }
  54. },
  55. configureWebpack: config => {
  56. // provide the app's title in webpack's name field, so that
  57. // it can be accessed in index.html to inject the correct title.
  58. // name: name,
  59. config.cache = {
  60. type: 'filesystem',
  61. cacheDirectory: path.resolve(__dirname, '.cache')
  62. }
  63. Object.assign(config.resolve, {
  64. alias: {
  65. '@': resolve('example'),
  66. vue$: 'vue/dist/vue.common',
  67. // 大屏工程路径别名
  68. 'data-room-ui': resolve('packages'),
  69. '@gcpaas/data-room-ui': resolve('packages/index.js')
  70. },
  71. fallback: {
  72. path: false,
  73. fs: false
  74. }
  75. })
  76. // 如果是开发模式,忽略一些组件打包,采用cdn
  77. config.externals =
  78. process.env.NODE_ENV === 'production'
  79. ? {
  80. 'vue-router': 'VueRouter',
  81. vuex: 'Vuex'
  82. }
  83. : {}
  84. if (process.env.NODE_ENV === 'production') {
  85. return {
  86. plugins: [
  87. // new BundleAnalyzerPlugin(),
  88. new CompressionPlugin({
  89. cache: true, // 开启缓存
  90. test: /\.js$|\.html$|\.css$|\.jpg$|\.jpeg$|\.png/, // 需要压缩的文件类型
  91. threshold: 10240, // 归档需要进行压缩的文件大小最小值,10K以上的进行压缩
  92. deleteOriginalAssets: false // 是否删除原文件
  93. }),
  94. ...plugins
  95. ]
  96. }
  97. } else {
  98. return {
  99. plugins: [
  100. ...plugins
  101. ]
  102. }
  103. }
  104. },
  105. chainWebpack: config => {
  106. config.optimization.splitChunks({
  107. cacheGroups: {
  108. element: {
  109. name: 'element-ui',
  110. test: /[\\/]element-ui[\\/]/,
  111. chunks: 'all',
  112. priority: 10 // 优化将优先考虑具有更高 priority(优先级)的缓存组
  113. },
  114. // remotevue2loader: {
  115. // name: 'remote-vue2-loader',
  116. // test: /[\\/]remote-vue2-loader[\\/]/,
  117. // chunks: 'all',
  118. // priority: 10 // 优化将优先考虑具有更高 priority(优先级)的缓存组
  119. // },
  120. echarts: {
  121. name: 'echarts',
  122. test: /[\\/]echarts[\\/]/,
  123. chunks: 'all',
  124. priority: 10 // 优化将优先考虑具有更高 priority(优先级)的缓存组
  125. },
  126. babel: {
  127. name: '@babel',
  128. test: /[\\/]@babel[\\/]/,
  129. chunks: 'all',
  130. priority: 10 // 优化将优先考虑具有更高 priority(优先级)的缓存组
  131. },
  132. vueJsonEditor: {
  133. name: 'vue-json-editor',
  134. test: /[\\/]vue-json-editor[\\/]/,
  135. chunks: 'all',
  136. priority: 10 // 优化将优先考虑具有更高 priority(优先级)的缓存组
  137. },
  138. moment: {
  139. name: 'moment',
  140. test: /[\\/]moment[\\/]/,
  141. chunks: 'all',
  142. priority: 10 // 优化将优先考虑具有更高 priority(优先级)的缓存组
  143. },
  144. antv: {
  145. name: '@antv',
  146. test: /[\\/]@antv[\\/]/,
  147. chunks: 'all',
  148. priority: 10 // 优化将优先考虑具有更高 priority(优先级)的缓存组
  149. },
  150. vendors: {
  151. name: 'chunk-vendors',
  152. test: /[\\/]node_modules[\\/]/,
  153. chunks: 'all',
  154. priority: 2,
  155. reuseExistingChunk: true
  156. }
  157. }
  158. })
  159. if (process.env.NODE_ENV === 'production') {
  160. config.plugin('html-index').tap(args => {
  161. // html中添加cdn
  162. args[0].cdn = cdn
  163. // 修复 Lazy loading routes Error
  164. args[0].chunksSortMode = 'none'
  165. return args
  166. })
  167. }
  168. config.plugins.delete('prefetch-index') // 关闭prefetch
  169. config.module
  170. .rule('svg')
  171. .exclude.add(resolve('packages/assets/images/dataSourceIcon/svg'))
  172. .add(resolve('packages/assets/images/pageIcon/svg'))
  173. .add(resolve('packages/assets/images/bigScreenIcon/svg'))
  174. .add(resolve('packages/Svgs/svg'))
  175. .add(resolve('packages/assets/images/alignIcon/svg'))
  176. .end()
  177. config.module
  178. .rule('icons')
  179. .test(/\.svg$/)
  180. .include.add(resolve('packages/assets/images/dataSourceIcon/svg'))
  181. .add(resolve('packages/assets/images/pageIcon/svg'))
  182. .add(resolve('packages/assets/images/bigScreenIcon/svg'))
  183. .add(resolve('packages/Svgs/svg'))
  184. .add(resolve('packages/assets/images/alignIcon/svg'))
  185. .end()
  186. .use('svg-sprite-loader')
  187. .loader('svg-sprite-loader')
  188. .options({
  189. symbolId: 'icon-[name]'
  190. })
  191. .end()
  192. },
  193. // 在这里配置需要对node_modules中需要进行语法转义的依赖
  194. transpileDependencies: ['@antv/*']
  195. }