vue.config.js 4.4 KB

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