vue.config.example.js 6.0 KB

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