vue.config.js 4.7 KB

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