vue.config.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. },
  54. configureWebpack: {
  55. // provide the app's title in webpack's name field, so that
  56. // it can be accessed in index.html to inject the correct title.
  57. name: '浪潮屏媒安播云平台',
  58. resolve: {
  59. alias: {
  60. '@': resolve('src')
  61. }
  62. }
  63. },
  64. chainWebpack (config) {
  65. // it can improve the speed of the first screen, it is recommended to turn on preload
  66. config.plugin('preload').tap(() => [
  67. {
  68. rel: 'preload',
  69. // to ignore runtime.js
  70. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  71. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  72. include: 'initial'
  73. }
  74. ])
  75. // when there are many pages, it will cause too many meaningless requests
  76. config.plugins.delete('prefetch')
  77. config.plugin('feature-flags')
  78. .use(webpack.DefinePlugin, [features])
  79. // set svg-sprite-loader
  80. config.module
  81. .rule('svg')
  82. .exclude.add(resolve('src/icons'))
  83. .end()
  84. config.module
  85. .rule('icons')
  86. .test(/\.svg$/)
  87. .include.add(resolve('src/icons'))
  88. .end()
  89. .use('svg-sprite-loader')
  90. .loader('svg-sprite-loader')
  91. .options({
  92. symbolId: 'icon-[name]'
  93. })
  94. .end()
  95. config.plugin('copy')
  96. .tap(args => {
  97. const [patterns, ...oArgs] = args
  98. return [
  99. [
  100. ...patterns,
  101. ...getCopyFiles()
  102. ],
  103. ...oArgs
  104. ]
  105. })
  106. config
  107. .when(isProd,
  108. config => {
  109. config
  110. .plugin('ScriptExtHtmlWebpackPlugin')
  111. .after('html')
  112. .use('script-ext-html-webpack-plugin', [{
  113. // `runtime` must same as runtimeChunk name. default is `runtime`
  114. inline: /runtime\..*\.js$/
  115. }])
  116. .end()
  117. config
  118. .optimization.splitChunks({
  119. chunks: 'all',
  120. cacheGroups: {
  121. libs: {
  122. name: 'chunk-libs',
  123. test: /[\\/]node_modules[\\/]/,
  124. priority: 10,
  125. chunks: 'initial' // only package third parties that are initially dependent
  126. },
  127. elementUI: {
  128. name: 'chunk-elementUI', // split elementUI into a single package
  129. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  130. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  131. },
  132. commons: {
  133. name: 'chunk-commons',
  134. test: resolve('src/components'), // can customize your rules
  135. minChunks: 3, // minimum common number
  136. priority: 5,
  137. reuseExistingChunk: true
  138. }
  139. }
  140. })
  141. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  142. config
  143. .optimization.runtimeChunk('single')
  144. // optimization
  145. config
  146. .optimization.minimizer('terser').tap(args => {
  147. // clear console.x
  148. args[0].terserOptions.compress.drop_console = !isStaging
  149. return args
  150. })
  151. }
  152. )
  153. },
  154. css: {
  155. loaderOptions: {
  156. scss: {
  157. prependData: `
  158. @import '~@/scss/helpers/index';
  159. `
  160. }
  161. }
  162. }
  163. }