vue.config.js 5.4 KB

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