vue.config.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. const fs = require('fs')
  2. const path = require('path')
  3. const webpack = require('webpack')
  4. const port = process.env.port || 9527
  5. const isProd = process.env.NODE_ENV !== 'development'
  6. const isStaging = process.env.ENV !== 'production'
  7. const features = {
  8. __VERSION__: JSON.stringify(`v${require('./package.json').version}.${getTimestamp()}`),
  9. // 未开发完的路由
  10. __DEV__: !isProd,
  11. // 预览
  12. __PREVIEW__: isStaging,
  13. // 未开发的功能组件
  14. __PLACEHOLDER__: !isProd || false,
  15. // 传感器
  16. __SENSOR__: !isProd || false,
  17. __SENSOR_ELK__: false
  18. }
  19. const copyFiles = [
  20. { file: 'mediainfo.min.js', from: 'mediainfo.js/dist', to: '.' },
  21. { file: 'MediaInfoModule.wasm', from: 'mediainfo.js/dist', to: '.' },
  22. { file: 'spark-md5.min.js', from: 'spark-md5', to: '.' }
  23. ]
  24. function resolve (...dir) {
  25. return path.join(__dirname, ...dir)
  26. }
  27. function getCopyFiles () {
  28. return copyFiles.filter(({ file }) => !fs.existsSync(resolve('public', file))).map(({ file, from, to }) => {
  29. return { from: resolve('node_modules', from, file), to }
  30. })
  31. }
  32. function getTimestamp () {
  33. const now = new Date()
  34. return `${now.getFullYear()}${(now.getMonth() + 1).toString().padStart(2, '0')}${now.getDate().toString().padStart(2, '0')}${now.getHours().toString().padStart(2, '0')}${now.getMinutes().toString().padStart(2, '0')}${now.getSeconds().toString().padStart(2, '0')}`
  35. }
  36. module.exports = {
  37. publicPath: '/',
  38. outputDir: 'dist',
  39. assetsDir: '',
  40. lintOnSave: isProd,
  41. productionSourceMap: false,
  42. devServer: {
  43. port: port,
  44. open: false,
  45. overlay: {
  46. warnings: false,
  47. errors: true
  48. },
  49. before: require('./mock/mock-server.js'),
  50. staticOptions: {
  51. redirect: true
  52. },
  53. proxy: {
  54. '/': {
  55. // webSocket代理
  56. target: 'ws://10.180.88.84:8093/', // 内网
  57. ws: true, // 开启ws, 如果是http代理此处可以不用设置
  58. changeOrigin: true,
  59. pathRewrite: {
  60. '^/': '/'
  61. }
  62. }
  63. }
  64. },
  65. configureWebpack: {
  66. // provide the app's title in webpack's name field, so that
  67. // it can be accessed in index.html to inject the correct title.
  68. name: '浪潮屏媒安播云平台',
  69. resolve: {
  70. alias: {
  71. '@': resolve('src')
  72. }
  73. }
  74. },
  75. chainWebpack (config) {
  76. // it can improve the speed of the first screen, it is recommended to turn on preload
  77. config.plugin('preload').tap(() => [
  78. {
  79. rel: 'preload',
  80. // to ignore runtime.js
  81. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  82. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  83. include: 'initial'
  84. }
  85. ])
  86. // when there are many pages, it will cause too many meaningless requests
  87. config.plugins.delete('prefetch')
  88. config.plugin('feature-flags')
  89. .use(webpack.DefinePlugin, [features])
  90. // set svg-sprite-loader
  91. config.module
  92. .rule('svg')
  93. .exclude.add(resolve('src/icons'))
  94. .end()
  95. config.module
  96. .rule('icons')
  97. .test(/\.svg$/)
  98. .include.add(resolve('src/icons'))
  99. .end()
  100. .use('svg-sprite-loader')
  101. .loader('svg-sprite-loader')
  102. .options({
  103. symbolId: 'icon-[name]'
  104. })
  105. .end()
  106. config.plugin('copy')
  107. .tap(args => {
  108. const [patterns, ...oArgs] = args
  109. return [
  110. [
  111. ...patterns,
  112. ...getCopyFiles()
  113. ],
  114. ...oArgs
  115. ]
  116. })
  117. config
  118. .when(isProd,
  119. config => {
  120. config
  121. .plugin('ScriptExtHtmlWebpackPlugin')
  122. .after('html')
  123. .use('script-ext-html-webpack-plugin', [{
  124. // `runtime` must same as runtimeChunk name. default is `runtime`
  125. inline: /runtime\..*\.js$/
  126. }])
  127. .end()
  128. config
  129. .optimization.splitChunks({
  130. chunks: 'all',
  131. cacheGroups: {
  132. libs: {
  133. name: 'chunk-libs',
  134. test: /[\\/]node_modules[\\/]/,
  135. priority: 10,
  136. chunks: 'initial' // only package third parties that are initially dependent
  137. },
  138. elementUI: {
  139. name: 'chunk-elementUI', // split elementUI into a single package
  140. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  141. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  142. },
  143. commons: {
  144. name: 'chunk-commons',
  145. test: resolve('src/components'), // can customize your rules
  146. minChunks: 3, // minimum common number
  147. priority: 5,
  148. reuseExistingChunk: true
  149. }
  150. }
  151. })
  152. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  153. config
  154. .optimization.runtimeChunk('single')
  155. // optimization
  156. config
  157. .optimization.minimizer('terser').tap(args => {
  158. // clear console.x
  159. args[0].terserOptions.compress.drop_console = !isStaging
  160. return args
  161. })
  162. }
  163. )
  164. },
  165. css: {
  166. loaderOptions: {
  167. scss: {
  168. prependData: `
  169. @import '~@/scss/helpers/index';
  170. `
  171. }
  172. }
  173. }
  174. }