vue.config.js 4.5 KB

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