vue.config.js 5.3 KB

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