vue.config.js 5.2 KB

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