vue.config.js 5.1 KB

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