vue.config.js 4.9 KB

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