vue.config.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. devServer: {
  50. port,
  51. open: true,
  52. proxy: {
  53. [process.env.VUE_APP_BASE_API]: {
  54. target: `http://172.31.33.2:20001`,
  55. changeOrigin: true
  56. }
  57. }
  58. // disableHostCheck: true
  59. },
  60. configureWebpack: {
  61. // provide the app's title in webpack's name field, so that
  62. // it can be accessed in index.html to inject the correct title.
  63. name: process.env.VUE_APP_NAME,
  64. resolve: {
  65. alias: {
  66. '@': resolve('src')
  67. }
  68. },
  69. performance: {
  70. maxAssetSize: 5 * 1024 * 1024,
  71. maxEntrypointSize: 2 * 1024 * 1024
  72. }
  73. },
  74. chainWebpack (config) {
  75. // it can improve the speed of the first screen, it is recommended to turn on preload
  76. config.plugin('preload').tap(() => [
  77. {
  78. rel: 'preload',
  79. // to ignore runtime.js
  80. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  81. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  82. include: 'initial'
  83. }
  84. ])
  85. // when there are many pages, it will cause too many meaningless requests
  86. config.plugins.delete('prefetch')
  87. config.plugin('feature-flags')
  88. .use(webpack.DefinePlugin, [features])
  89. // set svg-sprite-loader
  90. config.module
  91. .rule('svg')
  92. .exclude.add(resolve('src/icons'))
  93. .end()
  94. config.module
  95. .rule('icons')
  96. .test(/\.svg$/)
  97. .include.add(resolve('src/icons'))
  98. .end()
  99. .use('svg-sprite-loader')
  100. .loader('svg-sprite-loader')
  101. .options({
  102. symbolId: 'icon-[name]'
  103. })
  104. .end()
  105. config.plugin('copy')
  106. .tap(args => {
  107. const [patterns, ...oArgs] = args
  108. return [
  109. [
  110. ...patterns,
  111. ...getCopyFiles()
  112. ],
  113. ...oArgs
  114. ]
  115. })
  116. config
  117. .when(isProd, config => {
  118. config
  119. .plugin('ScriptExtHtmlWebpackPlugin')
  120. .after('html')
  121. .use('script-ext-html-webpack-plugin', [{
  122. // `runtime` must same as runtimeChunk name. default is `runtime`
  123. inline: /runtime\..*\.js$/
  124. }])
  125. .end()
  126. // config
  127. // .plugin('BundleAnalyzerPlugin')
  128. // .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
  129. config
  130. .optimization.splitChunks({
  131. chunks: 'all',
  132. cacheGroups: {
  133. libs: {
  134. name: 'chunk-libs',
  135. test: /[\\/]node_modules[\\/]/,
  136. priority: 10,
  137. chunks: 'initial' // only package third parties that are initially dependent
  138. },
  139. elementUI: {
  140. name: 'chunk-elementUI', // split elementUI into a single package
  141. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  142. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  143. },
  144. commons: {
  145. name: 'chunk-commons',
  146. test: resolve('src/components'), // can customize your rules
  147. minChunks: 3, // minimum common number
  148. priority: 5,
  149. reuseExistingChunk: true
  150. }
  151. }
  152. })
  153. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  154. config
  155. .optimization.runtimeChunk('single')
  156. config
  157. .optimization.minimizer('terser').tap(args => {
  158. // clear console.x
  159. args[0].terserOptions.compress.drop_console = !logger
  160. return args
  161. })
  162. })
  163. },
  164. css: {
  165. loaderOptions: {
  166. scss: {
  167. prependData: `
  168. @import '~@/scss/helpers/index';
  169. `
  170. }
  171. }
  172. }
  173. }