vue.config.js 4.8 KB

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