vue.config.js 4.8 KB

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