vue.config.js 5.3 KB

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