vue.config.js 5.1 KB

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