vue.config.js 4.6 KB

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